SQLAI.ai

Public API

Generate SQL and NoSQL queries within your application. This documentation applies to v2.

Table of contents

Prerequisites

To integrate with our public API, you need:

Endpoint

Send a POST request to: https://api.sqlai.ai/api/public/v2.

Options

Body options:

  • prompt (required): Describe what you want to do in everyday language.
  • engine (required): Choose from mysql, postgres, oracle, mssql, snowflake, mariadb, sqlite, bigquery, mongodb.
  • mode: textToSQL, explainSQL (default: textToSQL)
  • dataSourceId: The dataSourceId can be obtained from the list of data sources.
  • dataSource: Provide a custom database schema for query generation.

Note: Any data source rules set for the data source will be included. These can be set on the data source.

Retrieve database schema by running the query:

The database schema has the following format (case insensitive):

            
[ { "table": "", "column": "", "type": "", "meta": "" // Optional, e.g., foreign key relations } ]

Recommendation: Using this format enables SQLAI.ai to handle very large databases without exhausting the context window or diminishing performance/accuracy.

The dataSource can also be a CSV-formatted string, but note that it doesn't handle larger database schemas as efficiently:

            
table_name,column_name,type table_name,column_name1,type table_name,column_name1,type

If you prefer a custom database notation, the dataSource accepts any string as a database schema format. However, it is limited to a maximum of 4,000 tokens (~15,000 characters) to ensure performance and accuracy.

Response

The JSON response object:

            
{ "query": "SELECT film.title, film.last_update FROM...", "content": "", // Markdown for `explainSQL` mode "error": "" }

Example

Retrieve a text-to-SQL query with a custom database schema:

            
const response = await fetch('https://api.sqlai.ai/api/public/v2', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer SECRET_TOKEN` }, body: JSON.stringify({ prompt: 'Get actor with id 13 and his recent movies', engine: 'postgres', dataSource: [ { table: 'actor', column: 'actor_id', type: 'smallint', }, { table: 'actor', column: 'first_name', type: 'varchar', }, { table: 'actor', column: 'last_name', type: 'varchar', }, // ... limited for the sake of brevity ], }), }); const { query } = await response.json(); console.log(query); // SELECT film.title, film.last_update FROM actor JOIN film_actor ON actor.actor_id = film_actor.actor_id JOIN film ON film_actor.film_id = film.film_id WHERE actor.actor_id = 13 ORDER BY film.last_update DESC;

If an error occurs, it will be returned as a string in the error field. If using the explainSQL mode, the explanation will be returned in content field as Markdown.

Questions

If you have any questions regarding technical implementation, want to use generators other than those currently available, or have inquiries about subscriptions, please reach out.

Old documentation

Links to the deprecated but still running API: