Skip to main content

Persistent Authorization in FastAPI Swagger UI

·1 min
Table of Contents

When testing authenticated endpoints in FastAPI’s Swagger UI, you normally lose your bearer token every time you refresh the page. This gets annoying fast when you’re actively developing.

The Fix #

Add persistAuthorization: True to your Swagger UI parameters:

1
2
3
4
5
6
7
from fastapi import FastAPI

app = FastAPI(
    swagger_ui_parameters={
        "persistAuthorization": True,
    },
)

That’s it. Your authorization token is now stored in the browser’s local storage and survives page refreshes.

Why This Matters #

During development, you might refresh Swagger UI dozens of times per session. Without persistence, each refresh means:

  1. Click “Authorize”
  2. Paste your token
  3. Click “Authorize” again
  4. Close the modal

With this one-liner, you authenticate once and forget about it until the token expires.