Track API requests by Consumer with Metering & Billing

TL;DR

Configure the Metering & Billing plugin on a Gateway Service with meter_api_requests: true and subject.look_up_value_in: consumer. The plugin emits one CloudEvent per API request to the Konnect ingest endpoint, with the authenticated Consumer set as the event subject. Verify that events appear in the Konnect UI under Metering & Billing > Events.

Prerequisites

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

    curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial, install decK version 1.43 or later.

This guide uses deck gateway apply, which directly applies entity configuration to your Gateway instance. We recommend upgrading your decK installation to take advantage of this tool.

You can check your current decK version with deck version.

For this tutorial, you’ll need Kong Gateway entities, like Gateway Services and Routes, pre-configured. These entities are essential for Kong Gateway to function but installing them isn’t the focus of this guide. Follow these steps to pre-configure them:

  1. Run the following command:

    echo '
    _format_version: "3.0"
    services:
      - name: example-service
        url: http://httpbin.konghq.com/anything
    routes:
      - name: example-route
        paths:
        - "/anything"
        service:
          name: example-service
        protocols:
        - http
        - https
    ' | deck gateway apply -
    

To learn more about entities, you can read our entities documentation.

You need a Konnect system account token (spat_) with the Ingest role for Metering. This token authenticates the plugin when it sends events to the Konnect ingest endpoint.

Export your system account token:

export DECK_AUTH_TOKEN='YOUR SPAT TOKEN'

For more information, see system accounts and access tokens.

You can track per-consumer API usage without modifying your upstream services by using the Metering & Billing plugin. The plugin emits a CloudEvent for every API request that passes through Kong Gateway, using the authenticated Consumer as the billable subject.

In this guide, you’ll:

  • Create a Consumer with a Key Auth credential
  • Configure Key Authentication on the example Service
  • Configure the Metering & Billing plugin to emit usage events to Konnect
  • Verify that usage events appear in the Konnect UI

Create a Consumer

Consumers can represent the clients that call your APIs. The Metering & Billing plugin uses the Consumer’s username as the subject field in each CloudEvent it emits, which enables per-client billing downstream.

echo '
_format_version: "3.0"
consumers:
  - username: alice
    keyauth_credentials:
    - key: alice-key
' | deck gateway apply -

Configure Key Authentication

This Metering & Billing plugin configuration requires an authenticated Consumer to be present on each request. Enable the Key Auth plugin on the example Service:

echo '
_format_version: "3.0"
plugins:
  - name: key-auth
    service: example-service
    config:
      key_names:
      - apikey
' | deck gateway apply -

Configure the Metering & Billing plugin

Enable the Metering & Billing plugin on the example Service. Setting meter_api_requests: true tells the plugin to emit one event per request. Setting subject.look_up_value_in: consumer populates the subject field in each CloudEvent with the authenticated Consumer’s username.

echo '
_format_version: "3.0"
plugins:
  - name: metering-and-billing
    service: example-service
    config:
      ingest_endpoint: https://us.api.konghq.com/v3/openmeter/events
      api_token: "${{ env "DECK_AUTH_TOKEN" }}"
      meter_api_requests: true
      meter_ai_token_usage: false
      subject:
        look_up_value_in: consumer
' | deck gateway apply -

Validate

Send a few requests through the example Service using the alice Consumer’s API key:

for _ in {1..3}; do
  curl -i http://localhost:8000/anything \
       -H "apikey: alice-key"
  echo
done

This sends three requests, each emitting a CloudEvent with subject: alice to Konnect.

Now verify that the events were received:

  1. In the Konnect sidebar, click Metering & Billing.
  2. Click the Events tab.

You’ll see three events listed, each with subject: alice, one for each request that passed through Kong Gateway.

You’ll also see an error message like no customer found for event subject: consumer associated with the event. This is expected since we’re only tracking API usage. If you want to meter and bill Consumer’s usage, see Metering, Customers and usage attribution, and Billing, invoicing, and subscriptions.

Cleanup

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!