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.
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.
Prepare a personal access token with access to the target workspace and the target workspace ID.
Request body¶
Replace caller-specific values in the example with actual values.
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"
}'
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
string |
Yes |
The question to answer with data. It cannot be empty. |
|
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.
{
"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"
}
}
Field |
Type |
Description |
|---|---|---|
|
integer |
|
|
object |
The SQL draft. |
The following table expands the data object in the response example. Each row describes one field in that object.
Field |
Type |
Description |
|---|---|---|
|
string |
The data dashboard ID. |
|
string |
A generated, validated, read-only SQL draft. |
|
string |
The suggested chart type. |
Error response¶
{
"code": "ErrDataDashboardSQLDraftValidationFailed",
"message": "The generated SQL did not pass validation"
}
Field |
Type |
Description |
|---|---|---|
|
integer or string |
An error code or status code. |
|
string |
Error message. |
Next steps¶
Use Preview dashboard chart SQL to inspect the result, then create a dashboard chart after confirming it.