SQL
When the query types don't answer your question, run your own SQL across one or more websites in the same organization. You can join analytics tables, group results, and get up to 50,000 rows back. To try a query before you put it in code, ask it through the MCP server from your AI assistant.
Authentication
Send an API key in the x-api-key header. The key needs the read:data scope for every website in the request. Keep the key on your server.
Run a query
POST https://api.databuddy.cc/sql/query
curl https://api.databuddy.cc/sql/query \
-H "x-api-key: $DATABUDDY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"websiteId": "YOUR_WEBSITE_ID",
"sql": "SELECT path, count() AS pageviews, uniq(anonymous_id) AS visitors FROM analytics.events WHERE event_name = '\''screen_view'\'' AND time >= {from:DateTime} GROUP BY path ORDER BY pageviews DESC",
"params": { "from": "2026-11-01 00:00:00" },
"limit": 1000
}'The response:
{
"columns": [
{ "name": "path", "type": "String" },
{ "name": "pageviews", "type": "UInt64" },
{ "name": "visitors", "type": "UInt64" }
],
"rows": [
{ "path": "/", "pageviews": "1204", "visitors": "811" },
{ "path": "/pricing", "pageviews": "377", "visitors": "290" }
],
"truncated": false,
"elapsedMs": 38
}Every query only sees the websites you select. To compare sites, include the website column in your results. For example, analytics.events uses client_id.
{
"websiteIds": ["FIRST_WEBSITE_ID", "SECOND_WEBSITE_ID"],
"sql": "SELECT client_id, count() AS pageviews FROM analytics.events WHERE event_name = 'screen_view' GROUP BY client_id"
}When joining tables across sites, include the website columns in your join if the matches should come from the same site. Use GET /sql/schema or the agent's schema tool to find those columns.
Tables you can read
GET https://api.databuddy.cc/sql/schema lists every table and column SQL can read, with each column's type and notes on how to count it. Any API key can read the list; it is the same for every plan, and a query still needs read:data and a plan that includes the table.
The SQL tables page shows the same list with every table's notes.
curl https://api.databuddy.cc/sql/schema -H "x-api-key: $DATABUDDY_API_KEY"Most questions start from analytics.events. Keep these in mind:
Limits
Errors
Errors return JSON with a code and a message that says what to change.
{
"defined": true,
"code": "BAD_REQUEST",
"status": 400,
"message": "DQL syntax is invalid."
}How is this guide?