Add analytics to FastAPI.
smolanalytics tracks a FastAPI service two ways: async server-side events posted with httpx from your endpoints, and a browser snippet for any HTML you serve that autocaptures pageviews and clicks. You query it in plain English and get deterministic reports, with a CI test proving the numbers aren't hallucinated.
import httpx
from fastapi import BackgroundTasks
async def send_event(name: str, distinct_id: str, props: dict):
async with httpx.AsyncClient(timeout=2) as client:
await client.post(
"https://YOUR-INSTANCE/v1/events",
headers={"Authorization": "Bearer WRITE_KEY"},
json={"name": name, "distinct_id": distinct_id, "properties": props},
)
@app.post("/signup")
async def signup(bg: BackgroundTasks):
# ... create the user ...
bg.add_task(send_event, "signup", str(user.id), {"plan": "pro"})
return {"ok": True}
# Web autocapture (if you serve HTML): two tags in your template <head>
# <script src="https://YOUR-INSTANCE/sdk.js"></script>
# <script>smolanalytics.init("WRITE_KEY", { host: "https://YOUR-INSTANCE" })</script>use BackgroundTasks (shown) so the event fires after the response returns and never adds latency to the request.
how do I add analytics to fastapi?
smolanalytics tracks a FastAPI service two ways: async server-side events posted with httpx from your endpoints, and a browser snippet for any HTML you serve that autocaptures pageviews and clicks. You query it in plain English and get deterministic reports, with a CI test proving the numbers aren't hallucinated.
FastAPI is usually an API, so the server side does most of the work here. Fire events from BackgroundTasks with httpx and they never touch your response time. The body is plain JSON: a name, a distinct_id, a properties dict, batched into a list when you have several. If you also serve HTML (docs pages, a landing template), the two script tags add browser autocapture on the same write key.
The collector is one Go binary you self-host (MIT) or run hosted from $29/mo. Answers come back as deterministic reports with a CI test that proves they can't be invented, so an agent building against your metrics can trust them. Ask in plain English from the dashboard bar or straight from your editor over MCP.
Honest pricing: 14-day full trial, no credit card. Then Solo $29/mo, never metered on seats or sites. Overage is $5/million with an emailed receipt, the dashboard never locks, and self-hosting the single Go binary is free forever (MIT).
Add analytics to FastAPI tonight.
One snippet, or one endpoint. Tomorrow morning the verdict tells you which part of the funnel to fix.