Skip to main content

A lightweight and simple library for adding visual environment banners to FastAPI applications

Project description

FastAPI Environment Banner

FastAPI Environment Banner Logo

PyPI version Python Versions License: MIT Documentation

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

FastAPI Environment Banner Demo

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:

  • local
  • dev, development
  • stage, staging
  • prod, production
  • test, 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

  1. Middleware: Intercepts HTML responses and automatically injects the banner
  2. Templates: Clean, pythonic HTML generation using string templates
  3. Swagger UI: Customizes the documentation page to include the banner
  4. Zero Impact: Does not affect JSON APIs or other non-HTML responses
  5. Performance: Minimal overhead, only processes HTML responses

Contributing

Contributions are welcome! Feel free to:

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Support

If you encounter any issues or have suggestions, please open an issue.


Made with ❤️ by PinnLabs

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_env_banner-0.1.0.tar.gz (566.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_env_banner-0.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_env_banner-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_env_banner-0.1.0.tar.gz
  • Upload date:
  • Size: 566.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for fastapi_env_banner-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c90fd969c93a66178bc9550c9cc02800635d9c0458ea0af470a185148c52e9d6
MD5 b150785911392e1b2b84d89764ed2698
BLAKE2b-256 a3492ab14d6ae9b74cea94919209b328d79c4df32bcf858a4c674b458d4b2214

See more details on using hashes here.

File details

Details for the file fastapi_env_banner-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_env_banner-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2eaf582dbf3d0725e4bd5342c17608391e5eea4941cbfbf77910747ce65aea3d
MD5 53ebdca44e95196d5d273dd908dd1366
BLAKE2b-256 7ea86ef262e2fd04037f92887ee41b781dbd7c284b8b8380c2a30e95491bcac7

See more details on using hashes here.

Supported by

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