Environment variables for FastAPI configuration
Environment variables (env vars) are values that live outside of Python code in the operating system and can be read by FastAPI applications and other programs. FastAPI applications commonly use environment variables for configuration such as database URLs, email credentials, and secret keys.
Settings and Environment Variables guide location
Configuration guidance for using environment variables in FastAPI is found in the 'Settings and Environment Variables' section under advanced topics.
fastapi dev command for development
The `fastapi dev` command runs your FastAPI app in development mode. It enables auto-reload by default, which automatically restarts the server when you make code changes. It listens on 127.0.0.1:8000.
fastapi dev with --entrypoint CLI option
You can pass the `--entrypoint` option to the `fastapi dev` command to specify the app location on the command line. Example: `uv run fastapi dev --entrypoint main:app`. However, using `pyproject.toml` is recommended so other tools like the VS Code Extension can find your app.
FASTAPI_ENV environment variable in development
When `fastapi dev` runs, it sets the `FASTAPI_ENV` environment variable to `development` before importing your app, unless it is already set. This allows app startup code to choose development-friendly behavior while permitting app-specific environments like `staging`.
FASTAPI_ENV in production mode
The `fastapi run` command currently leaves `FASTAPI_ENV` unchanged. If your app needs to detect production mode, you should set `FASTAPI_ENV` explicitly.
Conventional FASTAPI_ENV values
The conventional values for `FASTAPI_ENV` are `development` and `production`.
Auto-reload is resource-intensive in development
Auto-reload in `fastapi dev` is enabled by default but is resource-intensive and could be less stable than when disabled. It should only be used for development.
Production deployment requires termination proxy for HTTPS
In most production deployments, you should have a termination proxy handling HTTPS on top of your FastAPI application. This depends on how you deploy your application; your provider might handle it, or you might need to set it up yourself.
FastAPI CLI comes with fastapi[standard] package
When you add FastAPI to your project using `uv add "fastapi[standard]"` or similar, it includes a command line interface program you can run in the terminal.
FastAPI sensible defaults and optional configuration
FastAPI provides sensible defaults for everything with optional configurations available everywhere. All parameters can be fine-tuned to define the required API, but by default everything works without additional configuration.
Conditional OpenAPI using environment variables and settings
You can use Pydantic settings with environment variables to conditionally configure OpenAPI. Declare an openapi_url setting with a default value of '/openapi.json', then pass this setting to the FastAPI constructor. Set the environment variable OPENAPI_URL to an empty string to disable OpenAPI in specific environments like production.
Disabling OpenAPI via openapi_url parameter
You can disable OpenAPI documentation and the UI docs by setting the FastAPI app's openapi_url parameter to an empty string or None. When openapi_url is disabled, requests to /openapi.json, /docs, and /redoc endpoints will return 404 Not Found errors.
Disable syntax highlighting in Swagger UI
Set the key 'syntaxHighlight' to False in the swagger_ui_parameters dictionary to disable syntax highlighting in Swagger UI. By default, syntax highlighting is enabled.
Change syntax highlighting theme in Swagger UI
Use the key 'syntaxHighlight.theme' (note the dot in the middle) in the swagger_ui_parameters dictionary to change the syntax highlighting color theme in Swagger UI.
FastAPI default Swagger UI parameters
FastAPI includes default Swagger UI configuration parameters that are appropriate for most use cases. These default parameters can be overridden by passing different values in the swagger_ui_parameters argument. The default configurations are stored in fastapi/openapi/docs.py.
Disable deepLinking in Swagger UI
Set the key 'deepLinking' to False in the swagger_ui_parameters dictionary to disable deep linking in Swagger UI. This is an example of overriding a default FastAPI Swagger UI parameter.
JavaScript-only Swagger UI settings limitation
Swagger UI allows JavaScript-only configurations such as JavaScript functions that cannot be passed directly from Python code because they are JavaScript objects, not strings. To use JavaScript-only configurations like presets, you must override the entire Swagger UI path operation and manually write the required JavaScript.
Default Swagger UI presets in FastAPI
FastAPI includes these default JavaScript presets for Swagger UI: SwaggerUIBundle.presets.apis and SwaggerUIBundle.SwaggerUIStandalonePreset. These are JavaScript objects and cannot be configured from Python code directly.
get_swagger_ui_html function accepts swagger_ui_parameters
The swagger_ui_parameters argument can be passed when calling the get_swagger_ui_html() function to configure Swagger UI parameters.
swagger_ui_parameters argument for FastAPI app configuration
The FastAPI() app object accepts a swagger_ui_parameters argument that receives a dictionary with Swagger UI configurations. These configurations are passed to Swagger UI directly. FastAPI converts the configurations to JSON to make them compatible with JavaScript, as that is what Swagger UI needs.
Self-hosted docs enables offline access
By downloading and self-hosting the documentation static files (JavaScript and CSS) for Swagger UI and ReDoc through your FastAPI app, you can serve the documentation without requiring external CDN access. This allows the API docs to work offline or in environments without open Internet access.
Disable automatic docs by setting URLs to None
To disable the automatic API documentation that uses CDN by default, set the documentation URLs to None when creating the FastAPI app. This is done by passing docs_url=None, redoc_url=None, and openapi_url=None to the FastAPI constructor.
FastAPI internal functions for custom docs HTML
FastAPI provides internal functions to create custom HTML pages for documentation. When creating custom doc paths, you can reuse these functions and pass arguments like openapi_url, title, oauth2_redirect_url, swagger_js_url, swagger_css_url, and redoc_js_url to customize the documentation pages.
Custom CDN URLs for Swagger UI and ReDoc
For custom documentation serving, pass swagger_js_url and swagger_css_url parameters to specify where Swagger UI should load its JavaScript and CSS files. Similarly, pass redoc_js_url for ReDoc. These can point to a custom CDN or to your own app serving static files.
app.openapi_url attribute for docs configuration
When configuring custom docs paths, use the app.openapi_url attribute to specify where the documentation HTML page should fetch your API's OpenAPI schema.
Static files for Swagger UI and ReDoc
Swagger UI requires two static files: swagger-ui-bundle.js and swagger-ui.css. ReDoc requires one static file: redoc.standalone.js. These files can be downloaded from CDNs and self-hosted by mounting them with StaticFiles.
Mount StaticFiles for serving docs assets
To serve static documentation files from your FastAPI app, import StaticFiles and mount a StaticFiles() instance at a specific path using app.mount(). This allows serving downloaded JavaScript and CSS files for offline documentation access.
separate_input_output_schemas parameter in FastAPI
FastAPI supports a parameter named `separate_input_output_schemas` that can be set to False to disable the generation of separate schemas for input and output. When set to False, the same schema will be used for both input and output. This parameter was added in FastAPI 0.102.0.
FastAPI Pydantic v1 support timeline
FastAPI version 0.100.0 supported either Pydantic v1 or v2. FastAPI 0.119.0 introduced partial support for Pydantic v1 from inside Pydantic v2 (as pydantic.v1). FastAPI 0.126.0 dropped support for Pydantic v1 while still supporting pydantic.v1. FastAPI 0.128.0 dropped support for pydantic.v1 entirely. Current versions of FastAPI require Pydantic v2 and using pydantic.v1 models will raise an error.
Python 3.14 and Pydantic v1 support
The Pydantic team stopped support for Pydantic v1 starting with Python 3.14. This includes pydantic.v1, which is no longer supported in Python 3.14 and above. To use the latest features of Python, you must use Pydantic v2.
Pydantic v1 and v2 nesting restrictions
It is not supported by Pydantic to have a Pydantic v2 model with fields defined as Pydantic v1 models, or a Pydantic v1 model with fields defined as Pydantic v2 models. However, you can have separate models in the same app where some use Pydantic v1 and others use Pydantic v2, and it is possible to have both Pydantic v1 and v2 models in the same path operation.
Pydantic v1 parameters migration import
When using FastAPI-specific parameter tools like Body, Query, Form, etc. with Pydantic v1 models during migration, you can import them from fastapi.temp_pydantic_v1_params while finishing the migration to Pydantic v2.
Gradual Pydantic v1 to v2 migration support timeline
Gradual migration using both Pydantic v1 and v2 models in the same app was only supported in FastAPI versions 0.119.0 to 0.127.x. It was removed in FastAPI 0.128.0. The latest versions of FastAPI require Pydantic v2 models exclusively.
bump-pydantic tool for migration
The bump-pydantic tool from the Pydantic team can automate most of the migration process from Pydantic v1 to v2 when using regular Pydantic models without customizations. After running bump-pydantic, you should run tests to verify everything works correctly.
FastAPI installation command
Install FastAPI using `uv add "fastapi[standard]"`. Quotes are required to ensure compatibility across all terminals. As an alternative, use `pip` to install `fastapi[standard]` inside a virtual environment.
FastAPI dev server command
Run `fastapi dev` to start the development server. This command automatically reads main.py, detects the FastAPI app, and starts Uvicorn with auto-reload enabled. The default server URL is http://127.0.0.1:8000. For production, use `fastapi run` instead.
FastAPI standard installation dependencies
When installed with `uv add "fastapi[standard]"`, FastAPI includes: email-validator (Pydantic); httpx (required for TestClient); jinja2 (for default template configuration); python-multipart (for form parsing); uvicorn with uvloop (server); fastapi-cli[standard] (CLI commands including fastapi-cloud-cli for deployment).
FastAPI without standard dependencies
Install without standard optional dependencies using `uv add fastapi` instead of `uv add "fastapi[standard]"`.
FastAPI without fastapi-cloud-cli dependency
Install FastAPI with standard dependencies but without fastapi-cloud-cli using `uv add "fastapi[standard-no-fastapi-cloud-cli]"`.
Optional Pydantic additional dependencies
Additional optional Pydantic packages: pydantic-settings for settings management; pydantic-extra-types for extra types.
Optional FastAPI additional dependencies
Additional optional FastAPI packages: orjson for using ORJSONResponse; ujson for using UJSONResponse.
FastAPI Cloud deployment
Deploy FastAPI apps to FastAPI Cloud using `fastapi deploy`. The CLI automatically detects the FastAPI application and deploys it. If not logged in, the browser opens for authentication. FastAPI Cloud is built by the same author and team behind FastAPI and streamlines building, deploying, and accessing APIs.
FastAPI open source and cloud provider flexibility
FastAPI is open source and based on standards. It can be deployed to any cloud provider. Each cloud provider has its own deployment guides for FastAPI applications.