# Generate dashboard chart SQL draft

Generate a read-only SQL draft and suggested chart type for the current data dashboard from a natural-language question. This API generates and validates a draft only; it does not execute SQL or create a dashboard chart.

```text
POST $CLOUDSIGMA_API_BASE/data-dashboards/{dashboard_id}/generate-sql
```

## Before you call this API

Configure the [regional API endpoint and authentication](https://omnifabric.cloudsigma.com/docs/developer/endpoints-and-authentication.html.md), and select the target workspace.

First choose a data dashboard with a configured database and data tables in [List data dashboards](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/data-dashboards/list-data-dashboards.html.md#successful-response).

## Request body

Replace caller-specific values in the example with actual values.

:::::::{div} mo-api-tabs
::::::{tab-set}
:::::{tab-item} Example

```bash
curl -X POST "$CLOUDSIGMA_API_BASE/data-dashboards/$DASHBOARD_ID/generate-sql" \
  -H "X-API-Key: $CLOUDSIGMA_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "question": "Show daily sales for the last 30 days",
    "title": "Sales trend for the last 30 days"
  }'
```

:::::
:::::{tab-item} Parameters

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `question` | string | Yes | The question to answer with data. It cannot be empty. |
| `title` | string | No | A chart title that helps generate the draft. |

:::::
::::::
:::::::

## Successful response

On success, the API returns a draft that passed read-only and SQL-executability validation. To view query results, pass `sql_text` to [Preview dashboard chart SQL](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/data-dashboards/preview-data-dashboard-chart-sql.html.md#request-example).

:::::::{div} mo-api-tabs mo-api-response-tabs
::::::{tab-set}
:::::{tab-item} Example

```json
{
  "code": 200,
  "data": {
    "dashboard_id": "dashboard-001",
    "sql_text": "SELECT sale_date, SUM(amount) AS total_amount FROM sales WHERE sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) GROUP BY sale_date ORDER BY sale_date",
    "chart_type": "line"
  }
}
```

:::::
:::::{tab-item} Fields

::::{tab-set}
:::{tab-item} Common fields

| Field | Type | Description |
| --- | --- | --- |
| `code` | integer | `200` on success. |
| `data` | object | The SQL draft. |

:::
:::{tab-item} SQL draft

The following table expands the `data` object in the response example. Each row describes one field in that object.

| Field | Type | Description |
| --- | --- | --- |
| `dashboard_id` | string | The data dashboard ID. |
| `sql_text` | string | A generated, validated, read-only SQL draft. |
| `chart_type` | string | The suggested chart type. |

:::
::::
:::::
::::::
:::::::

## Error response

:::::::{div} mo-api-tabs mo-api-response-tabs
::::::{tab-set}
:::::{tab-item} Example

```json
{
  "code": "ErrDataDashboardSQLDraftValidationFailed",
  "message": "The generated SQL did not pass validation"
}
```

:::::
:::::{tab-item} Fields

| Field | Type | Description |
| --- | --- | --- |
| `code` | integer or string | An error code or status code. |
| `message` | string | Error message. |

:::::
::::::
:::::::

## Next steps

Use [Preview dashboard chart SQL](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/data-dashboards/preview-data-dashboard-chart-sql.html.md#request-example) to inspect the result, then [create a dashboard chart](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/data-dashboards/create-data-dashboard-chart.html.md#request-example) after confirming it.
