Overview
CORS (Cross-Origin Resource Sharing) is a security mechanism that controls which domains can access your API. This guide explains the current CORS setup and how to configure it for production environments.Current CORS Configuration
The API currently uses permissive CORS settings suitable for development:main.py (lines 15-22)
CORS Parameters Explained
allow_origins
allow_origins
Current Value:
["*"]Description: List of origins (domains) allowed to access the APIWildcard (*): Allows requests from any domainSecurity Impact: High risk in production - allows anyone to call your API from any websiteRecommended: Specify exact domains in productionallow_credentials
allow_credentials
Current Value:
TrueDescription: Whether to allow cookies and authentication headersWhen True:- Accepts cookies with requests
- Allows Authorization headers
- Enables session-based authentication
allow_origins=["*"] in browsers - must specify exact originsallow_methods
allow_methods
Current Value:
["*"]Description: HTTP methods allowed for cross-origin requestsWildcard (*): Allows all methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, etc.)Recommended: Specify only needed methods (e.g., ["GET", "POST"])allow_headers
allow_headers
Current Value:
["*"]Description: HTTP headers allowed in cross-origin requestsWildcard (*): Allows all headersCommon Headers:Content-TypeAuthorizationAcceptOrigin
Production CORS Configuration
Recommended Production Setup
For production deployments, restrict CORS to specific trusted domains:- Single Domain
- Multiple Domains
- Development + Production
- Environment Variables
main.py
This configuration:
- Only allows requests from
https://yourdomain.com - Enables credentials (cookies/auth)
- Restricts to GET and POST methods
- Limits headers to Content-Type and Authorization
Configuration by Use Case
Public API
Scenario: API available to any domain
Single Page Application
Scenario: React/Vue/Angular frontend
Mobile App Backend
Scenario: API for mobile applications
Native mobile apps bypass CORS. Focus on authentication instead.
Internal Tools
Scenario: Admin dashboards and internal tools
Testing CORS Configuration
Using cURL
Test CORS headers from command line:Test Preflight Request
Using Browser Console
Test from browser’s JavaScript console:Common CORS Errors
No 'Access-Control-Allow-Origin' header
No 'Access-Control-Allow-Origin' header
Error Message:Cause: CORS middleware not configured or origin not in allowed listSolution:
- Verify CORS middleware is added to FastAPI app
- Check that requesting origin is in
allow_originslist - Ensure middleware is added before route definitions
Wildcard with credentials
Wildcard with credentials
Error Message:Cause: Using
allow_origins=["*"] with allow_credentials=TrueSolution:- Change
allow_originsto specific domains, OR - Set
allow_credentials=False
Method not allowed
Method not allowed
Error Message:Cause: The HTTP method is not in
allow_methods listSolution: Add the required method:Header not allowed
Header not allowed
Error Message:Cause: Custom header not in
allow_headers listSolution: Add the required header:Security Best Practices
Monitoring CORS Issues
Logging CORS Requests
Add logging to track CORS requests:main.py
Additional Resources
FastAPI CORS Guide
Official FastAPI CORS documentation
MDN CORS Guide
Comprehensive CORS reference
Docker Deployment
Configure CORS in Docker containers
API Reference
Test CORS with API endpoints