OpenAPI schema customization lets you change how your API's description looks. This helps make your API easier to understand and use.
OpenAPI schema customization in FastAPI
from fastapi import FastAPI app = FastAPI( title="My API", description="This API does cool things.", version="1.0.0", contact={"name": "Support", "email": "support@example.com"}, license_info={"name": "MIT"}, openapi_url="/custom_openapi.json", docs_url="/custom_docs" )
You customize the schema by passing parameters when creating the FastAPI app.
openapi_url changes the JSON schema URL; docs_url changes the interactive docs URL.
app = FastAPI(title="Book Store API", version="2.0")
app = FastAPI(
description="API for managing tasks.",
contact={"name": "Jane Doe", "email": "jane@example.com"}
)app = FastAPI(
openapi_url="/api/schema.json",
docs_url="/api/docs"
)This example shows how to set title, description, version, contact, and license info. It also changes the OpenAPI and docs URLs. The endpoint '/hello' appears in docs with a tag. The '/hidden' endpoint is not shown in the docs.
from fastapi import FastAPI from fastapi.responses import JSONResponse app = FastAPI( title="Simple API", description="This API shows how to customize OpenAPI schema.", version="0.1", contact={"name": "API Support", "email": "support@simpleapi.com"}, license_info={"name": "Apache 2.0"}, openapi_url="/openapi.json", docs_url="/docs" ) @app.get("/hello", tags=["Greetings"]) async def say_hello(): return {"message": "Hello, world!"} @app.get("/hidden", include_in_schema=False) async def hidden_endpoint(): return JSONResponse({"secret": "This is hidden from docs"})
Use include_in_schema=False to hide endpoints from docs.
Tags help organize endpoints in the docs.
Changing openapi_url and docs_url helps if you want custom paths for your API docs.
Customize your API docs by setting parameters in FastAPI app creation.
You can add info like title, version, description, contact, and license.
You can hide endpoints or change where docs appear.
Practice
Solution
Step 1: Understand OpenAPI schema role
The OpenAPI schema defines how the API is documented and described for users and tools.Step 2: Identify customization benefits
Customizing it lets you add titles, descriptions, versions, and contact info to make docs clearer.Final Answer:
To provide clear API documentation with custom titles, descriptions, and versions -> Option AQuick Check:
OpenAPI schema customization = clear docs [OK]
- Confusing schema customization with performance tuning
- Thinking it changes database or backend logic
- Assuming it auto-generates frontend code
Solution
Step 1: Recall FastAPI app parameters
FastAPI accepts title and version as direct parameters in its constructor.Step 2: Match correct parameter names
The correct parameters are exactly title and version, not schema_title or openapi_title.Final Answer:
app = FastAPI(title="My API", version="1.0.0") -> Option CQuick Check:
FastAPI(title, version) = correct syntax [OK]
- Using incorrect parameter names like openapi_title
- Trying to set schema properties after app creation
- Confusing app config with route decorators
from fastapi import FastAPI
app = FastAPI(title="Test API")
@app.get("/items/")
async def read_items():
return [{"item_id": "foo"}]Solution
Step 1: Recall FastAPI default docs path
FastAPI by default serves Swagger UI at the path /docs.Step 2: Differentiate docs and schema URLs
/openapi.json is the raw schema JSON, not the interactive docs UI.Final Answer:
/docs -> Option DQuick Check:
Default docs URL = /docs [OK]
- Confusing /openapi.json with docs UI
- Assuming /swagger or /api-docs are default
- Not knowing FastAPI defaults for docs
app = FastAPI(docs_url=None)
@app.get("/users")
async def get_users():
return [{"name": "Alice"}]Solution
Step 1: Understand docs_url and redoc_url
FastAPI has two docs UIs: Swagger UI at docs_url and ReDoc at redoc_url.Step 2: Check hiding docs UI fully
Setting docs_url=None hides Swagger UI but ReDoc remains unless redoc_url=None is also set.Final Answer:
You must also set redoc_url=None to hide all docs -> Option BQuick Check:
Hide all docs = docs_url=None + redoc_url=None [OK]
- Assuming docs_url=None hides all docs
- Using invalid values like False for docs_url
- Not knowing about ReDoc docs URL
Solution
Step 1: Recall FastAPI parameters for contact and license
FastAPI uses contact and license_info parameters to customize OpenAPI schema contact and license sections.Step 2: Match correct parameter names and structure
contact and license_info expect dictionaries with keys like email and name respectively.Final Answer:
app = FastAPI(contact={"email": "support@example.com"}, license_info={"name": "MIT"}) -> Option AQuick Check:
Use contact and license_info dicts in FastAPI() [OK]
- Using openapi_contact or openapi_license which are invalid
- Trying to nest contact inside openapi_info
- Passing strings instead of dicts for contact/license
Ready to prove it?
Pass the test and this pattern moves unknown
