A multi-tenant backend API for WordPress chatbot integration, built with FastAPI and Supabase. This server manages tenant registration, authentication, and provides a secure foundation for chatbot services across multiple WordPress sites.
- Multi-Tenant Architecture: UUID-based tenant isolation ensures complete data separation
- Automatic Credential Generation: Secure API keys and tenant IDs generated server-side
- WordPress Callback Verification: Validates registration requests by calling back to WordPress
- Daily Rolling Logs: Automatic log rotation with configurable retention policies
- Comprehensive Validation: Email, URL, and API key validation with detailed error messages
- RESTful API: Clean, well-documented endpoints for tenant management
- Production-Ready: Error handling, logging, retry logic, and security best practices
- Python 3.8+
- Supabase account with a project
- PostgreSQL database (via Supabase)
git clone <repository-url>
cd eaglechatserver# Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install required packages
pip install -r requirements.txt- Log into your Supabase project dashboard
- Navigate to the SQL Editor
- Run the SQL scripts in order:
- First, execute all SQL from
suprabase.md(creates the documents table and vector search functions) - Then, execute all SQL from
suprabase_tenant.md(creates the tenants table and management functions)
- First, execute all SQL from
-
Copy the example configuration:
cp config.example.json config.json
-
Edit
config.jsonwith your Supabase credentials:{ "supabase": { "url": "https://your-project.supabase.co", "service_role_key": "your-service-role-key" }, "logging": { "level": "INFO", "retention_days": 30, "log_directory": "logs" }, "api": { "title": "Eagle Chat Server", "description": "Multi-tenant chatbot backend for WordPress", "version": "1.0.0" }, "callback": { "retry_attempts": 3, "retry_delay_seconds": 3 } }Finding your Supabase credentials:
- URL: Settings β API β Project URL
- Service Role Key: Settings β API β Service Role Key (secret)
# Development mode with auto-reload
uvicorn main:app --reload
# Production mode
uvicorn main:app --host 0.0.0.0 --port 8000The server will start at http://localhost:8000
Once running, access the interactive API docs at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
GET /Returns server status, version, and health information.
POST /api/v1/register
Content-Type: application/json
{
"site_url": "https://your-wordpress-site.com",
"admin_email": "admin@your-site.com",
"callback_token": "wordpress_generated_token_here"
}Registration Workflow:
- WordPress plugin generates a temporary callback token (expires in 60 seconds)
- Plugin sends registration request with site URL, admin email, and callback token
- FastAPI server immediately calls back to WordPress at
/wp-json/eaglechat-plugin/v1/verify - WordPress verifies the token matches what it generated
- On successful verification, FastAPI generates and returns tenant credentials
Response:
{
"success": true,
"tenant_id": "123e4567-e89b-12d3-a456-426614174000",
"api_key": "eck_AbCdEfGhIjKlMnOpQrStUvWxYz0123456789",
"message": "Tenant registered successfully"
}Error Responses:
400: Site URL already registered, email already associated, or callback verification failed500: Server error
POST /api/v1/validate
Content-Type: application/json
{
"tenant_id": "123e4567-e89b-12d3-a456-426614174000",
"api_key": "eck_AbCdEfGhIjKlMnOpQrStUvWxYz0123456789"
}Response:
{
"valid": true,
"message": "Credentials are valid"
}Error Response:
401: Invalid credentials
The test_api.http file contains pre-configured test scenarios:
- VS Code: Install the "REST Client" extension by Huachao Mao
- IntelliJ IDEA: Use the built-in HTTP Client
- Command Line: Use curl or httpie
# Health check
curl http://localhost:8000/
# Register a new tenant
curl -X POST http://localhost:8000/api/v1/register \
-H "Content-Type: application/json" \
-d '{
"site_url": "https://test-site.com",
"admin_email": "admin@test-site.com",
"callback_token": "test_callback_token_1234567890"
}'
# Validate tenant (use actual values from registration)
curl -X POST http://localhost:8000/api/v1/validate \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "your-tenant-id",
"api_key": "your-api-key"
}'- Successful Registration: Register a new site with valid URL, email, and callback token
- Duplicate Site URL: Attempt to register the same URL twice (should fail)
- Duplicate Email: Use an already registered email (should fail)
- Invalid Email Format: Test email validation
- Invalid URL Format: Test URL validation
- Invalid Callback Token: Test with short or missing callback token
- WordPress Callback Failure: Test when WordPress doesn't verify the token
- Valid Credentials: Validate with correct tenant ID and API key
- Invalid Credentials: Test authentication failure
The callback verification includes configurable retry logic:
- retry_attempts: Number of times to retry the WordPress callback (default: 3)
- retry_delay_seconds: Delay between retries (default: 3 seconds)
Logs are automatically created in the logs/ directory with the format:
logs/
βββ 20250124_LOG.log # Today's log
βββ 20250123_LOG.log # Yesterday's log
βββ ...
2025-01-24 10:30:45 - eaglechat - INFO - Registration request received for site: https://example.com
2025-01-24 10:30:46 - eaglechat - INFO - Successfully registered tenant: 123e4567... for site: https://example.com
- Configured in
config.json(default: 30 days) - Old logs are automatically deleted
- Adjust
retention_daysas needed (1-365)
-
API Keys:
- Generated using cryptographically secure random functions
- 48+ characters with prefix "eck_"
- Only alphanumeric, hyphens, and underscores allowed
-
Tenant IDs:
- Standard UUID v4 format
- Globally unique identifiers
-
Configuration:
- Sensitive credentials stored in
config.json - File excluded from version control via
.gitignore - Use environment variables in production
- Sensitive credentials stored in
-
Validation:
- Strict email validation
- URL format validation
- Input sanitization
- Use environment variables for sensitive configuration
- Set up a reverse proxy (nginx/Apache)
- Enable HTTPS with SSL certificates
- Configure CORS appropriately (not "*" in production)
- Set up monitoring and alerting
- Regular backup of Supabase database
- Use a process manager (systemd, supervisor)
- The server uses async/await for all database operations
- Logging is configured at both application and request levels
- All endpoints return appropriate HTTP status codes
- Comprehensive error messages aid in debugging
- Follow the existing code style
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting
[Add your license information here]
- "config.json not found": Create it from
config.example.json - Database connection errors: Verify Supabase credentials
- "Table does not exist": Run the SQL setup scripts
- Port already in use: Change port with
--port 8001
Set logging level to "DEBUG" in config.json for verbose output:
{
"logging": {
"level": "DEBUG"
}
}