Skip to main content

Simple env support of CORS settings for Fastapi applications

Project description

fastapi-cors

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

Usage

from fastapi import FastAPI

# import (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_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

See the FastAPI documentation on CORS for more information

Example Env

HOST=0.0.0.0
PORT=8000
LOG_LEVEL=info
ALLOW_ORIGINS=http://localhost,http://localhost:3000
ALLOWED_CREDENTIALS=True
ALLOWED_METHODS=["*"]
ALLOWED_HEADERS

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-cors-0.0.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

fastapi_cors-0.0.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-cors-0.0.2.tar.gz.

File metadata

  • Download URL: fastapi-cors-0.0.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.7.4 CPython/3.11.4

File hashes

Hashes for fastapi-cors-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a3e47ac8e902c24567d3424c1174b0e507d977164654839fefd1ad6cf8335e71
MD5 0be45a03bc9fec9ec37d8f543c3fc8c0
BLAKE2b-256 efb12de9cfe88746bd42342f963650dfcd37c86fb07367b813f2775880027e7e

See more details on using hashes here.

File details

Details for the file fastapi_cors-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_cors-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b42c530037fe38500da5e22138a08e08e30f94986a743042b14be572a779a66f
MD5 14328e55a42d92e825886b08bccb31f6
BLAKE2b-256 c8d82aaca65f94d121aaead7a291b6ccd8f5d5c7da42029677b2204dcfbaca0d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page