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

2 min read

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.

Dark Theme Auth FastAPI Server
2 min10

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
PyNextStack: a full-stack user management starter
PyNextStack: a full-stack user management starter illustrationFeaturedGitHub
2 min25

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
Travel Panel: the core travel management platform
Travel Panel: the core travel management platform illustrationFeaturedMoon Holidays
11 minDec 2022 — Present

Travel Panel: the core travel management platform

FastAPI backend, Next.js operator portal, and B2B partner portal powering Moon Holidays end to end

Travel Panel is the core system at Moon Holidays. A FastAPI backend, a Next.js operator portal, a B2B partner portal, and the orchestrator for every downstream product: TravelOffer for end customers, Live Deck for call-center TVs, Vercel Controller for deployment cache, StaySync for allotment availability, and a WebSocket messenger for internal communication. Running on AWS with ALB, MemoryDB, CloudFront, S3, and more.

fastapinextjspythontypescript