Custom SQL

Generate SQL Query

Turns a plain-language question into a ClickHouse query for the Run SQL Query endpoint. The query is returned, not run.

POST /api/organizations/:organizationId/analytics/query/generate

Uses an AI model to write a scoped_events query from a plain-language prompt. The generated SQL is checked against the same rules as Run SQL Query and returned; nothing is executed. Pass currentQuery and history to refine an earlier query.

Permissions: the viewer role or above in the organization. API keys need the sql:read scope.

Rate limit: 20 requests per minute per user (per organization for organization API keys). Self-hosted instances need an OpenRouter API key configured.

Path Parameters

Prop

Type

Request Body

Prop

Type

Response

Prop

Type

Error Responses

Errors use the standard body { error, code, message, resolution }.

Prop

Type

Request
curl -X POST "https://app.rybbit.io/api/organizations/org_123/analytics/query/generate" \
  -H "Authorization: Bearer your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Top 10 referrers last month",
    "currentSiteId": 123
  }'
Request
const response = await fetch(
  'https://app.rybbit.io/api/organizations/org_123/analytics/query/generate',
  {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer your_api_key_here',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      prompt: 'Top 10 referrers last month',
      currentSiteId: 123
    })
  }
);

const data = await response.json();
Request
import requests

response = requests.post(
    'https://app.rybbit.io/api/organizations/org_123/analytics/query/generate',
    json={
        'prompt': 'Top 10 referrers last month',
        'currentSiteId': 123
    },
    headers={
        'Authorization': 'Bearer your_api_key_here'
    }
)

data = response.json()
Request
$body = [
    'prompt' => 'Top 10 referrers last month',
    'currentSiteId' => 123
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://app.rybbit.io/api/organizations/org_123/analytics/query/generate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer your_api_key_here',
    'Content-Type: application/json'
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
Request
require 'net/http'
require 'json'

uri = URI('https://app.rybbit.io/api/organizations/org_123/analytics/query/generate')
request = Net::HTTP::Post.new(uri)
request['Authorization'] = 'Bearer your_api_key_here'
request['Content-Type'] = 'application/json'
request.body = {
  prompt: 'Top 10 referrers last month',
  currentSiteId: 123
}.to_json

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
  http.request(request)
end

data = JSON.parse(response.body)
Request
body := map[string]interface{}{
    "prompt": "Top 10 referrers last month",
    "currentSiteId": 123,
}
jsonBody, _ := json.Marshal(body)

req, _ := http.NewRequest("POST", "https://app.rybbit.io/api/organizations/org_123/analytics/query/generate", bytes.NewBuffer(jsonBody))
req.Header.Set("Authorization", "Bearer your_api_key_here")
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()

var data map[string]interface{}
json.NewDecoder(resp.Body).Decode(&data)
Request
let body = serde_json::json!({
  "prompt": "Top 10 referrers last month",
  "currentSiteId": 123
});

let client = reqwest::Client::new();
let res = client
    .post("https://app.rybbit.io/api/organizations/org_123/analytics/query/generate")
    .header("Authorization", "Bearer your_api_key_here")
    .json(&body)
    .send()
    .await?;

let data: serde_json::Value = res.json().await?;
Request
String json = """
{
  "prompt": "Top 10 referrers last month",
  "currentSiteId": 123
}
""";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://app.rybbit.io/api/organizations/org_123/analytics/query/generate"))
    .header("Authorization", "Bearer your_api_key_here")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(json))
    .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Request
var body = new
{
    prompt = "Top 10 referrers last month",
    currentSiteId = 123
};

using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer your_api_key_here");

var content = new StringContent(
    JsonSerializer.Serialize(body),
    Encoding.UTF8,
    "application/json");

var response = await client.PostAsync("https://app.rybbit.io/api/organizations/org_123/analytics/query/generate", content);
var data = await response.Content.ReadAsStringAsync();
Response
{
  "query": "SELECT referrer, count() AS events FROM scoped_events WHERE site_id = 123 AND referrer != '' AND timestamp >= toStartOfMonth(now() - INTERVAL 1 MONTH) AND timestamp < toStartOfMonth(now()) GROUP BY referrer ORDER BY events DESC LIMIT 10"
}