API Integration
- Published on
Our public API is useful to build your own integrations, create your applications, white-label, etc.
Important: The API is only available for Pro plan users and above.
Each API request consumes credits from your plan's quota in the same way as using the application interface would
Authentication
All API requests require authentication using your API key in the Authorization header:
Authorization: Bearer YOUR_API_KEY
Generate SQL
Convert natural language to SQL queries
Parameters
prompt
(Required): The natural language description of the query you want to generatetype
(Optional): Database type (postgres, mysql, etc). Defaults to general SQLschema
(Optional): Database schema to use for contextconnectionID
(Optional): ID of a saved connection to use its schema
Example Request
curl -X POST "https://app2.text2sql.ai/api/external/generate-sql" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Find all items with id 1",
"type": "postgres",
"schema": "items (id: int, name: text)",
"connectionID": "optional-connection-id"
}'
Response
{
"output": "SELECT * FROM items WHERE id = 1;"
}
Fix SQL
Fix SQL query errors or improve query efficiency
Parameters
query
(Required): The SQL query to fixerror
(Optional): The error message receivedtype
(Optional): Database type (postgres, mysql, etc). Defaults to general SQLschema
(Optional): Database schema to use for contextconnectionID
(Optional): ID of a saved connection to use its schema
Example Request
curl -X POST "https://app2.text2sql.ai/api/external/fix-sql" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": "SELECT * FROM users WHERE id = 1;",
"error": "relation \"users\" does not exist",
"type": "postgres",
"schema": "items (id: int, name: text)",
"connectionID": "optional-connection-id"
}'
Response
{
"output": "SELECT * FROM items WHERE id = 1;"
}
Explain SQL
Get natural language explanations of SQL queries
Parameters
query
(Required): The SQL query to explainconnectionID
(Optional): ID of a saved connection to use its schema
Example Request
curl -X POST "https://app2.text2sql.ai/api/external/explain-sql" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": "SELECT * FROM items WHERE id = 1;",
"connectionID": "optional-connection-id"
}'
Response
{
"output": "This query retrieves all columns from the items table where the id equals 1."
}
Usage
Get current API usage statistics
Example Request
curl "https://app2.text2sql.ai/api/external/usage" \
-H "Authorization: Bearer YOUR_API_KEY"
Response
{
"used": 50,
"remaining": 950,
"limit": 1000,
"billing_period_start": "2024-03-01T00:00:00.000Z",
"billing_period_end": "2024-04-01T00:00:00.000Z"
}