Skip to main content

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 collection API 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 story uses these values: 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 field region. 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

  1. Go to Usage > Billable Metrics.
  2. 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 calls needs: the table, the customer, and the period. The billing team then changes only what the visual builder cannot give.
  3. Under Select tables & filters, in Table Name, select api_calls under Usage Event. Click NEXT.
  4. Under Map customer, keep customer_id in Your customer column and external_id in Zenskar customer column. Click NEXT.
  5. Under Set date field, keep timestamp in Column. Click NEXT.
  6. 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

  1. 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, named quantity, so it gives one number for each customer and period.
  2. 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.
  3. 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

  1. In the editor, after SELECT, add the line "data.region" AS "region",. The data field region is the column data.region of the table raw_metric_api_calls. See Usage events as rows and columns. The new line adds a column named region to the result of the query.
  2. Replace the lines from LIMIT to the end of the query with GROUP 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 with LIMIT 10, which keeps at most 10 rows.
    Zenskar runs the query as written, including its LIMIT. A query with a dimension and LIMIT 10 gives at most 10 combinations of dimension values, without a message. Remove the limit when you add a dimension.
    The query now returns two columns. In the SQL builder, the column quantity holds 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 is region, so each combination is one region, such as eu-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.
  1. Under Query preview, in Search By Customer Name, External ID, select Acme Corp.
  2. 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.
  3. 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 the calls values 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.
  1. Click CREATE BILLABLE METRIC. The Choose Billable Metric Name window opens.
  2. Enter API calls by region.
  3. 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 metric API 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.