# 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 https://moi.matrixorigin.cn/newmoi/data-dashboards/{dashboard_id}/generate-sql
```

## Preparation before calling

First choose a data dashboard with a configured database and data tables in [List data dashboards](list-data-dashboards.md#successful-response).

Prepare a [personal access token](../../../../../guides/genesis/api-keys.md#create-and-manage-personal-access-tokens) with access to the target workspace and the [target workspace ID](../../../../../guides/ai-studio/resource-center/workspace.md#copy-a-workspace-id).

## 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 "https://moi.matrixorigin.cn/newmoi/data-dashboards/$DASHBOARD_ID/generate-sql" \
  -H "X-API-Key: $AI_STUDIO_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](preview-data-dashboard-chart-sql.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](preview-data-dashboard-chart-sql.md#request-example) to inspect the result, then [create a dashboard chart](create-data-dashboard-chart.md#request-example) after confirming it.
