Python Integration
Integrate PureSignup with Flask, Django, FastAPI, or any Python web framework.
Installation
Install requests library
pip install requestsBasic Implementation
PureSignup.py
import os
import requests
from typing import Optional, Dict
class PureSignupClient:
def __init__(self, api_key: Optional[str] = None):
self.api_key = api_key or os.environ.get('PureSignup_API_KEY')
self.base_url = 'https://puresignup.com/api/v1'
def check_email(self, email: str) -> Optional[Dict]:
"""Check if an email is disposable."""
try:
response = requests.get(
f'{self.base_url}/check',
params={'q': email},
headers={'Authorization': f'Bearer {self.api_key}'},
timeout=5
)
if not response.ok:
print(f'API error: {response.status_code}')
return None
data = response.json()
return data.get('data')
except Exception as e:
print(f'PureSignup check failed: {e}')
return None # Fail open
def is_disposable(self, email: str) -> bool:
"""Returns True if email is disposable."""
result = self.check_email(email)
return result and result.get('risk_level') == 'high'
# Usage
client = PureSignupClient()
if client.is_disposable('user@guerrillamail.com'):
print('Disposable email detected!')Flask Integration
app.py
from flask import Flask, request, jsonify
from PureSignup import PureSignupClient
app = Flask(__name__)
shield_client = PureSignupClient()
@app.route('/api/signup', methods=['POST'])
def signup():
data = request.get_json()
email = data.get('email')
if not email:
return jsonify({'error': 'Email is required'}), 400
# Check if email is disposable
if shield_client.is_disposable(email):
return jsonify({
'error': 'Disposable email addresses are not allowed',
'field': 'email'
}), 400
# Continue with signup logic
# ... create user ...
return jsonify({'message': 'Signup successful'}), 201
if __name__ == '__main__':
app.run(debug=True)Django Integration
validators.py
from django.core.exceptions import ValidationError
from .PureSignup import PureSignupClient
shield_client = PureSignupClient()
def validate_not_disposable(email):
"""Django validator to check for disposable emails."""
if shield_client.is_disposable(email):
raise ValidationError(
'Disposable email addresses are not allowed.',
code='disposable_email'
)models.py
from django.db import models
from .validators import validate_not_disposable
class User(models.Model):
email = models.EmailField(
unique=True,
validators=[validate_not_disposable]
)
name = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)