Flask-CORS

6.0.2 · active · verified Sat Mar 28

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.

Warnings

Install

Imports

Quickstart

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.

from flask import Flask
from flask_cors import CORS
import os

app = Flask(__name__)
CORS(app) # Enable CORS for all routes, for all origins and methods

@app.route("/")
def hello_world():
    return "Hello, cross-origin-world!"

# Example of specific CORS for an API endpoint
@app.route("/api/data")
@cross_origin(origins="http://localhost:3000", methods=["GET", "POST"], supports_credentials=True)
def get_data():
    return {"message": "Data from API!"}

if __name__ == '__main__':
    app.run(debug=True, port=int(os.environ.get('PORT', 5000)))

view raw JSON →