> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.labric.co/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.labric.co/_mcp/server.

# Query data with SQL

POST https://platform.labric.co/api/v1/data/query
Content-Type: application/json

Execute a read-only SQL query.

Runs the query against the organization's read replica, so it cannot
mutate data. Only SELECT statements are accepted. Supports positional
parameters: write %s placeholders in the query and pass the values in
order in the params list. Named :param placeholders belong to saved
dataset queries and are not accepted here. Use the schema tool to discover tables
first, and reference each column by its sql_column_name — foreign keys
carry an _id suffix in SQL (e.g. a 'sample' reference is the 'sample_id'
column).

Requires an API key with the `read` scope.

Reference: https://docs.labric.co/api-reference/labric-api/data/query

## Authentication

- `Authorization` header (bearer token, required) — Bearer authentication of the form `Bearer <token>`, where token is your auth token.

## Request

### Body (application/json)

This endpoint expects a QueryBody.

- `query` (string, required)
- `params` (list of any, optional, nullable)

## Response

### 200

OK

- `columns` (list of string, required)
- `data` (list of list of any, required)

## Errors

### 400 Bad Request Error

Bad Request

- `detail` (string, required)

### 401 Unauthorized Error

Unauthorized

- `detail` (string, required)

### 403 Forbidden Error

Forbidden

- `detail` (string, required)

### 404 Not Found Error

Not Found

- `detail` (string, required)

### 422 Unprocessable Entity Error

Unprocessable Content

- `detail` (list of map from string to any, required)

### 500 Internal Server Error

Internal Server Error

- `detail` (string, required)

### 503 Service Unavailable Error

Service Unavailable

- `detail` (string, required)

## Examples

**Request**

```json
{
  "query": "SELECT i.name, i.serial_number, t.name AS instrument_type FROM core_instrument AS i LEFT JOIN core_instrumenttype AS t ON t.id = i.instrument_type_id WHERE i.name ILIKE %s ORDER BY i.name LIMIT 5",
  "params": [
    "%oven%"
  ]
}
```

**Response**

```json
{
  "columns": [
    "name",
    "serial_number",
    "instrument_type"
  ],
  "data": [
    [
      "Oven A1",
      "OV-2024-0117",
      "Bakery Oven"
    ],
    [
      "Oven A2",
      "OV-2024-0118",
      "Bakery Oven"
    ],
    [
      "Oven B1",
      "OV-2025-0042",
      "Bakery Oven"
    ]
  ]
}
```

**SDK Code**

```python
import requests

url = "https://platform.labric.co/api/v1/data/query"

payload = {
    "query": "SELECT i.name, i.serial_number, t.name AS instrument_type FROM core_instrument AS i LEFT JOIN core_instrumenttype AS t ON t.id = i.instrument_type_id WHERE i.name ILIKE %s ORDER BY i.name LIMIT 5",
    "params": ["%oven%"]
}
headers = {
    "Authorization": "Bearer <api_key>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```