# Dataset endpoints (/docs/python/dataset-endpoints)

A dataset endpoint is a `POST` endpoint that runs semantic queries against one dataset through your dataset client
(sync or async). Serve it at the conventional path with `create_api(datasets=...)`, or at any path with
`add_dataset_endpoint`:

<CodeBlock>
```python
from hypequery.serve import EndpointPolicy, add_dataset_endpoint, create_api, create_dataset_endpoint

policy = EndpointPolicy(tenant="required", max_limit=500)

# POST /datasets/orders/query
api = create_api(
    auth=authenticate,
    datasets={"orders": create_dataset_endpoint(dataset=orders, client=client, policy=policy)},
)

# or at a path of your choosing
add_dataset_endpoint(api, "/v1/orders", dataset=orders, client=client, policy=policy)
```
</CodeBlock>

A metric endpoint fixes one measure: `create_metric_endpoint(dataset=orders, measure="revenue", client=client)`,
served with `create_api(metrics={"revenue": ...})` at `POST /metrics/revenue`, or with `add_metric_endpoint(api, path,
...)`. In `metrics=`, the key names the metric: `metrics={"sales": ...}` serves `POST /metrics/sales`, and its rows and
client contract call the measure `sales`.

## Request body [#request-body]

<CodeBlock>
```json
{
  "dimensions": ["country"],
  "measures": ["revenue"],
  "filters": [{ "field": "status", "operator": "eq", "value": "completed" }],
  "orderBy": [{ "field": "revenue", "direction": "desc" }],
  "by": "month",
  "limit": 10,
  "offset": 0,
  "includeMeta": true
}
```
</CodeBlock>

The body is validated strictly:

- unknown fields are rejected with `400 VALIDATION_ERROR`;
- numbers and booleans are not coerced from strings;
- attempts to supply a tenant, SQL, settings, roles, or scopes are refused.

Omitting `measures` selects the dataset's default measures; `"measures": []` selects none. `timezone` (an IANA name)
is also accepted.

## Related fields [#related-fields]

An endpoint's policy says who may call it, not who may read the datasets its relationships reach. So fields from
related datasets are off by default: `customer.country`, related measures, and named filters defined on a related
field are refused with `400 VALIDATION_ERROR` and left out of the client contract. Opt in per relationship when this
endpoint's policy is enough to read the related data:

<CodeBlock>
```python
create_dataset_endpoint(dataset=orders, client=client, policy=policy, include_related={"customer"})
```
</CodeBlock>

## Response [#response]

<CodeBlock>
```json
{
  "data": [{ "country": "NZ", "revenue": "1200.5" }],
  "meta": {
    "requestId": "…",
    "timingMs": 12,
    "rowCount": 1,
    "pagination": { "limit": 10, "offset": 0, "hasMore": false },
    "cache": { "hit": false }
  }
}
```
</CodeBlock>

- `meta` is included only when the body sets `includeMeta: true` or the request sends `x-include-meta: true`.
- `meta.cache` appears only when the dataset client has a result cache; without one it is omitted, as in TypeScript.
- Non-null measure values are strings on the wire, matching TypeScript. So is a dimension integer beyond
  ±(2^53 − 1), such as a `UInt64` id, which a JavaScript client would otherwise round. Local Python client results keep
  native values.

## Pagination [#pagination]

Page sizes default to 1,000 and are clamped to the endpoint's `max_limit` and the dataset's limits. The endpoint
fetches one extra row to compute `hasMore`, then drops it from the returned data and row count.

## Streaming [#streaming]

An endpoint can stream its result for exports and other large reads. Set `stream_max_rows` on its policy; it caps one
streamed response and may not exceed the dataset's `max_stream_rows` (else its `max_result_size`), so an export can
return more rows than the same endpoint's pages:

<CodeBlock>
```python
policy = EndpointPolicy(tenant="required", max_limit=500, stream_max_rows=1_000_000)
add_dataset_endpoint(api, "/orders/query", dataset=orders, client=analytics, policy=policy)
```
</CodeBlock>

A request asks for a stream with `Accept`. `application/x-ndjson` returns one JSON frame per line: `start` (with
`meta` when the request sets `includeMeta`), one `rows` frame per batch holding the rows a page would, and a final
`end` frame with the `rowCount`. `application/vnd.apache.arrow.stream` returns an Arrow IPC stream with ClickHouse's
native column types, and needs `hypequery[arrow]`. The frames are the same as TypeScript serve's.

Streams keep the endpoint's authentication, roles, tenant scope and validation, and are never cached. Failures before
the first row are ordinary JSON errors with their status. A query that fails after the response began ends an NDJSON
stream with an `error` frame instead of `end`, and breaks an Arrow stream off before its end-of-stream marker.

Streaming needs a host that writes a response as it is produced: `to_asgi_app` (uvicorn, a FastAPI or Starlette mount,
Django's ASGI handler). Under WSGI, the Django views and Flask, a stream request gets the usual JSON page. With a
`ProductionProfile`, a stream holds its admission slot until it ends, its whole run counts against `timeout_seconds`,
`max_result_rows` caps its rows, and `max_result_bytes` caps what it writes: an NDJSON stream that would pass it ends
with a `PAYLOAD_TOO_LARGE` error frame, and an Arrow stream is broken off before its end-of-stream marker. Metric
endpoints and strict client endpoints answer with pages.

## Discovery [#discovery]

`create_api` serves `GET /discovery` whenever it serves `datasets=` or `metrics=`: a logical catalog of their
datasets. Pass `discovery=EndpointPolicy(...)` to change who may read it, or `discovery=False` to leave it out. To
publish a registry's catalog at another path, use `add_discovery_endpoint`:

<CodeBlock>
```python
add_discovery_endpoint(api, "/v1/discovery", registry=registry)
```
</CodeBlock>

- It requires authentication by default. Pass `EndpointPolicy(public=True)` only for intentionally public discovery.
- The catalog matches TypeScript's `/discovery` projection. Physical tables, columns, SQL, tenant policy, and tenant
  values never appear in it.
- Its size is capped at 256 KiB by default (`max_bytes=`), checked at startup.