Create billable metric
Route
POST
/api/v1/billable_metrics
Usage
- Curl
- Python
- Ruby
- Javascript
- Go
- C#
- PHP
LAGO_URL="https://api.getlago.com"
API_KEY="__YOUR_API_KEY__"
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": "bm_name",
"code": "bm_code",
"description": "description",
"aggregation_type": "sum_agg",
"field_name": "amount_sum",
"group": {
"key": "region",
"value": ["USA", "EUROPE"]
}
}
}'
from lago_python_client.client import Client
from lago_python_client.exceptions import LagoApiError
from lago_python_client.models import BillableMetricGroup, BillableMetric
client = Client(api_key='__YOUR_API_KEY__')
group = BillableMetricGroup(
key='country',
values=['france', 'italy', 'spain']
)
billable_metric = BillableMetric(
name='name',
code='code_first',
description='desc',
aggregation_type='sum_agg',
field_name='amount_sum',
group=group
)
try:
client.billable_metrics.create(billable_metric)
except LagoApiError as e:
repair_broken_state(e) # do something on error or raise your own exception
require 'lago-ruby-client'
client = Lago::Api::Client.new({api_key: '__YOUR_API_KEY__'})
client.billable_metrics.create({
name: 'BM1',
code: 'code_bm',
description: 'description',
aggregation_type: 'sum_agg',
field_name: 'amount_sum',
group: {
key: 'country',
values: %w[france italy spain]
}
})
await client.billableMetrics.createBillableMetric({
billable_metric: {
name: "name1",
code: "code1",
aggregationType: "sum_agg",
fieldName: "field_name",
group: {
key: "country",
values: ["france", "italy", "spain"],
},
},
});
import "fmt"
import "github.com/getlago/lago-go-client"
func main() {
lagoClient := lago.New().
SetApiKey("__YOUR_API_KEY__")
bmInput := &lago.BillableMetricInput{
Name: "First Billable Metric",
Code: "first_bm",
Description: "My First Billable Metric"
AggregationType: lago.CountAggregation,
FieldName: "sum",
Group: map[string]interface{}{
"key": "country",
"values": [3]string{"france", "italy", "spain"},
},
}
billableMetric, err := lagoClient.BillableMetric().Create(bmInput)
if err != nil {
// Error is *lago.Error
panic(err)
}
// billableMetric is *lago.BillableMetric
fmt.Println(billableMetric)
}
using System.Collections.Generic;
using System.Diagnostics;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
namespace Example
{
public class CreateBillableMetricExample
{
public static void Main()
{
Configuration.Default.BasePath = "https://api.getlago.com/api/v1";
// Configure HTTP bearer authorization: bearerAuth
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
var apiInstance = new BillableMetricsApi(Configuration.Default);
var billableMetricInput = new BillableMetricInput(); // BillableMetricInput | Billable metric payload
try
{
// Create a new billable metric
BillableMetric result = apiInstance.CreateBillableMetric(billableMetricInput);
Debug.WriteLine(result);
}
catch (ApiException e)
{
Debug.Print("Exception when calling BillableMetricsApi.CreateBillableMetric: " + e.Message );
Debug.Print("Status Code: "+ e.ErrorCode);
Debug.Print(e.StackTrace);
}
}
}
}
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure Bearer authorization: bearerAuth
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
$apiInstance = new OpenAPI\Client\Api\BillableMetricsApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
$billable_metric_input = new \OpenAPI\Client\Model\BillableMetricInput(); // \OpenAPI\Client\Model\BillableMetricInput | Billable metric payload
try {
$result = $apiInstance->createBillableMetric($billable_metric_input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling BillableMetricsApi->createBillableMetric: ', $e->getMessage(), PHP_EOL;
}
Arguments
{
"billable_metric": {
"name": "bm_name",
"code": "bm_code",
"description": "description",
"aggregation_type": "sum_agg",
"field_name": "amount_sum",
"group": {
"key": "country",
"values": ["france", "italy", "spain"]
}
}
}
Attributes | Type | Description |
---|---|---|
name | String Required | Billable metric name |
code | String Required | Code identifying the billable metric |
description | String Optional | Description of the billable metric |
aggregation_type | String Required | Aggregation type that is used in event calculations. It can be count_agg (metered), sum_agg (metered), max_agg (metered), unique_count_agg (metered) or recurring_count_agg (persistent) |
field_name | String Required (optional only for count_agg aggregation type) | Field name used in events. |
group | Object Optional | Group (one or two dimensions) for pricing differently the billable metric |
Group - One dimension
Attributes | Type | Description |
---|---|---|
key | String Required | Name of the event's field used for grouping |
values | Array Required | Array of string representing all possible values |
Example:
{
"group": {
"key": "country",
"values": ["france", "italy", "spain"]
}
}
Group - Two dimensions
Attributes | Type | Description |
---|---|---|
key | String Required | Name of the first event's field used for grouping |
values | Array Required | Array of objects representing all possible values |
Values:
Attributes | Type | Description |
---|---|---|
name | String Required | Value for the first dimension of the group |
key | Array Required | Name of the second event's field used for grouping |
values | Array Required | Array of string representing all possible values of the second dimension |
Example:
{
"group": {
"key": "cloud",
"values": [
{
"name": "AWS",
"key": "region",
"values": ["USA", "EUROPE"]
}, {
"name": "Google",
"key": "region",
"values": ["USA"]
}
]
}
}
Responses
- HTTP 200
- HTTP 400
- HTTP 401
- HTTP 422
The billable metric was created
Returns a billable metric object.
{
"status": 400,
"error": "Bad Request"
}
The billable_metric
json root is not present in the request body.
{
"status": 401,
"error": "Unauthorized"
}
Access to the API endpoint is unhautorized.
Possible reasons are:
- The
Authorization
header is missing - The
Authorization
header does not contain the API key - The Api key is invalid or expired
{
"status": 422,
"error": "Unprocessable entity",
"code": "validation_errors",
"error_details": {
"field": ["error"]
}
}
Possible errors:
Field | Code | Description |
---|---|---|
name | value_is_mandatory | name value is missing |
code | value_is_mandatory | code value is missing |
code | value_already_exists | code value is already used for another billable metric |
field_name | value_is_mandatory | field_name value is missing |
aggregation_type | value_is_invalid | Provided aggregation_type value is invalid |
group | value_is_invalid | Format of provided JSON for group is invalid |