---
title: "SQL query generator"
method: POST
path: "/api/v1/ai/sql-from-question"
tags: ["ai"]
---

# SQL query generator

`POST /api/v1/ai/sql-from-question`

## Request

- Base URL: `https://toolforte.com`
- URL: `https://toolforte.com/api/v1/ai/sql-from-question`
- Auth: one of:
  - HTTP bearer
  - none

Write a SQL query from a plain-language question, for PostgreSQL, MySQL, SQLite, SQL Server or BigQuery. Give it the table layout (CREATE TABLE statements or a description of the columns) and the question; it returns the query, an explanation, the assumptions it had to make about your schema, and warnings such as a missing index or an ambiguous join. Read-only by default: it will not write DELETE or UPDATE unless the question asks for a change, and it says so when it does. Costs 10 credits per call with an API key or a signed-in session, drawn from the same monthly balance as REST calls (1 credit) and renders (25). Without either, a small free demo allowance per day. Also available on the MCP server as generate_sql.

## Request body

- object
  - `question` string, required: The question in plain words, for example 'customers who ordered twice in March but not since'.
  - `dialect` 'postgres' | 'mysql' | 'sqlite' | 'sqlserver' | 'bigquery', required: The database the query must run on.
  - `schema` string: Optional: CREATE TABLE statements or a description of the tables and columns.

## Response `200`

The tool's answer plus metering

- object
  - `ok` boolean
  - `data` object
    - `sql` string, required
    - `explanation` string, required
    - `assumptions` string[], required
    - `warnings` string[], required
    - `modifiesData` boolean, required

## Other responses

- `400`: Missing or invalid request fields
- `429`: Daily demo allowance or monthly credits used up
- `502`: The model did not return a usable answer
- `503`: AI temporarily unavailable

---

[API](https://skmtc.dev/toolforte/apis/toolforte-api.md) · [All operations](https://skmtc.dev/toolforte/apis/toolforte-api/llms.txt) · [OpenAPI document](https://skmtc.dev/toolforte/apis/toolforte-api/revisions/4d814d484dfc?raw)
