Story: counting the API calls of Acme Corp by region
Your company sells access to a messaging API. It plans to charge a different price for the API calls of each region. Your billing team already meters the API calls of Acme Corp in the usage-event collectionAPI calls. Each usage event now also reports the region of its calls. The billing team needs one number of calls for each region. The visual builder gives one number for each customer and period, so it cannot give a number for each region.
The billing team starts a billable metric in the visual builder. It then moves the billable metric to the SQL builder, and changes the SQL query to give one number for each region. On the way, it puts these concepts into practice:
- The visual builder, and the SQL query that it builds
- The SQL builder, with the
quantitycolumn and a dimension - Zenskar SQL variables
- Results for each customer and period
The company, the customer, and the numbers are fictional.
Prerequisites
- A role with the Can Create Billable Metric and Can Read Billable Metric permissions.
- The usage-event collection
API calls, with the data fieldregion. See Add a data field. - The three usage events of Acme Corp in November 2026. See Send usage events through the API.
- A customer with the external ID
acme-corp.
Start the billable metric in the visual builder
- Go to Usage > Billable Metrics.
-
Click ADD NEW BILLABLE METRIC. The Add New Billable Metric screen opens with VISUAL BUILDER selected.
The billing team could write the whole SQL query in the SQL builder. It starts in the visual builder, because the visual builder writes the parts of the query that every billable metric of
API callsneeds: the table, the customer, and the period. The billing team then changes only what the visual builder cannot give. -
Under Select tables & filters, in Table Name, select
api_callsunder Usage Event. Click NEXT. -
Under Map customer, keep
customer_idin Your customer column and external_id in Zenskar customer column. Click NEXT. -
Under Set date field, keep
timestampin Column. Click NEXT. -
Under Set Billable Metric over, in Select calculation type, select SUM. In Select column to calculate over, select
calls. Zenskar converts each setting of the visual builder into a part of an SQL query:
Move the billable metric to the SQL builder
-
Click SQL BUILDER. The editor shows the SQL query of the settings, read-only:
{{customer.external_id}},{{start_date}}, and{{end_date}}are Zenskar SQL variables. Each time Zenskar runs the billable metric, it fills them in with the external ID of the customer and the first and last days of the period. The query returns one value, namedquantity, so it gives one number for each customer and period. - Click EDIT. The SQL Builder Edit Access window opens. In the SQL builder, the billing team changes the query itself. After the move, the settings of the visual builder no longer apply to the billable metric.
-
Click YES. The editor becomes editable, and VISUAL BUILDER is no longer available.
The move is one way: after the billable metric is saved as a query, it cannot return to the visual builder. Move a billable metric only when it needs a query that the visual builder cannot build, such as one with dimensions.
Add the region as a dimension
-
In the editor, after
SELECT, add the line"data.region" AS "region",. The data fieldregionis the columndata.regionof the tableraw_metric_api_calls. See Usage events as rows and columns. The new line adds a column namedregionto the result of the query. -
Replace the lines from
LIMITto the end of the query withGROUP BY "data.region". The query now reads:GROUP BY "data.region"gives one row for each region, with the sum of the calls of that region. The query of the visual builder ended withLIMIT 10, which keeps at most 10 rows. The query now returns two columns. In the SQL builder, the columnquantityholds the number, and each other column is a dimension. The billable metric therefore gives one number for each combination of dimension values. Here the only dimension isregion, so each combination is one region, such aseu-west. The Zenskar SQL variables stay in the query. The billable metric still gives its result for one customer and one period at a time.
Preview the billable metric
Before saving, the billing team checks the result for Acme Corp in November.- Under Query preview, in Search By Customer Name, External ID, select Acme Corp.
-
In Billing Period, select November 1, 2026, as the first day and November 30, 2026, as the last day.
The preview fills in the Zenskar SQL variables with the external ID of Acme Corp,
acme-corp, and with the first and last days of November. See Results for each customer and period. -
Click RUN QUERY. The table shows one row for each region:
Each row is one combination, with its number in
quantity. The number of each region is the sum of thecallsvalues of its usage events:A product can then charge a combination price for each region.
Save the billable metric
The result for Acme Corp matches its usage events, so the billing team saves the billable metric.- Click CREATE BILLABLE METRIC. The Choose Billable Metric Name window opens.
-
Enter
API calls by region. -
Click CREATE BILLABLE METRIC. The Billable Metrics list opens and shows
API calls by region. Zenskar saves the billable metric as its SQL query. Each time Zenskar runs the billable metric, it runs the query for one customer and one period, and gives one number for each region.
Summary: what your billing team did
Your billing team started the billable metricAPI calls by region in the visual builder, as the sum of calls. It moved the billable metric to the SQL builder. It then added region as a dimension, and replaced the LIMIT of the query with GROUP BY to keep every region. The preview gave 500 calls in eu-west and 400 calls in us-east for Acme Corp in November.
See Create a billable metric.
See Move a billable metric to the SQL builder.