Raw SQL
Run your own SQL statement against one database with POST /api/databases/{dbid}/query.
This needs the Databases.sql permission (Run SQL in the admin app). Your organization's administrators hold it. Other members and API keys need it granted, and a grant of * or Databases.* includes it. The admin app's SQL console uses the same endpoint.
Run a statement
curl -X POST https://api.vaultdb.io/api/databases/shop/query \
-H "Authorization: Bearer $VAULTDB_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"sql": "SELECT customer, SUM(total) AS spent FROM orders WHERE total > ? GROUP BY customer",
"params": [10]
}'
| Field | Required | Description |
|---|---|---|
sql |
Yes | One SQL statement. |
params |
No | Values for the ? placeholders, in order. Each is a string, number, boolean or null. |
Use params for every value that comes from outside your code. Do not build SQL by joining strings.
The response echoes the statement and carries the result:
{
"sql": "SELECT customer, SUM(total) AS spent FROM orders WHERE total > ? GROUP BY customer",
"result": [{ "customer": "ada", "spent": 130.5 }]
}
For a statement that reads, result is the array of rows. For a statement that writes, result is { "changes", "lastRowId" }.
A read returns at most 1,000 rows. When the statement had more, the response also carries "truncated": true; use LIMIT and OFFSET, or a WHERE on a key, to read the rest.
A statement may run for 10 seconds. A longer one is stopped and answered with 408, and a write stopped this way leaves nothing written.
What is blocked
| Statement | Response |
|---|---|
Starts with CREATE, DROP or ALTER |
403. Use the table API to manage tables. |
Is any other statement than SELECT, INSERT, UPDATE, DELETE, REPLACE, WITH or VALUES, such as PRAGMA, ATTACH, VACUUM, BEGIN or EXPLAIN |
403. Comments before the statement are skipped, so they do not change this. |
Contains pragma_ anywhere, in any letter case |
403. This blocks SQLite's pragma functions, such as pragma_table_info. |
Contains sqlite_ anywhere, in any letter case |
403. SQLite's internal tables are not reachable. This also blocks a statement whose string values contain sqlite_. |
| Does not parse | 400 |
| Runs longer than 10 seconds | 408, and nothing is written |
| Sent while 2 of your statements, or 4 on the server, are still running | 429. Wait for one to finish and send it again. |
| Writes, on the Free plan | 403. The Free plan allows read-only SQL. |
| Writes, once your databases use all of your plan's storage, or would grow them past it | 403, and nothing is written |
| Adds more rows to one table than your plan allows in a bulk insert | 403, and nothing is written |
| Leaves a table it added rows to above your plan's rows per table | 403, and nothing is written |
The Free plan's read-only check looks at what the statement would do, not at how it starts. A statement such as WITH x AS (SELECT 1) DELETE FROM orders counts as a write and is refused.
The row limits are the same ones the table API applies, and they are checked per table, by how many rows the statement adds. A statement that deletes rows or changes rows in place is never refused for row limits.
Things to know
- Raw SQL does not apply row-level policies. A key that can run SQL can read and change every row in the database. Do not grant
Databases.sql,Databases.*or*to a key that row-level policies are meant to restrict; give it the specific permissions it needs instead. - Writes made with raw SQL do not send webhooks or realtime events.