# SQL and migrations

> Run SQL over HTTPS, apply and track migrations, and read your schema as JSON, for servers, the console and agents.

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

For agents: SQL endpoint: POST /sql/v1/query {query, params?, read_only?, row_limit?} → {results:[{command, columns, rows, row_count}], duration_ms}; auth: secret key, sql:read/sql:write scopes, or the owner in the console; runs as sypnia_admin (not superuser); statement_timeout 15 s (max 60), ≤ 5000 rows; read_only uses SET TRANSACTION READ ONLY; audited in sypnia_data.sql_audit. Migrations: GET/POST /sql/v1/migrations {name, sql} applied in a transaction, recorded in sypnia_data.migrations (version, name, checksum, applied_at, applied_by). GET /sql/v1/schema returns tables, columns, keys, relations, policies, functions.

## Run a query

```bash
curl -X POST https://<ref>.data.sypnia.com/sql/v1/query \
  -H "Authorization: Bearer syp_sk_…" -H "content-type: application/json" \
  -d '{ "query": "select status, count(*) from public.orders where created_at > $1 group by 1", "params": ["2026-11-01"] }'
```

```json
{
  "results": [
    { "command": "SELECT", "columns": [{ "name": "status", "type": "text" }, { "name": "count", "type": "int8" }],
      "rows": [["paid", 412], ["pending", 37]], "row_count": 2 }
  ],
  "duration_ms": 18
}
```

- Several statements in one `query` give several results.
- `read_only: true` runs in a read-only transaction: safe for agents with `sql:read`.
- Who may call it: a secret key, a token with `sql:read` / `sql:write`, or the project's owner from the console.
- It runs as `sypnia_admin`, the owner of your schemas, **not a superuser**: it cannot touch other databases or the engine's own secrets.
- Limits: 15 seconds by default (up to 60 with `timeout_ms`), 5 000 rows returned per result.
  - Each result says `truncated: true` when it had more rows than came back; `row_count` counts them all.
  - The response says the `row_limit` applied, and `row_limit_capped: 5000` when you asked for more.
- `POST /sql/v1/transaction` with `{"statements": [{"sql", "params", "expect"}…]}` runs up to 100 statements, each with its own parameters, in one transaction and each in a savepoint. `read_only` and `timeout_ms` work as in `query`.
  - An error, or an `expect` (`{rows}`, `{min}`, `{max}`) that does not hold, rolls everything back and names the statement (`409 expectation_failed`).
- Every statement is recorded in `sypnia_data.sql_audit`: who, when, a hash and the first 200 characters.

## Migrations

```bash
POST /sql/v1/migrations   { "name": "20261117_add_orders", "sql": "create table …" }
GET  /sql/v1/migrations   → the list, newest first
```

Each migration runs in one transaction and is recorded in `sypnia_data.migrations` (version, name, checksum, when, by whom). Applying the same name twice with a different checksum is refused. `npx @sypnia/data migrate` applies a folder of `.sql` files this way, in order; any other migration tool works too with a direct connection.

## The schema as JSON

`GET /sql/v1/schema` returns every table, column, type, key, relation, policy and function of the exposed schemas. The console, `npx @sypnia/data pull`, ORMs' code generators and agents all read it.
