Native FastAPI
Routes use Depends and Security like any other dependency, and the Authorize button in your OpenAPI docs just works.
MicroAuth for FastAPI
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.
Quick starts
Pick one to see the whole app. Each example runs as shown: paste it, add your settings and try it on your machine.
Hosted sign in pages for the people who use your app. Your routes get the signed in person with their roles.
pip install "fastapi[standard]" "microauth-fastapi>=3.1"
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.
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)}
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.
MICROAUTH_ISSUER=https://yourapp.microauth.dev MICROAUTH_CLIENT_ID=... MICROAUTH_CLIENT_SECRET=... MICROAUTH_COOKIE_SECRET=... MICROAUTH_APP_URL=http://localhost:8000
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.
Developers sign up on your portal, make keys and pay you. Your route gets the customer behind each key.
pip install "fastapi[standard]" "microauth-fastapi>=3.1"
Save this as main.py. Security(auth) checks the X-API-Key header, applies your limits and bills each successful call to the customer at your price.
from fastapi import FastAPI, Securityfrom microauth_fastapi import Customer, MicroAuth app = FastAPI()auth = MicroAuth(app) # reads MICROAUTH_SECRET_KEY @app.get("/forecast")async def forecast(city: str, customer: Customer = Security(auth)): return {"city": city, "forecast": "sunny", "customer": customer.id}
Create an API to sell and pick how to charge: pay as you go, monthly plans or a free quota. Your developer portal goes live straight away, and the dashboard shows your SDK secret key. Save it in .env next to main.py.
MICROAUTH_SECRET_KEY=mas_...
fastapi dev main.py
Sign up on your portal as a developer would, create a key and call your API with it. With pay as you go, the $1 welcome credit covers the first 1,000 calls.
curl "http://localhost:8000/forecast?city=Paris" -H "X-API-Key: map_..."
Keys for the teams and services that call your API. Sign up is invite only and nobody is charged.
pip install "fastapi[standard]" "microauth-fastapi>=3.1"
Save this as main.py. It's the same SDK as a paid API. With billing off, Security(auth) checks the key and its limits and tells you which team is calling.
from fastapi import FastAPI, Securityfrom microauth_fastapi import Customer, MicroAuth app = FastAPI()auth = MicroAuth(app) # reads MICROAUTH_SECRET_KEY @app.get("/reports")async def reports(customer: Customer = Security(auth)): return {"team": customer.id, "key": customer.key_id}
Protect an internal API. Sign up is invite only, billing is off and each team can make 10 requests a second until you change it. The dashboard shows your SDK secret key. Save it in .env next to main.py.
MICROAUTH_SECRET_KEY=mas_...
fastapi dev main.py
The Connect tab has a test key you can use straight away:
curl http://localhost:8000/reports -H "X-API-Key: map_..."
Then invite your teams under Customers. Once a team joins, it makes its own keys on your portal, or you issue one from its page with an expiry date.
People sign in and approve what Claude, ChatGPT and other AI clients can do with your tools. Your users' API keys work in the same code.
pip install "fastapi[standard]" "microauth-fastapi[mcp]>=3.1"
Save this as main.py. It uses the official MCP Python SDK. Every call needs invoices:read, create_invoice also needs invoices:write, and api_keys=True accepts your users' keys next to sign in tokens.
from fastapi import FastAPIfrom mcp.server.mcpserver import MCPServerfrom microauth_fastapi import McpAuth, current_identity mcp = MCPServer("Invoices", log_level="WARNING")auth = McpAuth(scopes=["invoices:read"], api_keys=True)INVOICES: list[dict] = [] # your database @mcp.tool()@auth.tool("invoices:read")def list_invoices() -> list[dict]: me = current_identity().sub return [i for i in INVOICES if i["owner"] == me] @mcp.tool()@auth.tool("invoices:write")def create_invoice(customer: str, amount_cents: int) -> dict: me = current_identity().sub invoice = {"owner": me, "customer": customer, "cents": amount_cents} INVOICES.append(invoice) return invoice app = FastAPI()auth.mount(app, mcp) # serves /mcp next to your other routes
Create an MCP sign in service named Invoices, with the server URL http://localhost:8000/mcp. MicroAuth registers the server with invoices:read and invoices:write. Then, in the dashboard:
inv..env it shows and create the server credentials for the last two lines.MICROAUTH_ISSUER=https://yourapp.microauth.dev MICROAUTH_RESOURCE=http://localhost:8000/mcp MICROAUTH_RESOURCE_CLIENT_ID=rs_... MICROAUTH_RESOURCE_SECRET=...
fastapi dev main.py
Sign up on your sign in page and create a key under API keys on your account page. Any MCP client that can send a header connects with it:
URL: http://localhost:8000/mcp Header: Authorization: Bearer inv_...
On the Scale plan, Claude, ChatGPT and other AI clients can also sign people in through a consent screen once your server has a public https address, with no key to paste. The code stays the same. How AI clients connect
Why MicroAuth
Routes use Depends and Security like any other dependency, and the Authorize button in your OpenAPI docs just works.
Keys and limits are checked inside your app from a cached copy, so your traffic never goes through us.
Sign in runs on your server and keeps the session in an encrypted HttpOnly cookie, out of reach of browser JavaScript.
When you sell an API, developers pay you through your own Stripe account. We take no share of it.
Sign in is OAuth 2.1 and OpenID Connect, and AI access follows the MCP authorization spec, so you're never locked in.
Sign in for your pages, a paid API and an MCP server can live in the same FastAPI app, from one install.
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.
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.
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.
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.
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.
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.
Yes. Install microauth-fastapi[redis] and pass redis_url so every worker shares one exact limit. Vercel and AWS Lambda work too.
Yes. microauth-fastapi is MIT licensed. The source is on GitHub and releases are on PyPI. It needs Python 3.10 or later.
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.
You can start on a free plan without a credit card. Paid plans are a flat monthly price per service. See pricing.
Each one runs on your machine in a few minutes. The free plan needs no credit card.