Flask-CORS is a Flask extension that simplifies the implementation of Cross-Origin Resource Sharing (CORS) in Flask applications, enabling cross-origin AJAX requests. It supports global, resource-specific, and route-specific CORS configurations. The current version is 6.0.2, and it maintains an active release cadence with regular updates and security patches.
pip install Flask-CORSVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates enabling CORS globally for an entire Flask application and also for a specific route using the `@cross_origin` decorator. Global enablement is done by initializing `CORS(app)`. For fine-grained control, the `@cross_origin` decorator allows specifying allowed origins, methods, and credential support for individual routes.
Review your CORS configurations, especially those with multiple resource paths, to ensure they match the new specificity order. Test cross-origin requests thoroughly to confirm expected behavior. Ensure your application's request paths are consistently cased if matching rules rely on it.
If your application requires private network access, consult the Flask-CORS documentation for the specific configuration option to re-enable it. Typically, this involves setting a configuration flag.
Upgrade your Python environment to 3.8 or newer before upgrading to Flask-CORS 4.0.0+.
Implement CSRF protection (e.g., Flask-WTF CSRFProtect) when `supports_credentials=True` is enabled. Carefully define `origins` to restrict access to trusted domains only.
Always specify a list of explicit, trusted `origins` (e.g., `origins=['http://localhost:3000', 'https://your-frontend.com']`) instead of `*` in production deployments.
Always provide complete origin URLs, including schema and port, in the `origins` list or string.
Ensure `cross_origin` is imported from `flask_cors` (e.g., `from flask_cors import cross_origin`).
Ensure that `from flask_cors import cross_origin` is included at the top of any file where the `@cross_origin` decorator is used.
Install the package using pip: `pip install flask-cors`
Initialize Flask-CORS on your Flask app, specifying the allowed origins. For all origins (development): `from flask_cors import CORS; CORS(app)`. For specific origins: `CORS(app, origins=['http://your-frontend.com'])`
Ensure `flask-cors` is configured to allow the specific HTTP methods and headers for your routes. When initializing `CORS(app)`, you can specify `methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']` and `headers=['Content-Type', 'Authorization']` (adjust as needed). Also, ensure `OPTIONS` requests are handled, which Flask-CORS does by default when enabled globally or per route with `@cross_origin()`.
When using `supports_credentials=True` in `CORS(app, supports_credentials=True)`, you must specify exact origins instead of the wildcard '*'. Change `CORS(app)` or `CORS(app, origins='*')` to `CORS(app, origins=['http://localhost:3000'], supports_credentials=True)` (replace 'http://localhost:3000' with your actual frontend origin).