---
docId: doc_eb21749ad85847a5b7fcc0ff7d7134ea
title: Query a database
description: "Run a SELECT query with the same restrictions as the agent."
api:
  group: herramientas/bases-de-datos
  auth: bearer
  operations:
    - operationId: queryDatabase
      method: POST
      path: '/v1/tools/databases/{databaseId}/query'
      postmanName: Query a database
---

<script>
  import { Ways, Note, Collapsible } from '$lib/components';
</script>

# Query a database

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

<Ways tabs="api,sdk">
<Ways.Way name="api">

```bash
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" }'
```

</Ways.Way>
<Ways.Way name="sdk">

```ts
await platic.tools.databases.query("db_123", { sql: "SELECT id, total FROM orders ORDER BY id" });
```

</Ways.Way>
</Ways>

## 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:

```json
{
  "sql": "SELECT id, total, status FROM orders ORDER BY id",
  "pageSize": 2
}
```

The following ones carry only the cursor of the previous page:

```json
{
  "cursor": "eyJ2IjoxLCJvIjoyfQ.k3Jx9mQ2"
}
```

## Response

`200 OK`.

```json
{
  "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 |

<Note type="tip">

Use `ORDER BY` so pages do not repeat or skip rows.

</Note>

## Errors

<Collapsible summary="Show error codes and fixes">

| 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 |

</Collapsible>

## 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.
