[hypequery](https://hypequery.com/) › FastAPI and React Analytics with Typed Hooks | hypequery
Index for agents: https://hypequery.com/llms.txt
Canonical page: https://hypequery.com/fastapi-react-analytics

> Define ClickHouse datasets in Python, serve them from FastAPI, and get typed React hooks generated from the same definitions, with no duplicated types.

[Open sourceApache 2.0](https://github.com/hypequery/hypequery)

FastAPI + React

# FastAPI analytics with typed React hooks, generated from Python

Keep your analytics in Python, where your data team already works. hypequery serves the datasets from FastAPI and generates a typed React client from the same definitions, so the frontend can only ask for fields that exist.

[Get started ](https://hypequery.com/docs/python/client-endpoints)[React setup](https://hypequery.com/docs/react/getting-started#datasets-served-from-python)

Backend

Python + FastAPI

Frontend

@hypequery/react hooks

Contract

Exported, checked in CI

## Types drift across the language boundary

A Pydantic model in Python and a hand-written TypeScript interface in React agree until someone renames a field.

## Every chart needs fetch glue

Each panel gets its own request helper, loading state, and response type, written by hand against an API it cannot see.

## The frontend can ask for anything

Without a contract, the browser can request fields, filters, and page sizes the backend never meant to allow.

One contract

## Python defines it. React is generated from it.

Declare which endpoints clients may call in a definitions module with no side effects. Export a client contract from it, without starting a server or opening a connection.

- Only declared endpoints, dimensions, measures, and order fields type-check
- There is no tenant input at all; the tenant comes from Python auth
- Measure values arrive as strings, so decimals keep their precision
- `--check` in CI fails when definitions change but the committed contract did not

definitions.py

### Endpoints clients may call

analytics/definitions.py

```
# analytics/definitions.py — no FastAPI, driver, or credentials imported
from hypequery.datasets import (
    ClientEndpointDefinition, ClientQueryProfile, dataset, dimension,
    export_client_contract, measure,
)

orders = dataset(
    "orders",
    source="orders",
    tenant_key="org_id",
    time_key="created_at",
    dimensions={"status": dimension.string(), "country": dimension.string()},
    measures={
        "revenue": measure.sum("amount"),
        "orderCount": measure.count("order_id"),
    },
)
profile = ClientQueryProfile(default_limit=50, max_limit=500)

def build():
    """Factory for `python -m hypequery export client-contract`."""
    return export_client_contract(
        [ClientEndpointDefinition(orders, "/v1/orders", profile)]
    )
```

Install the same endpoints in FastAPI with `client_profile=` at the same path.

Generate the client

## Export, generate, and query from React

The Node CLI turns the exported contract into a typed client for `@hypequery/react`. Python needs no Node process at runtime.

Generated requests carry a schema hash. If the live endpoint no longer matches the contract, the server answers `409 CLIENT_SCHEMA_CHANGED` instead of returning the wrong shape.

The same live contract powers the MCP bridge, so React and AI agents call the same endpoints.

terminal

### From Python definitions to a React hook

terminal

```
# 1. Export the contract from Python (add --check in CI)
python -m hypequery export client-contract \
  --factory analytics.definitions:build \
  --output web/client-contract.json

# 2. Generate a typed React client in the frontend (web/)
cd web && npm install -D @hypequery/cli
npx hypequery generate:client client-contract.json \
  --output src/hypequery-client.ts

# 3. Use it in a component
#   const { data } = useDataset('orders', {
#     dimensions: ['country'],
#     measures: ['revenue'],
#   });
```

Re-run the export whenever definitions change, and commit the contract.

Where teams usually get stuck

## Questions teams ask

### FastAPI React dashboard

Typed hooks over FastAPI dataset endpoints, with caching and loading state from TanStack Query.

### Share types between Python and TypeScript

The client contract is the shared schema. Generate it, commit it, and check it in CI.

### ClickHouse dashboard with a Python backend

Keep SQL, auth, and tenancy in Python; let the frontend select dimensions and measures by name.

### Embedded customer analytics

Every hook call runs as the signed-in customer, because the tenant is resolved by your FastAPI authenticator.

Further reading

## Go deeper where it actually helps

### [React and MCP clients](https://hypequery.com/docs/python/client-endpoints)

[Declare endpoints, install them, export the contract.](https://hypequery.com/docs/python/client-endpoints)

[Open guide](https://hypequery.com/docs/python/client-endpoints)

### [React getting started](https://hypequery.com/docs/react/getting-started)

[Provider setup and the generated hooks.](https://hypequery.com/docs/react/getting-started)

[Open guide](https://hypequery.com/docs/react/getting-started)

### [FastAPI](https://hypequery.com/docs/python/fastapi)

[Mount hypequery in a FastAPI app.](https://hypequery.com/docs/python/fastapi)

[Open guide](https://hypequery.com/docs/python/fastapi)

### [Python CLI](https://hypequery.com/docs/python/cli)

[`export client-contract`, `--check`, and `--force`.](https://hypequery.com/docs/python/cli)

[Open guide](https://hypequery.com/docs/python/cli)

Related pillars

[ClickHouse with Python](https://hypequery.com/clickhouse-python)[ClickHouse with FastAPI](https://hypequery.com/clickhouse-fastapi)[ClickHouse React](https://hypequery.com/clickhouse-react)[MCP for Python backends](https://hypequery.com/clickhouse-python-mcp)[Python multi-tenancy](https://hypequery.com/clickhouse-python-multi-tenant)

Next step

## Generate your first typed hook from Python

Declare one client endpoint, export the contract, and call `useDataset` from a component.

Open source, and it runs in your own backend. Want it hosted?

[Get started ](https://hypequery.com/docs/python/client-endpoints)[React docs](https://hypequery.com/docs/react/getting-started)
