In this article, you will learn how to build a ‘pay-as-you-go’ billing system.
This template is suitable for companies whose pricing fully depends on usage, such as cloud service providers and API companies, that only charge their customers for the resources they consume.
Pricing structure
For one of its products, Algolia Search, the platform offers its customers to subscribe for free and only pay based on usage.
Model Search API Monthly price $1.50 / 1,000 requests Free usage (each month) 10,000 requests
Although users don’t need to subscribe to access the platform, Algolia offers its customers discounts based on volume and commitment (rates available upon request).
Get started
Set up Lago
cURL
Python
Ruby
Javascript
Go
LAGO_URL = "https://api.getlago.com"
API_KEY = "__API_KEY__"
Create billable metrics
Create billable metrics to track request usage for Search and Recommend APIs.
Set the aggregation_type
to sum_agg
to sum all request volumes
Set the field_name
to search_requests_volume
for Search API tracking
Set the recurring
to false
for metered billing
cURL
Python
Ruby
JavaScript
Go
curl --location --request POST " $LAGO_URL /api/v1/billable_metrics" \
--header 'Authorization: Bearer __API_KEY__' \
--header 'Content-Type: application/json' \
--data-raw '{
"billable_metric": {
"name": "Search API Requests",
"code": "__BILLABLE_METRIC_CODE__",
"description": "Search API request volume",
"aggregation_type": "sum_agg",
"field_name": "search_requests_volume",
"recurring": false
}
}'
Create a plan
Create a plan to price packages of requests used.
Set the amount_cents
to 0
since there is no subscription fee
Set the charges
to use package
pricing model with Algolia’s rates
Configure free_units
of 10,000 requests per month included
cURL
Python
Ruby
JavaScript
Go
curl --location --request POST " $LAGO_URL /api/v1/plans" \
--header 'Authorization: Bearer __API_KEY__' \
--header 'Content-Type: application/json' \
--data-raw '{
"plan": {
"name": "Algolia Search",
"code": "__PLAN_CODE__",
"interval": "monthly",
"description": "Pay-as-you-go pricing for Algolia Search API",
"amount_cents": 0,
"amount_currency": "USD",
"charges": [
{
"billable_metric_code": "__BILLABLE_METRIC_CODE__",
"charge_model": "package",
"properties": {
"amount": "1.50",
"free_units": 10000,
"package_size": 1000
}
}
]
}
}'
Refer to the API reference and guide on package charges to learn more.
Create a customer
Create a customer with a unique external_id
.
cURL
Python
Ruby
Javascript
Go
curl --location --request POST " $LAGO_URL /api/v1/customers" \
--header "Authorization: Bearer $API_KEY " \
--header 'Content-Type: application/json' \
--data-raw '{
"customer": {
"external_id": "__EXTERNAL_CUSTOMER_ID__",
"name": "Acme Inc",
"email": "john@acme.com",
"currency": "USD",
"timezone": "America/New_York"
}
}'
Refer to the API reference to create a customer.
Create a subscription
Create a subscription for the customer with the plan’s code
.
cURL
Python
Ruby
Javascript
Go
curl --location --request POST " $LAGO_URL /api/v1/subscriptions" \
--header "Authorization: Bearer $API_KEY " \
--header 'Content-Type: application/json' \
--data-raw '{
"subscription": {
"external_customer_id": "__EXTERNAL_CUSTOMER_ID__",
"plan_code": "__PLAN_CODE__",
"external_id": "__EXTERNAL_SUBSCRIPTION_ID__"
}
}'
Refer to the API reference to create a subscription.
Ingest usage via events
Send usage events to Lago to track API requests.
Set the code
to match your billable metric code
Include search_requests_volume
property with the number of requests
cURL
Python
Ruby
Javascript
Go
curl --location --request POST " $LAGO_URL /api/v1/events" \
--header 'Authorization: Bearer __API_KEY__' \
--header 'Content-Type: application/json' \
--data-raw '{
"event": {
"transaction_id": "__TRANSACTION_ID__",
"code": "__BILLABLE_METRIC_CODE__",
"external_subscription_id": "__EXTERNAL_SUBSCRIPTION_ID__",
"properties": {
"search_requests_volume": 5000
}
}
}'
Refer to the API reference to create an event.
Monitor current usage
Track real-time customer usage for the current billing period.
cURL
Python
Ruby
Javascript
Go
curl --location --request GET " $LAGO_URL /api/v1/customers/__EXTERNAL_CUSTOMER_ID__/current_usage?external_subscription_id=__EXTERNAL_SUBSCRIPTION_ID__" \
--header 'Authorization: Bearer __API_KEY__' \
--header 'Content-Type: application/json'
Refer to the API reference to get the current usage.
Wrap-up
‘Pay-as-you-go’ pricing strategies are popular among API companies like Algolia.
With Lago, you can adapt this template to your products and services, using some of our most popular features:
Plan models , with or without subscription;
Billable metrics , including the ‘sum’ aggregation type; and
Charges , including our package and graduated pricing models.
Give it a try, click here to get started!