Skip to main content

FastAPI Config Patterns

Reusable Pydantic settings classes for FastAPI applications with common configuration patterns.

Extracted from: GridFlow backend/src/config.py

Installation

pip install fastapi-config-patterns

Features

  • BaseFastAPISettings: Common FastAPI app settings (debug, host, port, CORS)
  • BaseDatabaseSettings: Database connection configuration mixin
  • Validators: Reusable field validators (CORS origins parser)

Usage

Basic Settings

from fastapi_config_patterns import BaseFastAPISettings

class Settings(BaseFastAPISettings):
    # Add your app-specific settings
    app_name: str = "my-app"
    secret_key: str

settings = Settings()
print(settings.debug)  # False
print(settings.port)   # 8000

With Database

from fastapi_config_patterns import (
    BaseFastAPISettings,
    BaseDatabaseSettings
)

class Settings(BaseFastAPISettings, BaseDatabaseSettings):
    app_name: str = "my-app"

settings = Settings()
print(settings.database_url)  # postgresql+asyncpg://...

CORS Origins Validator

from pydantic import field_validator
from pydantic_settings import BaseSettings
from fastapi_config_patterns import assemble_cors_origins

class Settings(BaseSettings):
    allowed_origins: list[str]
    
    @field_validator("allowed_origins", mode="before")
    @classmethod
    def parse_origins(cls, v):
        return assemble_cors_origins(v)

# From environment variable: ALLOWED_ORIGINS="http://localhost:3000,http://localhost:8080"
settings = Settings()
print(settings.allowed_origins)  # ["http://localhost:3000", "http://localhost:8080"]

Environment Variables

The base classes support these environment variables:

# BaseFastAPISettings
DEBUG=true
ALLOWED_ORIGINS="http://localhost:3000,http://localhost:8080"

# BaseDatabaseSettings
DATABASE_URL="postgresql+asyncpg://user:pass@localhost:5432/db"

API Reference

BaseFastAPISettings

Base settings class with common FastAPI configuration.

Fields:

  • debug (bool): Debug mode (default: False, env: DEBUG)
  • api_v1_str (str): API v1 prefix (default: "/v1")
  • host (str): Server host (default: "0.0.0.0")
  • port (int): Server port (default: 8000)
  • allowed_origins (Union[str, List[str]]): CORS origins
  • cors_allow_credentials (bool): Allow credentials (default: False)
  • cors_max_age (int): Preflight cache seconds (default: 600)

BaseDatabaseSettings

Database connection configuration mixin.

Fields:

  • database_url (str): Database connection URL (env: DATABASE_URL)

Supported databases:

  • PostgreSQL: postgresql+asyncpg://user:pass@host:port/db
  • SQLite: sqlite+aiosqlite:///./app.db

assemble_cors_origins(v)

Parse CORS origins from string or list.

Args:

  • v (Union[str, List[str]]): Origins as string or list

Returns:

  • List[str]: Parsed list of origins

Handles:

  • Wildcard "*" for all origins
  • Comma-separated string
  • List of strings

Dependencies

  • pydantic>=2.0.0
  • pydantic-settings>=2.0.0

License

MIT

Metadata

Release files for fastapi-config-patterns 0.1.0

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-config-patterns 0.1.0
File Size Uploaded Provenance Actions
fastapi_config_patterns-0.1.0.tar.gz 4.2 kB Without provenance Details for fastapi_config_patterns-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-config-patterns 0.1.0
File Interpreter ABI Platform Actions
fastapi_config_patterns-0.1.0-py3-none-any.whl Python 3 none any Details for fastapi_config_patterns-0.1.0-py3-none-any.whl

Total release size: 9.2 kB

Release files / fastapi_config_patterns-0.1.0.tar.gz

Download URL fastapi_config_patterns-0.1.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1b536c79be15ecd21830ec74df42bfa88de6132001fc93fcc61611333d0abd97
BLAKE2b-256 checksum
How to use checksums
623f2d6ebe6ae89f13c1dfdfd9e25510cd482ab2e718d2da8fde98f8f7e19fc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.11

Release files / fastapi_config_patterns-0.1.0-py3-none-any.whl

Download URL fastapi_config_patterns-0.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b928dba0ca7ae7b3a3e6bb414bfade14e2ad40c366f72c2baff4e85c30b061e9
BLAKE2b-256 checksum
How to use checksums
e95fb70270212ea0bd67cc6b398508b2e38b520eb8e0f2a313960d191dd1fc92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.11

Security for fastapi-config-patterns 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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