# Data and SQL endpoints

> The Data API's operations and headers, and the SQL endpoints.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/api/data-endpoints/

For agents: Data API: GET/HEAD/POST/PATCH/DELETE /rest/v1/{table}, POST /rest/v1/rpc/{fn}, POST /rest/v1/transaction {operations: [{name?, method, table, schema?, query?, body?, increment?, expect?, on_error?, returning?}] ≤ 100}, GET /rest/v1/ (OpenAPI 3.1 of the exposed schema; not with a publishable key alone); query params select, filters (col=op.value: eq neq gt gte lt lte like ilike in is cs cd ov fts plfts phfts wfts, not.), or, and, order, limit, offset, on_conflict, columns; headers Prefer (return=minimal|representation, count=exact|planned|estimated, resolution=merge-duplicates|ignore-duplicates, missing=default), Range/Range-Unit, Accept-Profile/Content-Profile, Accept: application/vnd.pgrst.object+json; response Content-Range. Errors invalid_query, invalid_body, empty_update, filter_required, table_not_found, column_not_found, function_not_found, relationship_not_found, permission_denied (RLS/grants), statement_timeout, expectation_failed, insufficient_scope. SQL: POST /sql/v1/query {query, params?, read_only?, row_limit? ≤ 5000, timeout_ms? ≤ 60000}, POST /sql/v1/transaction {statements: [{sql, params?, expect?}] ≤ 100, read_only?, timeout_ms?}, GET/POST /sql/v1/migrations, GET /sql/v1/schema?schemas=; auth secret key, sql:* scopes or console owner.

## The Data API

| Operation                             | Path                                                                                    |
| ------------------------------------- | --------------------------------------------------------------------------------------- |
| read                                  | `GET /rest/v1/{table}` (`HEAD` for the headers and the count only)                      |
| insert / upsert                       | `POST /rest/v1/{table}`                                                                 |
| update                                | `PATCH /rest/v1/{table}?{filters}`                                                      |
| delete                                | `DELETE /rest/v1/{table}?{filters}`                                                     |
| call a function                       | `POST /rest/v1/rpc/{function}` with the arguments as a JSON object                      |
| several operations in one transaction | `POST /rest/v1/transaction`                                                             |
| the API description                   | `GET /rest/v1/` (OpenAPI 3.1; a signed-in user, a PAT, a machine token or a secret key) |

| Query parameter   | Example                                                                                                               |
| ----------------- | --------------------------------------------------------------------------------------------------------------------- |
| `select`          | `id,total,customer:customers(name)`                                                                                   |
| a filter          | `status=eq.paid`, `total=gte.50000`, `tags=cs.{pan}`, `name=ilike.*masa*`, `deleted_at=is.null`, `not.status=eq.void` |
| `or`, `and`       | `or=(status.eq.paid,total.gt.100000)`                                                                                 |
| `order`           | `created_at.desc.nullslast,id`                                                                                        |
| `limit`, `offset` | `20`, `40`                                                                                                            |
| `on_conflict`     | `email` (with an upsert)                                                                                              |
| `columns`         | `note,total` (with an insert: the columns to take from the body)                                                      |

Operators: `eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `like`, `ilike`, `in`, `is`, `cs`, `cd`, `ov`, `fts`, `plfts`, `phfts`, `wfts`, each optionally after `not.`.

| Header                                      | Values                                                                                                                                   |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `Prefer`                                    | `return=minimal\|representation`, `count=exact\|planned\|estimated`, `resolution=merge-duplicates\|ignore-duplicates`, `missing=default` |
| `Range` / `Range-Unit`                      | `0-19` / `items`                                                                                                                         |
| `Accept-Profile` / `Content-Profile`        | a schema name                                                                                                                            |
| `Accept: application/vnd.pgrst.object+json` | one object instead of a list; `406 not_single` when the result has another number of rows                                                |
| `Content-Range` (response)                  | `0-19/1324`                                                                                                                              |

A transaction:

| Field of an operation |                                                                              |
| --------------------- | ---------------------------------------------------------------------------- |
| `name`                | optional; later operations use its results with `{"$ref": "<name>.<field>"}` |
| `method`              | `GET`, `POST`, `PATCH` or `DELETE`                                           |
| `table`, `schema`     | the table, and its schema when it is not the default                         |
| `query`               | a PostgREST query, as a string or an object; object values may be `$ref`s    |
| `body`                | rows (`POST`) or values (`PATCH`); any value may be a `$ref`                 |
| `increment`           | `PATCH` only: `{ "col": n }` adds `n` in the database                        |
| `expect`              | `{ rows }`, `{ min }` or `{ max }`: rows returned or changed                 |
| `on_error`            | `rollback` (default) or `continue`                                           |
| `returning`           | `true` (default) returns the rows                                            |

The answer is `{ "results": [ { "op", "name", "count", "rows" } … ] }`, with `{ "op", "name", "error" }` for an operation that failed with `on_error: "continue"`. See [The Data API](https://data.docs.sypnia.com/data/rest-api#transactions).

Errors: `invalid_query`, `invalid_body`, `empty_update`, `filter_required` (update or delete without filters), `table_not_found`, `column_not_found`, `function_not_found`, `relationship_not_found`, `permission_denied` (a grant or a policy said no), `statement_timeout`, `expectation_failed`, `insufficient_scope`, and the database's own errors. Every code: [Errors and limits](https://data.docs.sypnia.com/api/errors-and-limits#the-data-api).

## SQL

| Endpoint                                                                                          | Does                                                                                                           |
| ------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `POST /sql/v1/query` `{ query, params?, read_only?, row_limit?, timeout_ms? }`                    | runs SQL; several statements give several results; `params` only with one statement                            |
| `POST /sql/v1/transaction` `{ statements: [{ sql, params?, expect? }], read_only?, timeout_ms? }` | up to 100 statements in one transaction, each in a savepoint; answers `{ results, duration_ms, row_limit }`    |
| `GET /sql/v1/migrations`                                                                          | applied migrations: `version`, `scope` (`engine` or `project`), `name`, `checksum`, `applied_at`, `applied_by` |
| `POST /sql/v1/migrations` `{ name, sql }`                                                         | applies one in a transaction; `201`, or `409 migration_checksum_mismatch`                                      |
| `GET /sql/v1/schema?schemas=public`                                                               | tables, columns, keys, relations, policies (and whether row-level security is on), functions as JSON           |

| Field        | Default | Limit                                        |
| ------------ | ------- | -------------------------------------------- |
| `row_limit`  | 5 000   | 5 000; more answers `row_limit_capped: 5000` |
| `timeout_ms` | 15 000  | 60 000                                       |

Each result has `command`, `columns`, `rows`, `row_count` (every row the statement returned or changed) and `truncated` when more rows came back than `row_limit`.

Who may call: a secret key, a token with `sql:read` (read-only queries, `schema` and the migrations list) or `sql:write` (everything), or the project's owner from the console. See [SQL and migrations](https://data.docs.sypnia.com/data/sql-and-migrations).
