A small FastAPI backend that receives contact/lead form submissions from my websites, saves them to SQLite, and emails the site owner.
One backend serves multiple sites. Each site has a site_key that the form sends along with the submission.
- Python 3.10
- FastAPI
- SQLite (stdlib
sqlite3) - Pydantic for request validation
- Resend for email notifications
app/
main.py FastAPI app, routes, CORS, startup
config.py Loads .env, paths, CORS origins
database.py SQLite connection and table creation
db_functions.py Insert/select/update queries
models.py Pydantic request/response models
email.py Resend email notification
logging_config.py Console + rotating file logs
scripts/
create_site.py Add a site to the database (CLI)
backup_database.py Timestamped SQLite backup
test_database_restore.py Check that a backup restores cleanly
git clone https://github.com/andrew-fraley/lead-form-backend.git
cd lead-form-backend
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .envEdit .env:
| Variable | Description |
|---|---|
RESEND_API_KEY |
Resend API key |
EMAIL_FROM |
Sender address (must be a verified Resend domain) |
CORS_ORIGINS |
Comma-separated list of allowed origins |
DATA_DIR |
Absolute path to the writable directory for db, logs, and backups. |
The app will not start if RESEND_API_KEY or EMAIL_FROM is missing. CORS_ORIGINS defaults to http://localhost:1313.
The code folder is read-only for the service. Everything the app writes goes in DATA_DIR:
the database, logs/, backups/ and restore-test.db. .env stays in the code folder.
DATA_DIR must exist and be writable before the app starts. Locally it defaults to the project folder.
uvicorn app.main:app --reloadThe database file (lead-form-database.db) is created in the DATA_DIR on first startup. Tables are created if they don't exist.
Note: Scripts use the same DATA_DIR as the app so run them with it set.
Run from the project root:
python -m scripts.create_siteIt prompts for a site key, site name, domain, and the email address that should receive notifications.
Health check. Returns {"message": "Lead-form backend is working!"}.
Request body (JSON):
| Field | Type | Required | Max length |
|---|---|---|---|
site_key |
string | yes | 100 |
email |
yes | 254 | |
name |
string | no | 100 |
phone |
string | no | 30 |
message |
string | no | 2000 |
page_url |
string | no | 2048 |
form_context |
string | no | 100 |
honeypot |
string | no | 100 |
Example:
curl -X POST http://localhost:8000/leads \
-H "Content-Type: application/json" \
-d '{
"site_key": "acme-plumbing",
"name": "Jane",
"email": "jane@example.com",
"message": "Need a quote"
}'Responses:
200{"success": true, "message": "Lead submitted successfully."}403unknownsite_key, or the site is inactive422failed validation (bad email, field too long, missing required field)
- Request is validated by the Pydantic model.
- If
honeypothas a value, the request is treated as a bot. It is logged, nothing is saved, and a normal success response is returned. - The site is looked up by
site_key. Unknown or inactive sites get a 403. - The lead is saved to
lead_submissionswithnotification_status = 'pending'. - An email is sent to the site's
notification_emailthrough Resend. notification_statusis updated toacceptedorfailed(with the error text). If the email fails, the lead is still saved and the client still gets a success response.
Two tables:
sites:id,site_key(unique),site_name,domain,notification_email,active,created_atlead_submissions:id,site_id(foreign key),name,email,phone,message,page_url,form_context,created_at,notification_status,notification_attempted_at,notification_error
Create a backup (saved to backups/ inside DATA_DIR with a timestamp):
python -m scripts.backup_databaseTest that a backup restores and passes SQLite's integrity check:
python -m scripts.test_database_restore backups/<backup-file>.dbThe restore test writes to restore-test.db in DATA_DIR and refuses to run if that file already exists. Delete it when you are done.
Logs go to the terminal and to logs/lead-form-backend.log (rotates at about 1 MB, keeps 5 old files).
.env, *.db, backups/, and logs/ are in .gitignore.