API reference

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
  }'
FieldMeaning
websiteIdThe ID of one website. Pass either websiteId or websiteIds.
websiteIdsA list of 1 to 50 website IDs in the same organization.
sqlRequired. One SELECT or WITH query in ClickHouse SQL, up to 50 KB.
paramsValues for {name:Type} placeholders, such as {from:DateTime} or {paths:Array(String)}. For a single website, Databuddy supplies websiteId and websiteDomain.
limitRows to return, from 1 to 50,000. Defaults to 10,000.

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
}
  • columns lists each result column with its ClickHouse type.
  • rows holds one object per row, keyed by column name. 64-bit integers such as count() and uniq() results, and decimals, are strings, so values too large for a JavaScript number stay exact. Parse them with BigInt or Number as you need.
  • truncated is true when the query had more rows than limit. Aggregate, add ORDER BY and LIMIT, or raise limit to get the rest.
  • elapsedMs is how long the query took.

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:

  • Page views are rows with event_name = 'screen_view'.
  • Each table's notes on the SQL tables page name the column that dates its rows: time on analytics.events, for example, and timestamp on most others.
  • Count visitors with uniq(anonymous_id).
  • Some tables, such as analytics.error_spans, need a plan that includes their feature.

Limits

  • Write only one SELECT or WITH query, without comments, comma joins (FROM a, b), or table functions.
  • A query stops after 20 seconds, after reading 5 GB or 100 million rows, or when its result passes 10 MB. Narrow the date range, select fewer columns, or aggregate to stay under them.
  • Each API key can run 60 queries a minute.
  • SQL from scripts and agents shares an organization budget for query count, running time, and result size. The budget resets each minute.

Errors

Errors return JSON with a code and a message that says what to change.

StatusCodeWhen
400BAD_REQUESTThe SQL is invalid, reads a table or column you can't read, or hit a limit. message says which.
401UNAUTHORIZEDThe API key is missing, revoked, or expired.
402FEATURE_UNAVAILABLEThe query reads a table your plan doesn't include. data.feature names the feature and data.requiredPlan the plan that has it.
403FORBIDDENThe key lacks read:data for this website, or the website belongs to another organization.
404NOT_FOUNDThe website doesn't exist.
429RATE_LIMITEDThe API key or organization reached its SQL limit. Wait the number of seconds in the Retry-After header before retrying.
503SERVICE_UNAVAILABLEToo many queries are running right now. Retry after the seconds in the Retry-After header.
{
  "defined": true,
  "code": "BAD_REQUEST",
  "status": 400,
  "message": "DQL syntax is invalid."
}

How is this guide?