# Receipts

> Every response shows the SQL the server ran and the datasets it read.

Source: https://openpipe.health/docs/receipts/

A number about healthcare is only useful if someone can check it. Every data response from the API therefore includes its own receipt: the exact SQL the server ran, its parameters, and the datasets it touched.

The receipt is for reading. The server writes every statement, either from the filters of a search or, for [Ask](https://openpipe.health/docs/api/ask.md), with a model whose SQL is checked before it runs. No endpoint accepts SQL from a caller.

## Fields

| Field | On | Meaning |
|---|---|---|
| `sql` | All data endpoints | The statement that ran. On lookups, an object of named statements. |
| `params` | Search endpoints | Values bound to `$1`, `$2`, … in `sql` |
| `sources` | Search, lookup, Ask tiles | Names of the datasets the statement read |
| `ms` | All data endpoints | Time the query took, in milliseconds |
| `engine` | Exports, Ask tiles | `postgres` or `duckdb`. See [which engine ran](#which-engine-ran). |

## Follow a receipt to its source

1. Take a name from `sources`, for example `medicare_service`.
2. Find it in [`GET /api/meta`](https://openpipe.health/docs/api/meta.md#list-datasets). That gives you the publisher, the file version and the URL the file was downloaded from.
3. Read the `sql` against the [Tables](https://openpipe.health/docs/data/tables.md) page to see which columns and filters produced the number.

```sh
curl "$OPH_API/api/providers?hcpcs=27447&state=TX&limit=3" \
  -H "Authorization: Bearer $OPH_KEY" | jq '{sql, params, sources}'
```

```json
{
  "sql": "SELECT s.npi,\n  trim(coalesce(m.first_name || ' ', '') || m.last_org_name) AS name, ...",
  "params": ["27447", "TX"],
  "sources": ["medicare_service", "medicare_provider"]
}
```

Search statements use numbered placeholders; `params` holds the values in order.

## Get every row behind a receipt

A receipt comes with a page of rows or a tile of at most 100. For all of them, ask for an [export](https://openpipe.health/docs/api/exports.md) by reference: the filters of the search, or the dashboard id and tile index of an answer. The server runs the same statement without the page limit and writes a CSV.

## Which engine ran

The same tables are held in two engines. Postgres answers indexed lookups in about a millisecond. DuckDB, reading a columnar copy of the same data, is much faster at scanning tens of millions of rows.

For each statement the model writes, the API asks Postgres for its cost estimate, without running anything. Cheap plans stay on Postgres. Plans over a cost threshold (10,000 by default) go to DuckDB, and so does anything that reads a table held only in the columnar store, such as `partd_drug`. If DuckDB is unavailable or rejects valid Postgres syntax, the statement runs on Postgres. `engine` tells you which one answered. Both hold the same rows, so results match.

## How model-written SQL is checked

A statement planned for an Ask tile is treated as untrusted. Before it runs:

- It must be one statement that starts with `SELECT` or `WITH`, at most 8,000 characters.
- Write and session keywords, functions that reach outside the query, system schemas and the account tables (workspaces, keys, usage, dashboards, profiles, exports) are rejected wherever they appear.
- It runs as a database login that can only `SELECT` the data tables, inside a read-only transaction, with a 5-second limit and a row cap.

A statement that fails any of these becomes a failed tile. The same checks run again every time a stored dashboard is refreshed or one of its tiles is exported.
