A secure FastAPI backend for receiving contact form submissions from a public website. This API implements multiple layers of security including token-based authentication, request signing, rate limiting, and CORS protection.
- Token-based Authentication: Short-lived JWT tokens (5 minutes default)
- Request Signing: HMAC-SHA256 signatures prevent replay attacks
- Rate Limiting: Per-IP and per-origin rate limiting
- CORS Protection: Configurable allowed origins
- Structured Logging: JSON-formatted logs streamed to stdout (journald-compatible)
- Email Notifications: Async SMTP email delivery
- Security Headers: Standard security headers on all responses
- Input Validation: Comprehensive Pydantic validation with sanitization
- Clone the repository:
cd contact_us_api- Create a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -e .Or install without editable mode:
pip install .- Configure the application:
- Copy
.env.exampleto.envand fill in your values - See Configuration Guide for all options
- Important: Change default security secrets in production!
- Copy
The application uses environment variables for configuration. All variables use the CONTACT_API_ prefix to avoid conflicts with system variables.
For local development, create a .env file in the project root:
cp .env.example .env
# Edit .env with your configuration valuesFor a complete reference of all configuration options, see the Configuration Guide.
cd src
python main.pyFor production deployment instructions, including systemd service setup, see Production Deployment Guide.
The HTTP endpoints are covered in the Endpoints markdown document.
For detailed frontend integration examples and code snippets, see the Frontend Integration Guide.
- Change Default Secrets: Always change the default token and signing secrets in production (see Configuration Guide)
- HTTPS Only: Use HTTPS in production to protect tokens and signatures in transit
- Secret Management: Use environment variables or a secrets manager (never commit secrets to version control)
- Signing Secret: The signing secret is kept secure on the server. Use the
/api/signatureendpoint to generate signatures server-side - never expose the signing secret to the frontend - Rate Limiting: Adjust rate limits based on your traffic patterns
- CORS Origins: Only allow trusted origins in the CORS configuration
- Environment Variables: The
.envfile is excluded from git. Never commit secrets to the repository
For detailed security best practices, see the Configuration Guide and Production Deployment Guide.
Logs are streamed to stdout in JSON format (without timestamps, as journald provides them). When running as a systemd service, logs are captured by journald and can be viewed with journalctl.
Log entries include:
- Request details (method, path, IP, origin)
- Security events (token generation, validation failures)
- Contact submissions
- Rate limit violations
- Errors and exceptions (with full tracebacks)
400: Invalid request data or missing fields401: Invalid or expired token403: Invalid signature or CORS violation429: Rate limit exceeded (includes Retry-After header)500: Internal server error (generic message to client)
For information about testing the API and email configuration, see the Testing Guide.
Additional documentation is available:
- Configuration Reference - Complete reference for all environment variables and configuration options
- Frontend Integration Guide - How to integrate the API with your website
- Testing Guide - How to test the API and email configuration
- Production Deployment Guide - Production deployment, systemd service setup, and best practices
- Docker Setup - Docker build, deployment, and GitHub Container Registry setup
- Pydantic Settings Notes - Technical notes on Pydantic settings implementation (for developers)
contact_us_api/
├── LICENSE # MIT License
├── pyproject.toml # Python project configuration and dependencies
├── README.md # This file
├── tests/ # Test scripts
│ ├── test_api.py # Test script to simulate website interaction
│ └── test_email.py # Test script to verify SMTP email credentials
├── docs/ # Additional documentation
│ ├── configuration.md # Configuration reference
│ ├── endpoints.md # API endpoints documentation
│ ├── frontend.md # Frontend integration guide
│ ├── production.md # Production deployment guide
│ ├── testing.md # Testing guide
│ └── Regarding_Pydantic.md # Technical notes on Pydantic settings
├── docker/ # Docker configuration
│ ├── README.md # Docker setup and deployment guide
│ ├── Dockerfile # Docker build configuration
│ └── docker-compose.yml # Docker Compose example
└── src/
├── main.py # FastAPI application entry point
└── app/
├── __init__.py
├── models.py # Pydantic models
├── security.py # Token and signature handling
├── rate_limiter.py # Rate limiting logic
├── email_service.py # SMTP email service
├── logger.py # Logging configuration
└── middleware.py # CORS, security headers, etc.
This project is licensed under the MIT License. See the LICENSE file for details.
For issues or questions, contact your support email address.