---
title: "Install DataPocket on FastAPI"
description: "Add DataPocket web analytics with revenue attribution to FastAPI. Async proxy with httpx."
url: "https://datapocket.vo1d.studio/docs/fastapi"
lang: "en"
updated: "2026-11-23"
---

# Install DataPocket on FastAPI

Async proxy with httpx. Replace `YOUR_SITE_ID` and `YOUR_DOMAIN` with the values from your site's Install page. This guide serves the script from your own domain, so ad blockers can't tell it apart from your own code.

## Steps

1. pip install httpx, then add:

```py
import re
import httpx
from fastapi import FastAPI, Request, Response

PD = "https://datapocket.vo1d.studio"
app = FastAPI()

@app.get("/dp/js/{file}")
async def dp_script(file: str):
    if not re.fullmatch(r"script(\.[a-z]+)?(@\d+\.\d+\.\d+)?\.js", file):
        return Response(status_code=404)
    async with httpx.AsyncClient() as client:
        r = await client.get(f"{PD}/js/{file}")
    return Response(r.content, media_type="application/javascript", headers={"Cache-Control": "public, max-age=3600"})

@app.post("/dp/api/event")
async def dp_event(request: Request):
    ip = request.headers.get("x-forwarded-for", request.client.host if request.client else "").split(",")[0].strip()
    async with httpx.AsyncClient() as client:
        r = await client.post(f"{PD}/api/event", content=await request.body(), headers={
            "Content-Type": "text/plain", "User-Agent": request.headers.get("user-agent", ""), "X-Forwarded-For": ip})
    return Response(r.content, status_code=r.status_code, media_type="application/json")
```

2. Then load the script from your own domain. Ad blockers can't tell it apart from your own code, so visits from people whose ad blocker blocks analytics domains are still counted:

```html
<script
  defer
  data-site-id="YOUR_SITE_ID"
  data-domain="YOUR_DOMAIN"
  src="/dp/js/script.js"
></script>
```

## Revenue attribution

1. Pass the visitor cookie to Stripe Checkout so each sale is attributed to the channel that brought the buyer:

```py
session = stripe.checkout.Session.create(
    # …your line_items, mode, success_url…
    metadata={
        "datapocket_visitor_id": request.cookies.get("dp_vid", ""),
        "datapocket_session_id": request.cookies.get("dp_sid", ""),
    },
)
```

2. Using another payment provider? Send payments from your backend (create an API key in Settings):

```bash
curl -X POST https://datapocket.vo1d.studio/api/v1/payments \
  -H "Authorization: Bearer dpk_…" \
  -H "Content-Type: application/json" \
  -d '{"amount": 29.99, "currency": "USD", "transaction_id": "order_123", "visitor_id": "<dp_vid cookie>"}'
```

## Verify

1. Deploy or publish, then open your site in a normal browser window (ad blockers off).
2. In the browser's Network tab, look for a POST to `/api/event` (or `/dp/api/event` when proxied) that returns `{"ok":true}`.
3. The DataPocket Install page for this site switches to "Receiving data" within a few seconds.

## Related guides

- [Next.js](https://datapocket.vo1d.studio/docs/nextjs): App Router or Pages Router. Client-side navigations are tracked automatically.
- [Astro](https://datapocket.vo1d.studio/docs/astro): Static or server-rendered. Navigations with <ClientRouter /> (view transitions) are tracked automatically.
- [React Router](https://datapocket.vo1d.studio/docs/react-router): Framework mode (Remix) and library mode. pushState navigations are tracked automatically.
- [Vue.js](https://datapocket.vo1d.studio/docs/vue): Vue 3 + Vue Router, Nuxt, Vite. History-mode routing is tracked automatically.
- [AI crawler tracking](https://datapocket.vo1d.studio/docs/ai-bots)
