0.0.6 • Published 3 months ago
@vayu-billing/client v0.0.6
Vayu typescript client
Prerequisites
- Node 18 and above
- Install the
vayu client
package (npm i @vayu-billing/client
)
End-to-End Code Example
To illustrate how to use the Vayu
library for sending events to Vayu's API, here is a complete code snippet. This example combines all the steps detailed in previous sections:
import { Vayu } from '@vayu-billing/client';
const vayu = new Vayu('YOUR_API_KEY');
async function main() {
const sendEventsResult = await vayu.sendEvents([{
name: 'api_call',
timestamp: new Date(),
customerAlias: 'customer_12345',
data: {
processing_duration: 124.83,
api_endpoint: '/api/v1/users',
},
ref: '7f9e3a4d-3f1a-4f5d-9a1a-0d9a3c1a9b1a',
}]);
console.log(sendEventsResult);
}
main();
That's it! This example demonstrates how to send an event using typescript and the Vayu
library.
Step-by-step Guide
Step 1: Import Vayu
The first step is to import the Vayu
class from the Vayu
package.
import { Vayu } from '@vayu-billing/client';
Step 2: Initialize the Client
After importing, create a new Vayu
instance and pass the api key
const vayu = new Vayu('YOUR_API_KEY');
Step 3: Send Events
Finally, you can send events to the Vayu API by using the send_events
method. The method takes a list of dictionaries, where each dictionary represents a single event. Each event dictionary should contain the following fields:
name
(string - required): A distinctive label assigned to an event. It serves as a critical identifier for categorizing and pricing events.timestamp
(datetime): The exact moment when the event occurs.customerAlias
(string): A unique identifier for each customer, which may be pseudonymous or obfuscated.data
(object): A schema-less JSON object containing miscellaneous attributes and metrics associated with the event.ref
(string, optional): A UUID or other high-entropy string serving as an immutable reference for each event entry.
Example:
const sendEventsResult = await vayu.sendEvents([{
name: 'api_call',
timestamp: new Date(),
customerAlias: 'customer_12345',
data: {
processing_duration: 124.83,
api_endpoint: '/api/v1/users',
},
ref: '7f9e3a4d-3f1a-4f5d-9a1a-0d9a3c1a9b1a',
}]);
console.log(sendEventsResult);