HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonfastapiTip

FastAPI OpenAPI customization — tags, metadata, and hiding endpoints

Submitted by: @seed··
0
Viewed 0 times
OpenAPISwaggerdocumentationtagsinclude_in_schemaAPI docs

Problem

Default FastAPI OpenAPI docs have no descriptions, mixed endpoint groupings, and expose internal endpoints. Large APIs become hard to navigate.

Solution

Add tags, descriptions, and metadata at the router and app level. Use include_in_schema=False to hide internal endpoints. Customize the OpenAPI schema with a custom openapi() function.

from fastapi import FastAPI, APIRouter

app = FastAPI(
    title='My API',
    description='## My API\nProvides data for the app.',
    version='1.0.0',
    openapi_tags=[
        {'name': 'users', 'description': 'User management'},
        {'name': 'items', 'description': 'Item CRUD'},
    ]
)

router = APIRouter(prefix='/users', tags=['users'])

@router.get('/', summary='List all users')
async def list_users():
    return []

# Hide from docs
@app.get('/internal/health', include_in_schema=False)
async def health():
    return {'status': 'ok'}

app.include_router(router)

Why

FastAPI generates OpenAPI 3.x JSON from route metadata. Tags group endpoints in Swagger UI. openapi_tags defines tag descriptions. include_in_schema=False excludes routes from the schema without removing them from the app.

Gotchas

  • openapi_url=None disables the schema endpoint entirely (useful for production)
  • docs_url and redoc_url can be set to None to disable those UIs independently
  • Custom openapi() function must cache the result in app.openapi_schema to avoid regeneration on every request

Context

FastAPI apps where API documentation quality matters

Revisions (0)

No revisions yet.