Text-to-SQL API

Generate and run SQL from a natural language question

The Text-to-SQL API converts a natural language question into SQL, runs it against the project's datasets, and returns the result. A companion endpoint returns the context block — the dataset schemas, SQL dialect details, and function list that Spice supplies to the model — so the same information can be inspected or reused elsewhere.

A model capable of SQL generation must be configured in the project's spicepod. When the request omits model, Spice uses the single compatible model configured in the spicepod; if none or more than one is configured, the request fails and the model must be named explicitly.

Generate and run SQL

POST https://<project-cname>.spiceai.io/v1/nsql

Headers

HeaderTypeDescription
X-API-KeystringYour project API key
Content-Typestringapplication/json

Body

FieldTypeDefaultDescription
querystring—The natural language question. Required.
modelstring—The model used for SQL generation. When omitted, the single compatible model configured in the spicepod is used.
streambooleanfalseStream the response instead of waiting for completion.
sample_data_enabledbooleanfalseInclude sample data in the context supplied to the model.
datasetsstring[]—Datasets to sample from when building the model context. A sampling hint only — it does not restrict which tables a query can target.
prompt_cache_keystring—A stable key forwarded to the model for provider-specific prompt caching.
curl -X POST "https://<project-cname>.spiceai.io/v1/nsql" \
  -H "X-API-Key: <project-api-key>" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "How many taxi trips were there last month?",
    "sample_data_enabled": true
  }'

Get the SQL generation context

GET https://<project-cname>.spiceai.io/v1/nsql/context

Returns the context block Spice injects into /v1/nsql model requests: the in-scope dataset schemas, the SQL dialect and engine details, and the available function groups. Use it to inspect or tune what the model sees.

Headers

HeaderTypeDescription
X-API-KeystringYour project API key
Acceptstringtext/markdown (used when the header is absent), text/plain, or application/json. Any other value returns a 406.

Query parameters

ParameterTypeDefaultDescription
modelstring—The model whose dataset allowlist is used. When omitted, the single compatible model is used.
include_samplingbooleanfalseInclude distinct-value samples per column. Also accepts sample_data_enabled.
sampling_limitinteger3Maximum rows per distinct-value sample. Maximum 100.
include_examplesbooleaninclude_samplingInclude example rows. Defaults to whatever include_sampling is set to.
examples_limitinteger3Maximum example rows per dataset. Maximum 100.
datasetsstringall in scopeRepeat the parameter to name several datasets. When omitted, every dataset in scope is included.
curl -H "X-API-Key: <project-api-key>" \
  -H "Accept: application/json" \
  "https://<project-cname>.spiceai.io/v1/nsql/context?include_sampling=true&sampling_limit=5"

Response

With Accept: application/json, the body contains:

FieldDescription
contextThe rendered context block injected into /v1/nsql model requests.
instructionsHigh-level SQL generation instructions.
sqlSQL engine and dialect details — engine, version, dialect, parser, notes.
datasetsIn-scope datasets with schema, metadata, relationship, key, and index details.
functionsAvailable function groups, filtered to the project's query context.
samplesSample blocks, present when sampling or examples are requested.

With text/markdown or text/plain, the body is the rendered context block itself.

In the Portal

Both endpoints have an interactive console in the Playground under APIs — NSQL and NSQL Context. The NSQL Context console builds the request from the options above and shows the returned block. See Playground.