Skip to main content

fastapi-cors

A simply scoped abstraction to provide CORS settings via environment variables to a Fastapi application.

Usage

from fastapi import FastAPI

# during this next line `fastapi_cors.env` will read environment variables from .env
from fastapi_cors import CORS 

app = FastAPI()

CORS(app)

Opinions

A health check route is optionally added that displays these (but not other) environment variables.

If you want to disable it, use the code below

from fastapi import FastAPI

# during this next line `fastapi_cors.env` will read environment variables from .env
from fastapi_cors import CORS 

app = FastAPI()

CORS(app, include_health_check=False)

Config

Configure FastAPI as usual. Extra arguments (that can be accessed from app.extra):

Name Default Description
HOST 0.0.0.0 Displayed in the Swagger title, with app.title.
PORT 8000 Where to mount the static directory. Disabled if value is falsy.
LOG_LEVEL info log level.
ALLOW_ORIGINS ["http://localhost","http://localhost:3000"] A list of origins that should be permitted to make cross-origin requests. E.g. ['https://example.org', 'https://www.example.org']. You can use ['*'] to allow any origin. These are the URLs clients can make requests from
ALLOWED_CREDENTIALS True Indicate that cookies should be supported for cross-origin requests. Also, allow_origins cannot be set to ['*'] for credentials to be allowed, origins must be specified.
ALLOWED_METHODS ["*"] A list of HTTP methods that should be allowed for cross-origin requests. Defaults to ['*'] to allow all standard methods. You can use ['GET'] to reduce the list.
ALLOWED_HEADERS ["Access-Control-Allow-Origin"] A list of HTTP request headers that should be supported for cross-origin requests. You can use ['*'] to allow all headers. The Accept, Accept-Language, Content-Language and Content-Type headers are always allowed for simple CORS requests

⚠️ allow_origins default is ["http://localhost","http://localhost:3000"], not [] (80 -> docs, 3000 -> frontend)

⚠️ allow_methods default is ["*"], not ["GET"]

⚠️ allowed_credentials default is True, not False

See the FastAPI documentation on CORS for more information

Example Env

Values will be cast into a list of str, as appropriate.

HOST=0.0.0.0
PORT=8000
LOG_LEVEL=info
ALLOW_ORIGINS=http://localhost,http://localhost:3000
ALLOWED_CREDENTIALS=True
ALLOWED_METHODS=*
ALLOWED_HEADERS=Access-Control-Allow-Origin

Note, this is not required unless you are changing a default or want to declare them all

Release files for fastapi-cors 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-cors 0.0.6
File Size Uploaded
fastapi-cors-0.0.6.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-cors 0.0.6
File Interpreter ABI Platform
fastapi_cors-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / fastapi-cors-0.0.6.tar.gz

Download URL fastapi-cors-0.0.6.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
17eeb92f2b3dd8f0deb8ca69f031760e35edd5a0c811aaf80f9743e5e6ae50a1
BLAKE2b-256 checksum
How to use checksums
224b33e263c78cc50b6477319b1403a3ae8f9139b4272411a415267104e145a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.7.4 CPython/3.11.4

Release files / fastapi_cors-0.0.6-py3-none-any.whl

Download URL fastapi_cors-0.0.6-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d116b482c682f9c5330f04b1c49a9d504f3a9df6373bc43dd6c31f3b9d0b8b15
BLAKE2b-256 checksum
How to use checksums
54ad016bdc85603cca123527167cb3e3ca408f639f3ad71b8d51fd50b116b85e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.7.4 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page