MicroAuth for FastAPI

Auth for every FastAPI app you ship. Sign in, paid APIs, internal APIs and MCP.

One package covers all four, in a few lines of code each. The checks run inside your app, so nothing sits in front of your API and your traffic never goes through us.

  • Free plan, no credit card
  • MIT licensed SDK on PyPI
  • No proxy in front of your API
  • OAuth 2.1, OpenID Connect and MCP

Quick starts

What are you building?

Pick one to see the whole app. Each example runs as shown: paste it, add your settings and try it on your machine.

Sign in for a web app

Hosted sign in pages for the people who use your app. Your routes get the signed in person with their roles.

  1. Install the SDK

    Terminal
    pip install "fastapi[standard]" "microauth-fastapi>=3.1"
  2. Add the code

    Save this as main.py. The highlighted lines are MicroAuth: UserAuth(app) adds the sign in routes, and auth.login_required sends signed out visitors to sign in and back.

    main.py
    from fastapi import Depends, FastAPIfrom microauth_fastapi import User, UserAuth app = FastAPI()auth = UserAuth(app)  # reads the MICROAUTH_* settings @app.get("/dashboard")async def dashboard(user: User = Depends(auth.login_required)):    return {"hello": user.name, "roles": sorted(user.roles)}
  3. Create the service and add your settings

    Create a sign in service. MicroAuth makes a web app for it that already accepts localhost:8000, and shows a ready .env with a cookie secret made in your browser. Save it next to main.py.

    .env
    MICROAUTH_ISSUER=https://yourapp.microauth.dev
    MICROAUTH_CLIENT_ID=...
    MICROAUTH_CLIENT_SECRET=...
    MICROAUTH_COOKIE_SECRET=...
    MICROAUTH_APP_URL=http://localhost:8000
  4. Run it and sign up

    Terminal
    fastapi dev main.py

    Open localhost:8000/dashboard. It sends you to your sign in page, and once you sign up you land back on the dashboard with your name and roles.

Why MicroAuth

Why FastAPI developers pick MicroAuth

Native FastAPI

Routes use Depends and Security like any other dependency, and the Authorize button in your OpenAPI docs just works.

No proxy in front of your API

Keys and limits are checked inside your app from a cached copy, so your traffic never goes through us.

No tokens in the browser

Sign in runs on your server and keeps the session in an encrypted HttpOnly cookie, out of reach of browser JavaScript.

Paid to your own Stripe

When you sell an API, developers pay you through your own Stripe account. We take no share of it.

Open standards

Sign in is OAuth 2.1 and OpenID Connect, and AI access follows the MCP authorization spec, so you're never locked in.

Mix all four in one app

Sign in for your pages, a paid API and an MCP server can live in the same FastAPI app, from one install.

Questions FastAPI developers ask

Can I use several of these in one app?

Yes. UserAuth, MicroAuth, TokenAuth and McpAuth are separate classes in the same package, so one FastAPI app can sign people in to its pages, sell an API and serve an MCP server at /mcp. Sign in and MCP can share one sign in service, and the API you sell has its own.

Does a token ever reach the browser?

No. UserAuth runs the code flow on your server and keeps the session in an encrypted, HttpOnly cookie. Browser JavaScript never sees a token, so a cross site scripting bug can't steal one.

What happens if MicroAuth is down?

Your API keeps answering. The SDK checks customer keys against a copy it keeps in memory, so a short outage doesn't stop your API, and a hard limit on how old that copy can be means it never serves stale data forever. Your users' API keys that were checked recently keep working for 10 more minutes by default. New sign ins do need MicroAuth, because the sign in pages are hosted.

How do role changes reach my app?

Access tokens are short lived. The SDK refreshes them just before they expire, and that is when new roles, removed permissions and suspensions arrive. When MicroAuth ends the session, the person is signed out.

Which plan do AI clients need?

Your users' API keys work on every plan, the Free plan included, so the MCP example above runs without paying. Letting Claude, ChatGPT and other AI clients sign people in through a consent screen needs the Scale plan. Your code is the same either way. See pricing.

Does it work with the official MCP Python SDK?

Yes. McpAuth mounts your MCP server in your FastAPI app, next to your other routes. If you prefer the MCP SDK's own auth settings, MicroAuthTokenVerifier plugs into them and checks tokens the same way.

I sell an API and run several workers. Does that work?

Yes. Install microauth-fastapi[redis] and pass redis_url so every worker shares one exact limit. Vercel and AWS Lambda work too.

Is the SDK open source?

Yes. microauth-fastapi is MIT licensed. The source is on GitHub and releases are on PyPI. It needs Python 3.10 or later.

I'm not using Python. Can I still use MicroAuth?

Yes. Sign in is standard OAuth 2.1 and OpenID Connect, so any library for your language works, and the HTTP API covers key checks and usage for APIs written in anything. The MCP guide lists what an MCP server in another language needs.

What does MicroAuth cost?

You can start on a free plan without a credit card. Paid plans are a flat monthly price per service. See pricing.

Pick an example and ship it today

Each one runs on your machine in a few minutes. The free plan needs no credit card.