Skip to main content

FastAPI Maintenance

Flexible maintenance mode middleware for FastAPI applications.

Package version Supported Python versions Test Coverage License


Documentation: https://msamsami.github.io/fastapi-maintenance

Source Code: https://github.com/msamsami/fastapi-maintenance


FastAPI Maintenance is a lightweight middleware for FastAPI applications that provides a flexible way to handle maintenance mode.

The package provides a simple yet powerful solution to temporarily disable your API endpoints during application maintenance states. It ensures a smooth experience for API consumers through customizable responses and fine-grained control over which routes remain accessible. The package is designed to be easy to integrate, highly customizable, and extensible to fit various use cases.

[!WARNING] FastAPI Maintenance is currently in experimental status. Although it's actively developed and tested, the API may undergo changes between releases. Be cautious when upgrading in production environments and always review the changelog carefully.

Features

  • ⚙️ Simple to use: Add just a few lines of code to enable maintenance mode.
  • 🔌 Flexible state management: Manage maintenance mode via environment variables, local files, or create your own custom backend.
  • 🚦 Per-route control: Force maintenance mode ON/OFF for specific routes.
  • 🎨 Customizable responses: Define your own maintenance page or custom JSON responses.
  • ⏱️ Context manager: Temporarily enable maintenance mode for critical operations.
  • 🧩 Extensible: Easy to extend with custom backends, handlers, and exemptions.

Install

pip install fastapi-maintenance

Quick Start

Middleware

Add the maintenance mode middleware to your FastAPI application:

from fastapi import FastAPI
from fastapi_maintenance import MaintenanceModeMiddleware

app = FastAPI()

# Add the middleware to your FastAPI application
app.add_middleware(MaintenanceModeMiddleware)

@app.get("/")
def root():
    return {"message": "Hello World"}

When maintenance mode is not active, endpoints function normally.

Enabling Maintenance Mode

You can enable maintenance mode in several ways:

1. Direct Configuration

Explicitly enable maintenance mode when adding the middleware:

app.add_middleware(MaintenanceModeMiddleware, enable_maintenance=True)

2. Environment Variables

Set the FASTAPI_MAINTENANCE_MODE environment variable before starting your application:

export FASTAPI_MAINTENANCE_MODE=1
uvicorn main:app

Maintenance Response

When maintenance mode is active, all endpoints (unless explicitly exempted) will return a 503 Service Unavailable response:

{"detail":"Service temporarily unavailable due to maintenance"}

Decorators

You can control maintenance mode behavior for specific routes using decorators:

from fastapi import FastAPI
from fastapi_maintenance import (
    MaintenanceModeMiddleware,
    force_maintenance_mode_off,
    force_maintenance_mode_on,
)

app = FastAPI()
app.add_middleware(MaintenanceModeMiddleware)

# Always accessible, even during maintenance
@app.get("/status")
@force_maintenance_mode_off
def status():
    return {"status": "operational"}

# Always returns maintenance response
@app.get("/deprecated")
@force_maintenance_mode_on
async def deprecated_endpoint():
    return {"message": "This endpoint is deprecated"}

The force_maintenance_mode_off decorator keeps an endpoint accessible even when maintenance mode is enabled globally. Conversely, the force_maintenance_mode_on decorator forces an endpoint to always return the maintenance response, regardless of the global maintenance state.

Context Manager

You can use context managers to temporarily enforce the maintenance state for specific operations:

from fastapi import FastAPI
from fastapi_maintenance import (
    MaintenanceModeMiddleware,
    maintenance_mode_off,
    maintenance_mode_on,
)

app = FastAPI()
app.add_middleware(MaintenanceModeMiddleware)

@app.post("/sync")
async def sync_data():
    # Enable maintenance mode during data sync
    async with maintenance_mode_on():
        # Data sync logic here
        await perform_sync()

    # Maintenance mode is automatically disabled after the block
    return {"status": "completed"}

@app.get("/health")
def health_check():
    return {"status": "healthy"}

The maintenance_mode_on context manager temporarily enables maintenance mode for critical operations.

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for fastapi-maintenance 0.0.5

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-maintenance 0.0.5
File Size Uploaded Provenance
fastapi_maintenance-0.0.5.tar.gz 189.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-maintenance 0.0.5
File Interpreter ABI Platform
fastapi_maintenance-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 204.8 kB

Release files / fastapi_maintenance-0.0.5.tar.gz

Download URL fastapi_maintenance-0.0.5.tar.gz
Size 189.4 kB
Tags Source
SHA-256 checksum
How to use checksums
054305d8bc27d37cdfddb4ff010d8a33bcdc8156b47846119848cdbcd1eb9030
BLAKE2b-256 checksum
How to use checksums
bb4af6cd3ad0ba0d28a17a8d580fce1b9d2a1987b65020d63e3b56ba03c156a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / fastapi_maintenance-0.0.5-py3-none-any.whl

Download URL fastapi_maintenance-0.0.5-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6672c8ac38226480175f738118d25fe12027f016ef130d435af17e4e5c55fc43
BLAKE2b-256 checksum
How to use checksums
72d1800560ebcffee255b90143961bd1673ef3b5e3afaf8e50a8efeb8e84cf47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Security for fastapi-maintenance 0.0.5

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.

Release history Release notifications | RSS feed

This release

0.0.5 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