Skip to main content

SQL Endpoints

These endpoints handle SQL generation from natural language and direct SQL execution.

Generate SQL

Convert a natural language question into a SQL query using the deployed semantic model.

POST /api/v1/generate_sql

Request Body

FieldTypeRequiredDescription
questionstringYesNatural language question
threadIdstringNoThread ID for conversation context
languagestringNoLanguage for AI responses (e.g. en, zh-TW). Defaults to project setting.
returnSqlDialectbooleanNoIf true, returns SQL in the native database dialect. Default: false

Example Request

curl -X POST https://your-host/api/v1/generate_sql \
-H "Authorization: Bearer osk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"question": "What are the top 10 customers by total revenue?",
"threadId": "9c537507-9cec-46ed-b877-07bfa6322bed"
}'

Success Response (200)

{
"id": "1fbc0d64-1c58-45b2-a990-9183bbbcf913",
"sql": "SELECT c.customer_name, SUM(o.total_amount) AS revenue FROM customers c JOIN orders o ON c.id = o.customer_id GROUP BY c.customer_name ORDER BY revenue DESC LIMIT 10",
"threadId": "9c537507-9cec-46ed-b877-07bfa6322bed"
}
FieldTypeDescription
idstringUnique response identifier
sqlstringGenerated SQL query
threadIdstringThread ID (existing or newly created)

Error Response (400)

When the question cannot be answered with SQL:

{
"id": "75c13d09-6f86-4e79-a00e-a4f85f73f2d7",
"code": "NON_SQL_QUERY",
"error": "User asks about features and capabilities, unrelated to database schema.",
"explanationQueryId": "71b016c5-42bb-4897-82d6-46f9b0bf7d94"
}

The explanationQueryId can be used with the stream explanation endpoint to get a detailed response for non-SQL questions.

Timeout

This endpoint polls the AI service internally with a 180-second timeout. If generation exceeds this limit, a timeout error is returned.


Run SQL

Execute a SQL query against your connected data source and return structured results.

POST /api/v1/run_sql

Request Body

FieldTypeRequiredDescription
sqlstringYesSQL query to execute
threadIdstringNoThread ID for conversation context
limitintegerNoMaximum rows to return. Default: 1000

Example Request

curl -X POST https://your-host/api/v1/run_sql \
-H "Authorization: Bearer osk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"sql": "SELECT customer_name, total_revenue FROM customers ORDER BY total_revenue DESC LIMIT 5",
"limit": 100
}'

Success Response (200)

{
"id": "09d46224-0068-4ca3-bce4-f1fc85093eb6",
"records": [
{
"customer_name": "Acme Corp",
"total_revenue": 152340.50
},
{
"customer_name": "Global Industries",
"total_revenue": 98200.00
}
],
"columns": [
{
"name": "customer_name",
"type": "VARCHAR"
},
{
"name": "total_revenue",
"type": "DECIMAL"
}
],
"threadId": "503a8ca5-8171-43b5-b45b-86de2849467b",
"totalRows": 2
}
FieldTypeDescription
idstringUnique response identifier
recordsarrayArray of row objects
columnsarrayColumn metadata (name, type, notNull, properties)
threadIdstringThread ID (existing or newly created)
totalRowsintegerTotal rows returned

Column Metadata

Each entry in the columns array:

FieldTypeDescription
namestringColumn name
typestringData type (e.g. VARCHAR, INTEGER, DECIMAL, TIMESTAMP)
notNullbooleanWhether the column disallows nulls
propertiesobjectAdditional column properties