A lightweight and simple library for adding visual environment banners to FastAPI applications.
Inspired by django-env-notice, but specifically designed for FastAPI with a focus on simplicity and ease of integration.
See it in action
Visual environment indicators help prevent mistakes by clearly showing which environment you're working in
Features
- Simple and Lightweight: Just a few lines of code to integrate
- Visual and Intuitive: Colorful banners that clearly differentiate environments
- Highly Configurable: Customize colors, texts, and positions
- Swagger UI Support: Banner also appears in API documentation
- Zero Configuration: Works out-of-the-box with sensible defaults
- Secure: Banner disabled in production by default
Documentation
Full documentation is available at pinnlabs.github.io/fastapi-env-banner
Installation
pip install fastapi-env-banner
or if you are using uv:
uv add fastapi-env-banner
Basic Setup
from fastapi import FastAPI
from fastapi_env_banner import EnvBannerConfig, EnvBannerMiddleware, setup_swagger_ui
# Create FastAPI application
app = FastAPI(title="My API")
# Configure banner (auto-detects from ENVIRONMENT variable)
config = EnvBannerConfig.from_env()
# Add middleware
app.add_middleware(EnvBannerMiddleware, config=config)
# Setup Swagger UI with banner
setup_swagger_ui(app, config)
@app.get("/")
async def root():
return {"message": "Hello World"}
Manual Configuration
from fastapi_env_banner import Environment, EnvBannerConfig
# Custom configuration
config = EnvBannerConfig(
environment=Environment.STAGING,
enabled=True,
custom_text="STAGING ENVIRONMENT",
custom_color="#FF6B6B",
position="top", # or "bottom"
show_in_swagger=True
)
Supported Environments
The library supports the following environments with pre-defined colors:
| Environment | Default Color | Default Text |
|---|---|---|
| LOCAL | Green (#4CAF50) |
LOCAL ENVIRONMENT |
| DEVELOPMENT | Blue (#2196F3) |
DEVELOPMENT ENVIRONMENT |
| STAGING | Orange (#FF9800) |
STAGING ENVIRONMENT |
| PRODUCTION | Red (#F44336) |
PRODUCTION ENVIRONMENT |
| TESTING | Purple (#9C27B0) |
TESTING ENVIRONMENT |
Detailed Configuration
Using Environment Variables
Set the ENVIRONMENT variable (or another of your choice):
export ENVIRONMENT=staging
Accepted values:
localdev,developmentstage,stagingprod,productiontest,testing
Configuration Parameters
EnvBannerConfig(
environment: Environment = Environment.LOCAL,
enabled: bool = True,
custom_text: Optional[str] = None,
custom_color: Optional[str] = None,
position: str = "top", # "top" or "bottom"
show_in_swagger: bool = True
)
Examples
Example 1: Minimal Configuration
from fastapi import FastAPI
from fastapi_env_banner import EnvBannerConfig, EnvBannerMiddleware
app = FastAPI()
app.add_middleware(EnvBannerMiddleware, config=EnvBannerConfig.from_env())
Example 2: Full Customization
from fastapi import FastAPI
from fastapi_env_banner import (
Environment,
EnvBannerConfig,
EnvBannerMiddleware,
setup_swagger_ui
)
app = FastAPI(title="Production API")
config = EnvBannerConfig(
environment=Environment.STAGING,
custom_text="⚠️ TEST ENVIRONMENT - DO NOT USE IN PRODUCTION ⚠️",
custom_color="#E91E63",
position="bottom",
show_in_swagger=True
)
app.add_middleware(EnvBannerMiddleware, config=config)
setup_swagger_ui(app, config)
Example 3: Disable in Production
import os
from fastapi import FastAPI
from fastapi_env_banner import EnvBannerConfig, EnvBannerMiddleware
app = FastAPI()
# Completely disable in production
is_production = os.getenv("ENVIRONMENT") == "production"
config = EnvBannerConfig.from_env()
config.enabled = not is_production
app.add_middleware(EnvBannerMiddleware, config=config)
Security
By default, the banner is NOT displayed in production environment as a security measure. This prevents environment information from being accidentally exposed.
To force display in production (not recommended):
config = EnvBannerConfig(
environment=Environment.PRODUCTION,
enabled=True # Force display
)
How It Works
- Middleware: Intercepts HTML responses and automatically injects the banner
- Templates: Clean, pythonic HTML generation using string templates
- Swagger UI: Customizes the documentation page to include the banner
- Zero Impact: Does not affect JSON APIs or other non-HTML responses
- Performance: Minimal overhead, only processes HTML responses
Contributing
Contributions are welcome! Feel free to:
- Fork the project
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by django-env-notice
- Built with FastAPI
- Developed by PinnLabs
Support
If you encounter any issues or have suggestions, please open an issue.
Made with ❤️ by PinnLabs
Metadata
Release files for fastapi-env-banner 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_env_banner-0.1.0.tar.gz | 566.1 kB | Without provenance | Details for fastapi_env_banner-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_env_banner-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_env_banner-0.1.0-py3-none-any.whl |
Total release size: 575.1 kB
Security for fastapi-env-banner 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.