Skip to main content
George Khananaev
Open Source

FastAPI DocShield: protect your API docs with one line

HTTP Basic Auth on the OpenAPI docs endpoints for FastAPI

Inside the project

1 / 1 · Project illustration

FastAPI DocShield project illustration

Project illustration

Open image

Overview

FastAPI DocShield is a small library that locks down the auto-generated documentation endpoints on a FastAPI application. By default FastAPI exposes /docs, /redoc, and /openapi.json without authentication, which is fine in development but often unwanted in production. DocShield adds HTTP Basic Auth to those routes with a single import.

Why this exists

Most FastAPI apps in production want to keep the interactive docs around for internal teams but do not want every GoogleBot in the world crawling them. The obvious solutions are either to disable the docs in production (annoying when you need them) or to write a middleware manually (easy to get wrong). DocShield captures the right approach in a reusable package.

How it works

from fastapi import FastAPI
from fastapi_docshield import DocShield

app = FastAPI()
DocShield(app, username="admin", password="secret")

Behind the scenes it wraps the three doc endpoints with a dependency that checks the Authorization header for Basic credentials. If they match, the request proceeds. If they don't, a WWW-Authenticate header is returned so the browser prompts for credentials.

Security notes

Basic Auth is fine for this use case — you are protecting documentation, not user data. If the docs themselves contain sensitive information (internal endpoints, undocumented features), consider combining DocShield with a VPN or an IP allowlist so a credential leak cannot single-handedly expose the API surface.

Always use HTTPS in production. Basic Auth sends credentials in base64 encoding, which is trivially decoded if intercepted.

Tech stack

Pure FastAPI. No external dependencies beyond the framework itself.

Takeaway

Small libraries that do one thing well earn adoption. DocShield is ~50 lines of code and has saved me from writing the same middleware in three different projects.

Read Dark Theme Auth FastAPI Server
Dark-themed Swagger UI from the FastAPI starter repository, showing an interactive documentation example1 images
Public projectGitHub

Dark Theme Auth FastAPI Server

A FastAPI template with auth-protected endpoints, Redis, logging, and a custom dark-themed docs UI

A versatile FastAPI server template with authentication-protected endpoints, Redis caching, structured logging, and a custom dark theme for the API documentation. A solid foundation for production APIs that want opinions baked in from day one.

fastapipythonredisauthentication+1 more
2 min read10
Explore project
Read PyNextStack: a full-stack user management starter
PyNextStack public demo sign-in interface2 images
Public projectGitHub

PyNextStack: a full-stack user management starter

FastAPI backend, Next.js frontend, auth and profiles out of the box

A full-stack user management system with a Next.js frontend and FastAPI backend. Registration, authentication, profile management, and a Material-UI interface. Production-ready defaults so you can focus on your actual product instead of rebuilding the login form.

fastapinextjspythontypescript+2 more
2 min read25
Explore project
Read Travel Panel: Enterprise Platform & Leadership
Moon Holidays public website, representing the organization rather than its private application1 images
Commercial workConfidential

Travel Panel: Enterprise Platform & Leadership

Confidential commercial work · public purpose and technologies

A public overview of Travel Panel, George’s engineering leadership, and organizational AI and automation work. Internal implementation stays private.

pythonfastapitypescriptnextjs+11 more
2 min read
Read overview