FastAPI: How to Disable the Docs (Swagger UI and ReDoc)

Updated: December 9, 2022 By: Frienzied Flame Post a comment

FastAPI is an open-source, lightweight, high-performance, and secure web framework for building backend APIs. Despite its young age, FastAPI has been well received by the community and in fact, many large companies are already using it for their platforms.

By default, FastAPI serves the two documentation user interfaces, including Swagger UI (at /docs) and ReDoc (at /redoc).

FastAPI Docs UI

These things are very useful during the development of your project, assisting team members in understanding the structure, function, and purpose of routes. However, in production, they should be disabled (unless you want to create a public API for developers) for the following reasons:

  • They are not needed by end users as they interact with your project through user-friendly interfaces designed specifically for web or mobile applications
  • They can make it easy for suspicious people to exploit or abuse your API

You can easily remove the interactive docs when initializing your app. To turn off the docs, just set docs_url=None, and to disable the ReDoc, set redoc_url=None.

Let’s see the full example below for more clarity:

# Sling Academy example

from fastapi import FastAPI

app = FastAPI(
    docs_url=None, # Disable docs (Swagger UI)
    redoc_url=None, # Disable redoc

def home_route():
    return {"message": "Hello World"}

Get it up and running with uvicorn:

uvicorn main:app --reload

If you go to http://localhost:8000/redoc or http://localhost:8000/docs, you will get a 404 error (page not found):

That means we have successfully disabled the built-in docs.

Notify of
Inline Feedbacks
View all comments