> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. # FastAPI Instrumentation > Learn about best practices for creating rich OpenAPI Specifications when instrumenting FastAPI applications. [FastAPI](https://fastapi.tiangolo.com/) is a popular Python web framework developed by [tiangolo](https://github.com/tiangolo). The offering brands itself as > FastAPI is a modern, fast (high-performance), web framework for building APIs with Python based on standard Python type hints. FastAPI plays very nicely with Fern because it has the power to output OpenAPI Specifications! Below we'll outline some tips for generating a rich OpenAPI with FastAPI. ## OpenAPI generation By default, FastAPI will generate an OpenAPI Specification for you based on your routes and your data models! You can access this spec by visiting `/docs` on your FastAPI server. If you are not seeing any OpenAPI Specification (or the Swagger UI), you may need to review your FastAPI server configuration as the path may have been changed, or completely omitted. ```python {6-8} from fastapi import FastAPI ... FastAPI( openapi_url="/openapi.json", # <-- this is the file and URL needed to access the OpenAPI Specification, `docs_url` and `redoc_url` are convenient wrappers that display the file in a UI! docs_url="/docs", # <-- this is the URL to access the Swagger UI, which will point to your OpenAPI Specification redoc_url="/redoc" # <-- this is the URL to access the ReDoc UI, which will point to your OpenAPI Specification ) ``` ## Specifying servers Fern will automatically generate clients that point to the servers you configure within your OpenAPI Specification, so it's important to specify the servers that your API will be hosted on. ```python {5} from fastapi import FastAPI ... app = FastAPI(servers=[{"url": "http://prod.test.com", "description": "Production server"}]) # This creates the following server object in your OpenAPI Specification: # "servers":[{"url":"http://prod.test.com","description":"Production server"}], ``` ## OpenAPI extensions FastAPI allows you to add in extra OpenAPI configuration directly within your route, through the use of the `openapi_extra` parameter. Below, we've annotated a "good" route within FastAPI that has it's typings as well as Fern extensions to assist in naming. ```python {5-9} @router.post( "/your/post/endpoint", response_model=YourResponseModel, # <-- with FastAPI, it is important to specify your response model so that it comes through to the OpenAPI Specification summary="Get some response for your req", # <-- if you'd like to add a description to your endpoint, you can do so here openapi_extra={ # <-- finally, you can add in your Fern extensions here, these extensions will produce SDK code that looks something like: `client.endpoints.create(...)` in python "x-fern-sdk-method-name": "create", "x-fern-sdk-group-name": "endpoints", "x-fern-availability": "beta", }, ) async def your_post_endpoint( payload: YourRequestModel, ) -> YourResponseModel: ``` ## Specifying examples FastAPI allows you to specify examples for your data models, which Fern will pick up and use within your generated SDKs and documentation automatically. For more information on leveraging examples within Fern, please refer to the [Fern documentation](/learn/api-definitions/openapi/extensions/request-response-examples). For more information on this FastAPI functionality, please refer to the [FastAPI documentation](https://fastapi.tiangolo.com/tutorial/schema-extra-example/). ```python {7-11} from pydantic import BaseModel class MyObject(BaseModel): id: str class Config: schema_extra = { "example": { "id": "a-cool-uuid", } } ``` ## Additional customization FastAPI has a lot of flexibility in how you can customize your OpenAPI Specification. Please refer to the [FastAPI documentation](https://fastapi.tiangolo.com/how-to/extending-openapi/#modify-the-openapi-schema) for more information. > Learn about best practices for creating rich OpenAPI Specifications when instrumenting FastAPI applications.