Quick Start
This guide will help you make your first API call to Wren AI. We'll walk through a simple example of generating a SQL query from a natural language question.
Prerequisites
- A Wren AI Cloud account
- An API key (see Manage API Keys)
Basic Example
Here's a simple example of generating a SQL query using curl:
curl -X POST 'https://cloud.getwren.ai/api/v1/generate_sql' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"projectId": 1,
"question": "Show me all customers"
}'
Response:
{
"id": "1fbc0d64-1c58-45b2-a990-9183bbbcf913",
"sql": "SELECT * FROM \"olist_customers_dataset\"",
"threadId": "9c537507-9cec-46ed-b877-07bfa6322bed"
}
Follow-up Questions
You can maintain conversation context using the threadId
from the previous response:
curl -X POST 'https://cloud.getwren.ai/api/v1/generate_sql' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"projectId": 1,
"question": "list top 10 only",
"threadId": "9c537507-9cec-46ed-b877-07bfa6322bed"
}'
Handling Questions that can't be translated to SQL
If a user asks a question that isn't related to your data schema or can't be translated to SQL, you'll receive an error response:
curl -X POST 'https://cloud.getwren.ai/api/v1/generate_sql' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"projectId": 1,
"question": "What can you do?"
}'
Response:
{
"id": "75c13d09-6f86-4e79-a00e-a4f85f73f2d7",
"code": "NON_SQL_QUERY",
"error": "User asks about Wren AI's features and capabilities, unrelated to database schema.",
"explanationQueryId": "71b016c5-42bb-4897-82d6-46f9b0bf7d94"
}
You have two options to handle this:
- Use the error message to generate your own response to users
- Stream Wren AI's explanation to users using the
stream_explanation
endpoint. Check out the API Reference for more details.
Next Steps
- Explore chart generation capabilities
- Check your API usage history
For complete API documentation, visit our API Reference.