Integrate the SQL-to-SQL generators in your application.
You can integrate with our platform by using a simple fetch
request:
const response = await fetch('https://api.sqlai.ai/api/public/v1', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authentication: `Bearer ${SECRET_TOKEN}`,
},
body: JSON.stringify({
prompt: 'SELECT * from users;',
mode: 'optimizeSQL',
engine: 'postgres',
}),
});
const { query, error } = await res.json();
if (error) {
// Handle error
}
// Run generated SQL query on your database
const data = await client.query(query);
Send POST
request to:
https://api.sqlai.ai/api/public/v1
Options when requesting a SQL generation:
prompt
(required): The SQL query.mode
(required): "optimizeSQL", "fixSQL", "simplifySQL", "formatSQL", "databaseIndex".engine
: The database engine to use:
mysql
postgres
oracle
mssql
snowflake
mariadb
sqlite
bigquery
mongodb
sql
(native SQL is default)engineVersion
: Database engine version number/code as string, e.g. "16"
, "5.7"
, "21c"
.dataSourceId
: Use a data source when generating SQL, e.g. database schema hosted on SQLAI.ai.dataSourceRaw
: Use included data source to generate SQL. CSV format recommended for SQL databases with: table_name, table_column, table_type
(table type can be left out if needed, AI can usually infer it).The above example will return this response:
{
"query": "SELECT actor_id, first_name, last_name, last_update\nFROM actor\nWHERE actor_id = 13;",
"content": "```sql-answer\nSELECT actor_id, first_name, last_name, last_update\nFROM actor\nWHERE actor_id = 13;\n```",
"queries": {
"markdown": ["```sql\nSELECT actor_id, first_name, last_name, last_update\nFROM actor\nWHERE actor_id = 13;\n```"],
"clean": ["SELECT actor_id, first_name, last_name, last_update\nFROM actor\nWHERE actor_id = 13;"]
},
"meta": { "usage": { "prompt_tokens": 598, "completion_tokens": 28, "total_tokens": 626 }, "finish_reason": "stop" }
}
The response object type:
type ResponseBody = {
query: string; // Generated SQL query stripped of formatting
content: string; // Complete Markdown generation
queries: {
markdown: string[]; // All SQL queries with formatting
clean: string[]; // All SQL queries stripped of formatting
};
meta: {
usage: { completion_tokens: number; prompt_tokens: number; total_tokens: number };
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call';
};
error: string | undefined;
};
If you have any questions or suggestions, please reach out.