Query a database
SDK platic.tools.databases.query()
Run a read-only SELECT with the same rules as the agent: the connection's time limit, paginated results, and hidden tables and columns excluded. Use it to check results before assigning the database to an agent.
Example
curl https://api.platic.ai/v1/tools/databases/$DB_ID/query \
-H "Authorization: Bearer $PLATIC_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "sql": "SELECT id, total FROM orders ORDER BY id" }' await platic.tools.databases.query("db_123", { sql: "SELECT id, total FROM orders ORDER BY id" }); Path parameters
| Parameter | Type | Description | Required |
|---|---|---|---|
databaseId | string | The id of the connection | ✓ |
Request body
| Field | Type | Description | Required |
|---|---|---|---|
sql | string | A single SELECT, up to 10,000 characters | On the first page |
pageSize | number | Rows per page, from 1 to 200. Defaults to the connection's pageSize. Only on the first page | — |
cursor | string | The nextCursor of the previous page, exactly as it arrived | On the following pages |
Send sql for the first page and only cursor for subsequent pages.
The first page carries the query:
{
"sql": "SELECT id, total, status FROM orders ORDER BY id",
"pageSize": 2
} The following ones carry only the cursor of the previous page:
{
"cursor": "eyJ2IjoxLCJvIjoyfQ.k3Jx9mQ2"
} Response
200 OK.
{
"status": "success",
"data": {
"columns": ["id", "total", "status"],
"rows": [
[1001, 1250.5, "delivered"],
[1002, 380, "in transit"]
],
"rowCount": 2,
"hasMore": true,
"nextCursor": "eyJ2IjoxLCJvIjoyfQ.k3Jx9mQ2"
}
} | Field | Type | Description |
|---|---|---|
columns | string[] | The columns of the SELECT, in order |
rows | array[] | Each row is a list of values, in the order of columns. A value longer than 1,000 characters arrives cut |
rowCount | number | How many rows this page has |
hasMore | boolean | Whether there are more rows |
nextCursor | string | Only with hasMore: send it as cursor for the next page. It lasts 30 minutes |
notes | string[] | Notices, for example that the page was cut so it would not exceed the maximum size |
Tip
Use ORDER BY so pages do not repeat or skip rows.
Errors
| Status | details.code | When it happens |
|---|---|---|
400 | INVALID_REQUEST | sql and cursor are both missing, both are present, or a value is not accepted |
400 | SQL_READ_ONLY | It is not a SELECT, it writes something, or it uses SELECT … INTO or locks |
400 | SQL_SINGLE_STATEMENT | It has more than one statement |
400 | SQL_TABLE_NOT_AVAILABLE | It uses a hidden or system table |
400 | SQL_COLUMNS_REQUIRED | It uses * (or the whole row) on a table with hidden columns: name the columns |
400 | SQL_COLUMN_NOT_AVAILABLE | It uses a hidden column |
400 | SQL_FUNCTION_NOT_ALLOWED | It uses a function that is not allowed |
400 | SQL_NOT_SUPPORTED | The query could not be parsed or uses syntax that is not supported |
400 | CURSOR_INVALID or CURSOR_EXPIRED | The cursor is not from this connection, was tampered with, or more than 30 minutes passed. Run the query again |
403 | FORBIDDEN or INSUFFICIENT_SCOPE | The person behind the token does not have access to Integrations, or the token does not have write permission |
404 | NOT_FOUND | There is no connection with that id in the workspace |
409 | DB_TIMEOUT | The query went over the time limit and was cancelled. Make it more specific |
409 | DB_ERROR, DB_PERMISSION_DENIED, and the other DB_* | The database rejected the query, for example because of a column that does not exist, or did not respond. message says what happened |
503 | TRANSPORT_UNAVAILABLE | Database queries are not available right now. Try again |
Notes
- Querying does not write anything to the database: it is safe to repeat.
- Neither the query nor the rows are kept in the API logs.