> For the complete documentation index, see [llms.txt](https://docs.spice.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.spice.ai/docs/portal/playground/nsql-query.md).

# Text-to-SQL

**Text-to-SQL** turns a natural language question into a SQL query, runs it, and shows both the query and its results. The generated SQL stays visible and editable, so it can be checked and corrected before being relied on.

Open it by navigating to an App **Playground** and clicking **Text-to-SQL** in the sidebar.

## Prerequisites

The app must have a model with tools support configured in its spicepod — Text-to-SQL relies on the model's access to dataset schemas to generate a query. When no suitable model is configured, the page prompts to configure one instead.

To add a model, see [Spicepod Configuration](/docs/portal/app-spicepod/spicepod-configuration.md) and set `tools: auto` in the model's `params`, as in the [AI Chat](/docs/portal/playground/ai-chat.md) example.

## Generating a query

1. Select a model. The first available model is selected automatically.
2. Type a question, for example `Show the top 10 trips by distance`.
3. Optionally enable **Sample data** to include sample values from the datasets in the context sent to the model. This is off by default.
4. Click **Generate & Run**.

The generated SQL appears in the editor and runs automatically, with results in the panel below. The SQL can then be edited and re-run, as in the [SQL Query](/docs/portal/playground/sql-query-editor.md) editor.

{% hint style="info" %}
Enabling **Sample data** sends sample values from the app's datasets to the configured model provider. Leave it off when working with sensitive data.
{% endhint %}

## Using the API

The same capability is available outside the Portal through `POST /v1/nsql`, and the context Spice supplies to the model can be retrieved with `GET /v1/nsql/context`. See the [Text-to-SQL API](https://github.com/spicehq/docs/tree/trunk/cloud/api/nsql.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.spice.ai/docs/portal/playground/nsql-query.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
