Batch Events
Batch events represents the data delivered to lago application that will then be aggregated into billable metrics. The following endpoint explains how to send events for multiple subscriptions for one customer.
Route
POST
api/v1/events/batch
Usage
- Curl
- Python
- Ruby
- Javascript
- C#
- PHP
LAGO_URL="https://api.getlago.com"
API_KEY="__YOUR_API_KEY__"
curl --location --request POST "$LAGO_URL/api/v1/events/batch" \
--header "Authorization: Bearer $API_KEY" \
--header 'Content-Type: application/json' \
--data-raw '{
"event": {
"transaction_id": "__UNIQUE_ID__",
"external_subscription_ids": ["id1", "id2"],
"code": "__EVENT_CODE__",
"timestamp": $(date +%s),
"properties": {
"custom_field": 12
}
}
}'
from lago_python_client.client import Client
from lago_python_client.exceptions import LagoApiError
from lago_python_client.models import BatchEvent
client = Client(api_key='__YOUR_API_KEY__')
event = BatchEvent(
external_subscription_ids=["id1", "id2"],
transaction_id="__UNIQUE_ID__",
code="code",
timestamp=1650893379,
properties={"custom_field": "custom"}
)
try:
client.events.batch_create(event)
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.events.batch_create(
transaction_id: "__UNIQUE_ID__",
external_subscription_ids: ["id1", "id2"],
code: "code",
timestamp: Time.now.to_i,
properties: {
custom_field: 12
}
)
const batchEvent = {
transactionId: "__UNIQUE_TRANSACTION_ID__",
externalSubscriptionIds: ["id1", "id2"],
code: "code",
timestamp: 1650893379,
properties: { customField: "custom" },
};
await client.events.createBatchEvents({ event: batchEvent });
using System.Collections.Generic;
using System.Diagnostics;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
namespace Example
{
public class CreateBatchEventsExample
{
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 EventsApi(Configuration.Default);
var batchEventInput = new BatchEventInput(); // BatchEventInput | Batch events payload
try
{
// Create batch events
apiInstance.CreateBatchEvents(batchEventInput);
}
catch (ApiException e)
{
Debug.Print("Exception when calling EventsApi.CreateBatchEvents: " + 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\EventsApi(
// 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
);
$batch_event_input = new \OpenAPI\Client\Model\BatchEventInput(); // \OpenAPI\Client\Model\BatchEventInput | Batch events payload
try {
$apiInstance->createBatchEvents($batch_event_input);
} catch (Exception $e) {
echo 'Exception when calling EventsApi->createBatchEvents: ', $e->getMessage(), PHP_EOL;
}
Arguments
{
"event": {
"transaction_id": "__UNIQUE_ID__",
"external_customer_id": "__YOUR_CUSTOMER_ID__",
"external_subscription_ids": ["id1", "id2"],
"code": "__EVENT_CODE__",
"timestamp": 1650893379,
"properties": {
"custom_field": 12
}
}
}
Attributes | Type | Description |
---|---|---|
transaction_id | String Required | Unique ID identifying the event. As it will be used for idempotency, it should be a unique identifier |
external_subscription_ids | Array Required | Array of subscription IDs associated with the customer |
external_customer_id | String Optional | Customer unique identifier in your application |
code | String Required | Code identifying the type of the event. It should match the code property of one of your active billable metrics, otherwise it will be ignored |
timestamp | Integer Optional Default: event reception timestamp | Unix timestamp in seconds of the event occurence (UTC). If not provided, the API will set the event reception time |
properties | JSON Variable | Extra data to use for the event aggregation. When mandatory, it should contains the field_name configured at billable metric level as key and any value as field value .Aggregation type:- COUNT : optional- MAX : required. value must be a number- SUM : required. value must be a number- COUNT UNIQUE : required. value could have any datatype- RECURRING COUNT : required. value must be the unique identifier of the persisted object. |
Recurring count aggregation
Content of the event properties
field:
Attributes | Types | Description |
---|---|---|
field_name (*) | String Required | (*) Key must be the field_name configured at billable metric level and be the unique identifier of the object to persist. |
operation_type | String Required | Type of operation to perform on the persisted object. Possible values- add : Add or unsuspend a persisted object.- remove : Remove or suspend a persisted object. |
Responses
- HTTP 200
- HTTP 400
- HTTP 401
- HTTP 422
The event has been stored in the system and will be aggregated to generate fees.
Returns an empty response body.
{
"status": 400,
"error": "Bad Request",
}
The event
json root is not present in the request body.
{
"status": 401,
"error": "Unauthorized",
}
Access to the API end point 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 | error | description |
---|---|---|
transaction_id | value_is_mandatory | transaction_id value is missing |
code | value_is_mandatory | code value is missing |
external_subscription_ids | value_is_mandatory | external_subscription_ids is missing |