Skip to main content
The Query API returns your events as JSON over HTTPS. Use it to export data, to build your own dashboard, or to feed events into another tool. The dashboard at https://grainql.com/dashboard covers one-time questions and live monitoring without code. This page is the task guide. The field tables, error tables, and limits are on the reference pages, starting with Query API. Before you start, make sure that you have:
  • A tenant on the Builder plan or higher. The Free plan has no Query API.
  • The tenant alias from the dashboard. It is not the tenant UUID.
  • An HTTP client. Analytics Web has no Query API methods.

Get an API key

  1. Open https://grainql.com/dashboard/settings, then Authentication.
  2. Click Generate New Secret.
  3. Enter a name, for example Query API key.
  4. Select the Query API permission.
  5. Copy the secret. The dashboard shows it one time.
  6. Store the secret in an environment variable named GRAIN_API_KEY.
The key is independent of the Analytics Web authStrategy. It works when the SDK uses NONE, SERVER_SIDE, or JWT. Read Authentication.

Your first query

Every request sends the key in the X-API-Key header. This request reads ten page views from January 2024:
Every event has the same six fields. properties holds what track sent. userId holds what identify or setUserId set. Read Query events for the field table and the paging rules. To select events, add a filterSet. This request reads the events of one user:
A filter can address properties.<name>, nested paths such as properties.user.plan, eventName, userId, and eventTs. Read Filters for the ten operators.

Count events

When you need a number and not the events, call the count endpoint. It takes the same body without pagination:
Read Count events.

List event names

To find out which event names the tenant holds, call the list endpoint:
Read List event names.

Common tasks

Export a date range

Page through the query endpoint with size 1000, then write the events to a file. This Node.js script exports January 2024:
The file holds one array of event objects in the shape of the query response. Each page is one request against the rate limit, so an export of 12,543 events takes 13 requests. Read Data export for a full example.

A dashboard of totals

Send one count per number, in parallel, then divide on the client:
Three counts are three requests. On the Builder plan, three requests take more than one minute. Cache the numbers, and refresh them on a timer. A dashboard is only as good as the event names behind it. Read Tracking checklist. For a full example, read Custom dashboard.

A funnel check

Count each step of the funnel with the same date range, then compare the numbers:
The conversion from the signup page to an account is 611 divided by 4210, or 14.5 percent. The dashboard shows the same funnel without code. Read Tracks.

Limits

The rate limit applies per API key, and the plan sets it. Custom plans with higher limits exist. Rules for the rate limit:
  1. Count the requests of a task before you run it. One page and one count are one request each.
  2. Queue requests in one place. Do not let several parts of an application call the API at the same time.
  3. If the API returns 429, wait Retry-After seconds, then retry one time.
  4. Cache a number that a dashboard shows often.
Read Query API for the rate limit headers and the error table.

Keep the key on the server

Do not put the API key in browser code. Anyone who reads the page source then holds the key.
  1. Store the key in an environment variable, and add the .env file to .gitignore.
  2. When a browser needs data, call the Query API from your own backend route, and return the result to the browser.
  3. To rotate a key, create a new key in the dashboard, deploy the application with the new key, then make sure that the application works, then revoke the old key.