Get started with Metering & Billing in Konnect

Incompatible with
on-prem
Minimum Version
Kong Gateway - 3.4
TL;DR

Metering & Billing provides flexible billing and metering for AI and DevTool companies. It also includes real-time insights and usage limit enforcement.

This tutorial will help you get started with Metering & Billing by setting up metering based on Kong Gateway API requests, turn raw API usage into billable product offerings by defining features and pricing plans, and start subscriptions to assign to customers.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    

    Copy and paste these into your terminal to configure your session.

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
    ' | deck gateway apply -
    

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

You need the Metering & Billing Admin role in Konnect to configure Metering & Billing.

This getting-started guide shows how you can meter Kong Gateway API requests and invoice your customers based on their API consumption with Metering & Billing in Konnect.

In this guide, you’ll:

  • Create a Kong Gateway Consumer that you’ll map as a customer
  • Set up a meter for Kong Gateway API requests
  • Create a premium plan based on API usage
  • Start subscriptions for a customer
  • Generate an invoice for a customer on the paid premium plan and see their API usage

The following diagram shows how Kong Gateway entities and Metering & Billing entities are associated:

 
flowchart TB
  subgraph gateway["Kong Gateway"]
    direction LR
        service["example-service"]
        route["example-route"]
        consumer1["Consumer-Kong Air"]
  end
  subgraph metering["Konnect Metering & Billing"]
    direction LR
        meter["Meter"]
    subgraph plan["Premium Plan"]
      direction LR
          feature2["Feature (example-service)"]
          rate-card2["Rate card"]
    end
    subgraph subscription["Premium Subscription"]
      direction LR
          customer1["Customer (Kong Air)"]
    end
  end
    gateway --> metering
    service --> meter
    meter --> feature2
    consumer1 --> customer1
    subscription --> plan

  

Create a Consumer

Before you configure Metering & Billing, you can set up a Consumer, Kong Air. Consumers let you identify the client that’s interacting with Kong Gateway. Later in this guide, you’ll be mapping this Consumer to a customer in Metering & Billing and assigning them to a Premium plan. Doing this allows you map existing Consumers that are already consuming your APIs to customers to make them billable.

You’re going to use key authentication in this tutorial, so the Consumer needs an API key to access any Kong Gateway Services.

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

Enable authentication

Authentication lets you identify a Consumer so you can invoice them as customers after they’ve consumed the resource, in this case, the API request. This example uses the Key Authentication plugin, but you can use any authentication plugin that you prefer.

Enable the plugin globally, which means it applies to all Kong Gateway Services and Routes:

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

Create a meter

In Metering & Billing, meters track and record the consumption of a resource or service over time. This usage can take various forms, such as API requests, compute time seconds, or tokens consumed. Usage metering is commonly event-based to ensure accuracy and data you can audit.

In this guide, you’ll enable API Gateway requests for metering. This will meter API request traffic in Metering & Billing so that you can charge customers for API traffic usage.

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the API Gateway Requests settings, click Enable Gateways.
  3. Select the “quickstart” control plane.
  4. Click Enable 1 Gateways.

Create a feature

Meters collect raw usage data, but features make that data billable. Without a feature, usage is tracked but not invoiced. Now that you’re metering API consumption, you need to associate traffic from the example-service Gateway Service with a feature as something you want to price or govern.

Features are customer-facing, and show up on the invoice for paid plans. Feature examples could include things like flight data requests, GPT-5 input tokens, or available LLM models.

In this guide, you’ll create a feature for the example-service you created in the prerequisites.

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Product Catalog.
  3. Click Create Feature.
  4. In the Name field, enter example-service.
  5. From the Meter dropdown menu, select “API Gateway Requests”.
  6. Click Add group by filter. The group by filter ensures you only bill for traffic to example-service, not all Kong Gateway traffic. This lets you offer different pricing for different APIs.
  7. From the Group by dropdown menu, select “service_name”.
  8. From the Operator dropdown menu, select “Equals”.
  9. From the Value dropdown menu, select “example-service”.
  10. Click Save.

Create a Premium plan

Plans are the core building blocks of your product catalog. They are a collection of rate cards that define the price and access of a feature. Plans can be assigned to customers by starting a subscription.

A rate card describes price and usage limits or access control for a feature or item. Rate cards are made up of the associated feature, price, and optional usage limits or access control for the feature, called entitlements.

In this section, you’ll create a Premium plan that grants paying customers access to the example-service at a rate of 5,000 requests per month:

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Product Catalog.
  3. Click the Plans tab.
  4. Click Create Plan.
  5. In the Name field, enter Premium.
  6. In the Billing cadence dropdown menu, select “1 month”.
  7. Click Save.
  8. Click Add Rate Card.
  9. From the Feature dropdown menu, select “example-service”.
  10. Click Next Step.
  11. From the Pricing model dropdown menu, select “Usage based”.
  12. In the Price per unit field, enter 1.
  13. Click Next Step.
  14. Click Save Rate Card.
  15. Click Publish Plan.

Start a subscription

Customers are the entities who pay for the consumption. In many cases, it’s equal to your Consumer. Here you are going to create a customer and map our Consumer to it.

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Billing.
  3. Click Create Customer.
  4. In the Name field, enter Kong Air.
  5. In the Include usage from dropdown, select “kong-air”.
  6. Click Save.
  7. Click the Subscriptions tab.
  8. Click Create a Subscription.
  9. From the Subscribed Plan dropdown, select “Premium”.
  10. Click Next Step.
  11. Click Create Subscription.

Validate

You can run the following command to test the that the Kong Air Consumer is invoiced correctly:

for _ in {1..6}; do
  curl  -i $KONNECT_PROXY_URL/anything \
       -H "apikey:air-key" 
  echo
done

This will generate six requests. Now, check the invoice that was created in Metering & Billing:

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Billing.
  3. Click the Invoices tab.
  4. Click Kong Air.
  5. Click the Invoicing tab.
  6. Click Preview Invoice.

You’ll see in Lines that example-service is listed and was used six times. In this guide, you’re using the sandbox for invoices. To deploy your subscription in production, configure a payments integration in Metering & Billing > Settings.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

Something wrong?

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!