---
title: "Execute an analytics query"
---

> Documentation Index
> Fetch the complete documentation index at: https://developer.channelautomations.com/llms.txt
> Use this file to discover all available pages before exploring further.

Path: Channel Automation API › Analytics

`POST /api/analytics/execute`

## Authentication

Requires one of the following:

- `bearerAuth`
- `apiKey`

## Request body

_Required._

- `sql_query` (`post/api/analytics/execute.sql_query`; string, required) — The SQL query to execute (SELECT only)
  - example `"SELECT id, name, created_at FROM contacts WHERE workspace_id = 'workspace-id' LIMIT 100"`

## Example request

```json
{
  "sql_query": "SELECT id, name, created_at FROM contacts WHERE workspace_id = 'workspace-id' LIMIT 100"
}
```

## Code samples

### cURL

```bash
curl --request POST \
  --url https://api.channelautomation.com/api/analytics/execute \
  --header 'Content-Type: application/json' \
  --data @- <<'EOF'
{
  "sql_query": "SELECT id, name, created_at FROM contacts WHERE workspace_id = 'workspace-id' LIMIT 100"
}
EOF
```

### TypeScript

```typescript
const url = 'https://api.channelautomation.com/api/analytics/execute';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({
    sql_query: 'SELECT id, name, created_at FROM contacts WHERE workspace_id = \'workspace-id\' LIMIT 100'
  })
};

fetch(url, options)
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error(err));
```

### Python

```python
import requests

url = "https://api.channelautomation.com/api/analytics/execute"

payload = { "sql_query": "SELECT id, name, created_at FROM contacts WHERE workspace_id = 'workspace-id' LIMIT 100" }
headers = {"Content-Type": "application/json"}

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

print(response.text)
```

## Responses

### 200

Query executed successfully

#### Example

```json
{
  "success": true,
  "data": [
    {}
  ],
  "metadata": {
    "execution_time_ms": 0,
    "row_count": 0
  }
}
```

- `success` (`post/api/analytics/execute.response.200.success`; boolean, optional)
- `data` (`post/api/analytics/execute.response.200.data`; array<object>, optional)
- `metadata` (`post/api/analytics/execute.response.200.metadata`; object, optional)
  - `execution_time_ms` (`post/api/analytics/execute.response.200.metadata.execution_time_ms`; integer, optional)
  - `row_count` (`post/api/analytics/execute.response.200.metadata.row_count`; integer, optional)

### 400

Invalid query or missing workspace ID

#### Example

```json
{
  "error": "string",
  "details": "string"
}
```

- `error` (`post/api/analytics/execute.response.400.error`; string, optional)
- `details` (`post/api/analytics/execute.response.400.details`; string, optional)

### 500

Query execution failed

#### Example

```json
{
  "error": "string",
  "details": "string"
}
```

- `error` (`post/api/analytics/execute.response.500.error`; string, optional)
- `details` (`post/api/analytics/execute.response.500.details`; string, optional)


Source: https://developer.channelautomations.com/api/post-api-analytics-execute/index.md
