---
title: "Export query results as CSV"
---

> 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/export/csv`

## Authentication

Requires one of the following:

- `bearerAuth`
- `apiKey`

## Request body

_Required._

- `sql_query` (`post/api/analytics/export/csv.sql_query`; string, required) — The SQL query to execute and export
  - example `"SELECT id, name, email FROM contacts WHERE workspace_id = 'workspace-id'"`
- `filename` (`post/api/analytics/export/csv.filename`; string, optional) — Custom filename for the export (without extension)
  - default `"analytics_export"`; example `"monthly_leads_report"`

## Example request

```json
{
  "sql_query": "SELECT id, name, email FROM contacts WHERE workspace_id = 'workspace-id'",
  "filename": "monthly_leads_report"
}
```

## Code samples

### cURL

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

### TypeScript

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

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/export/csv"

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

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

print(response.text)
```

## Responses

### 200

CSV file download

#### Example

```text
string
```

### 400

Invalid query, missing workspace ID, or no data to export

#### Example

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

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

### 500

Export failed

#### Example

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

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


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