When you run a FastAPI application with the openapi_url set to "/openapi.json", you can access the OpenAPI schema definition of your API by visiting that URL in your browser or using a tool like curl.
In this example, I will demonstrate how to generate and retrieve the OpenAPI schema using the previously provided FastAPI application:
Example FastAPI Application
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(openapi_url="/openapi.json") # Custom OpenAPI URL
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/items/", response_model=Item)
def create_item(item: Item):
return item
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
Accessing /openapi.json
-
Run the FastAPI Application:
Start the FastAPI application by saving the code above into a file (let's say called main.py) and run it using uvicorn:
uvicorn main:app --reload
The application should start and you will see output indicating it’s running on http://127.0.0.1:8000.
-
Retrieve OpenAPI JSON:
Open your web browser and go to the URL:
http://127.0.0.1:8000/openapi.json
You will see something like the following JSON structure, which is the OpenAPI schema for your FastAPI application:
Sample Output for /openapi.json
{
"openapi": "3.0.2",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/": {
"get": {
"summary": "Read Root",
"operationId": "read_root__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"example": {
"Hello": "World"
}
}
}
}
}
}
},
"/items/": {
"post": {
"summary": "Create Item",
"operationId": "create_item_item_post",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Item"
}
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Item"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Item": {
"title": "Item",
"required": [
"name",
"price"
],
"type": "object",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"description": {
"title": "Description",
"type": "string"
},
"price": {
"title": "Price",
"type": "number"
},
"tax": {
"title": "Tax",
"type": "number"
}
}
}
}
}
}
Explanation of the OpenAPI JSON Output
- openapi: The OpenAPI version being used (3.0.2 in this case).
- info: Contains metadata about the API, such as the title and version.
- paths: Contains the endpoints available in your API. Each endpoint has information about which HTTP methods are supported (e.g., GET for
/ and POST for /items/), along with a summary and response descriptions.
- components: This section contains reusable schemas that are referenced in the paths section. For instance, the
Item schema defines the properties expected in the JSON body when creating an item.
This schema can be used to generate API documentation and client libraries, as well as for validation and development purposes. You can also check the schema's structure against open-source tools like Swagger UI which can consume OpenAPI specs for interactive API documentation. FastAPI provides a built-in user interface for this at /docs.