Skip to main content
In FastAPI, simplifying route definitions using router prefixes and tags not only reduces code redundancy but also improves the clarity of your automatically generated API documentation. In this guide, we explain how to implement router prefixes and tags to group endpoints logically, making your API more maintainable and easier to understand.

Defining Routes Without a Prefix

Consider the following example where routes are defined without a common prefix. Both GET and POST operations use the “/posts” path:
Example log entries for these routes:
For operations that target a specific post (such as GET, DELETE, or PUT), the URL path generally includes the post’s ID. Consider this snippet for deleting a post:
And the corresponding log entries might look like this:
For simple APIs, repeating route strings might not seem problematic. However, as applications grow in complexity, consolidating routes with common prefixes minimizes errors and simplifies maintenance.

Simplifying with Router Prefix

FastAPI enables you to define a common prefix once for all routes within a router. Instead of hardcoding “/posts” in each route, you can specify it when creating the APIRouter. This approach cleans up the code significantly. Here’s an updated version of the posts routes using a prefix:
The log entries for these operations now update accordingly:
When targeting a specific post (for example, /posts/{id}), only the relative path needs to be specified:
And the resulting logs:
Using a router prefix improves code clarity and maintainability, as any changes to the prefix are made in a single location. Organizing routes into logical groups is essential for scaling your API. For instance, you can group user-related endpoints under a /users prefix while applying a tag to clearly separate them in your documentation. Below is an example of a users router with a defined prefix and tag:
Example log entries for user operations:

Testing and Verifying the Implementation

After refining your routes with prefixes and tags, test your endpoints to ensure everything works as expected. For example, a GET request to /posts should return a list of posts:
Similarly, creating a new user with a unique email should yield a proper response:

Leveraging Automatic Documentation with Swagger UI

One of FastAPI’s most powerful features is its automatic generation of interactive API documentation with Swagger UI. Navigate to /docs in your browser to explore and test your endpoints. Organizing routes with prefixes and tags further enhances the documentation by grouping related endpoints together.
The image shows a FastAPI Swagger UI interface displaying various API endpoints for managing posts and users, including GET, POST, PUT, and DELETE methods.
The Swagger UI documentation is generated dynamically by FastAPI. While a static image provides a visual reference, the interactive docs offer comprehensive details about every endpoint.

Final Version Example

Below is a concise example of the final posts router implementation with a prefix and tag:
And sample logs for these endpoints:
In summary, using router prefixes and tags in FastAPI streamlines your code, reduces redundancy, and enhances the automatic documentation. This results in an API that is easier to maintain and more intuitive for both users and developers.

Watch Video