SDKs and Recipes
Typescript SDK
4min
1\ use the api key provided by amberflo import { ingestoptions, metering, flushmode, usageclient, usageapipayload, aggregationtype, aggregationinterval, timerange } from "amberflo metering typescript"; / this sample illustrates how to ingest your metering data into amberflo in auto flush mode auto is the default mode / export async function runingest() { //obtain your amberflo api key const apikey = 'my api key'; 2\ setup metering client import { ingestoptions, metering, flushmode, usageclient, usageapipayload, aggregationtype, aggregationinterval, timerange } from "amberflo metering typescript"; / this sample illustrates how to ingest your metering data into amberflo in auto flush mode auto is the default mode / export async function runingest() { //obtain your amberflo api key const apikey = 'my api key'; //optional ingest options let ingestoptions = new ingestoptions(); //set flush mode to auto this is also the default, so this step is optional ingestoptions flushmode = flushmode auto; //number of messages posted to the api default is 100 ingestoptions batchsize = 20; //frequency at which queued data will be sent to api default is 1000 milliseconds ingestoptions frequencymillis = 3000; //set to true to log debug statements const debug = false; const metering = new metering(apikey, debug, ingestoptions); 3\ ingest meters import { ingestoptions, metering, flushmode, usageclient, usageapipayload, aggregationtype, aggregationinterval, timerange } from "amberflo metering typescript"; / this sample illustrates how to ingest your metering data into amberflo in auto flush mode auto is the default mode / export async function runingest() { //obtain your amberflo api key const apikey = 'my api key'; //optional ingest options let ingestoptions = new ingestoptions(); //set flush mode to auto this is also the default, so this step is optional ingestoptions flushmode = flushmode auto; //number of messages posted to the api default is 100 ingestoptions batchsize = 20; //frequency at which queued data will be sent to api default is 1000 milliseconds ingestoptions frequencymillis = 3000; //set to true to log debug statements const debug = false; const metering = new metering(apikey, debug, ingestoptions); //initialize and start the ingestion client metering start(); //define dimesions for your meters const dimensions = new map\<string, string>(); dimensions set("region", "midwest"); dimensions set("customertype", "tech"); let j = 0; for (j = 0; j < 50; j++) { let delay = new promise(resolve => settimeout(resolve, 100)); await delay; //queue meter messages for ingestion //in auto flush mode, queue will be flushed when ingestoptions batchsize is exceeded or periodically ingestoptions frequencymillis //params meterapiname string, metervalue number, metertimeinmillis number, customerid string, dimensions map\<string, string> metering meter("typescript apicalls", j + 1, date now(), "123", dimensions); metering meter("typescript bandwidth", j + 1, date now(), "123", dimensions); metering meter("typescript transactions", j + 1, date now(), "123", dimensions); metering meter("typescript cpu", j + 1, date now(), "123", dimensions); } 4\ shutdown the metering client import { ingestoptions, metering, flushmode, usageclient, usageapipayload, aggregationtype, aggregationinterval, timerange } from "amberflo metering typescript"; / this sample illustrates how to ingest your metering data into amberflo in auto flush mode auto is the default mode / export async function runingest() { //obtain your amberflo api key const apikey = 'my api key'; //optional ingest options let ingestoptions = new ingestoptions(); //set flush mode to auto this is also the default, so this step is optional ingestoptions flushmode = flushmode auto; //number of messages posted to the api default is 100 ingestoptions batchsize = 20; //frequency at which queued data will be sent to api default is 1000 milliseconds ingestoptions frequencymillis = 3000; //set to true to log debug statements const debug = false; const metering = new metering(apikey, debug, ingestoptions); //initialize and start the ingestion client metering start(); //define dimesions for your meters const dimensions = new map\<string, string>(); dimensions set("region", "midwest"); dimensions set("customertype", "tech"); let j = 0; for (j = 0; j < 50; j++) { let delay = new promise(resolve => settimeout(resolve, 100)); await delay; //queue meter messages for ingestion //in auto flush mode, queue will be flushed when ingestoptions batchsize is exceeded or periodically ingestoptions frequencymillis //params meterapiname string, metervalue number, metertimeinmillis number, customerid string, dimensions map\<string, string> metering meter("typescript apicalls", j + 1, date now(), "123", dimensions); metering meter("typescript bandwidth", j + 1, date now(), "123", dimensions); metering meter("typescript transactions", j + 1, date now(), "123", dimensions); metering meter("typescript cpu", j + 1, date now(), "123", dimensions); } //wait for messages and requests to api to complete await metering flush(); 5\ create customer import { ingestoptions, metering, flushmode, usageclient, usageapipayload, aggregationtype, aggregationinterval, timerange } from "amberflo metering typescript"; / this sample illustrates how to ingest your metering data into amberflo in auto flush mode auto is the default mode / export async function runingest() { //obtain your amberflo api key const apikey = 'my api key'; //optional ingest options let ingestoptions = new ingestoptions(); //set flush mode to auto this is also the default, so this step is optional ingestoptions flushmode = flushmode auto; //number of messages posted to the api default is 100 ingestoptions batchsize = 20; //frequency at which queued data will be sent to api default is 1000 milliseconds ingestoptions frequencymillis = 3000; //set to true to log debug statements const debug = false; const metering = new metering(apikey, debug, ingestoptions); //initialize and start the ingestion client metering start(); //define dimesions for your meters const dimensions = new map\<string, string>(); dimensions set("region", "midwest"); dimensions set("customertype", "tech"); let j = 0; for (j = 0; j < 50; j++) { let delay = new promise(resolve => settimeout(resolve, 100)); await delay; //queue meter messages for ingestion //in auto flush mode, queue will be flushed when ingestoptions batchsize is exceeded or periodically ingestoptions frequencymillis //params meterapiname string, metervalue number, metertimeinmillis number, customerid string, dimensions map\<string, string> metering meter("typescript apicalls", j + 1, date now(), "123", dimensions); metering meter("typescript bandwidth", j + 1, date now(), "123", dimensions); metering meter("typescript transactions", j + 1, date now(), "123", dimensions); metering meter("typescript cpu", j + 1, date now(), "123", dimensions); } //wait for messages and requests to api to complete await metering flush(); //perform graceful shutdown, flush, stop the timer await metering shutdown(); } runingest(); / this sample illustrates how to setup customer details / export async function runcustomerdetails() { //obtain your amberflo api key const apikey = 'my api key'; const traits = new map\<string, string>(); traits set("stripeid", "cus aj6by3vqcalaes"); traits set("customertype", "tech"); const metering = new metering(apikey, false); 6\ query usage import { ingestoptions, metering, flushmode, usageclient, usageapipayload, aggregationtype, aggregationinterval, timerange } from "amberflo metering typescript"; / this sample illustrates how to ingest your metering data into amberflo in auto flush mode auto is the default mode / export async function runingest() { //obtain your amberflo api key const apikey = 'my api key'; //optional ingest options let ingestoptions = new ingestoptions(); //set flush mode to auto this is also the default, so this step is optional ingestoptions flushmode = flushmode auto; //number of messages posted to the api default is 100 ingestoptions batchsize = 20; //frequency at which queued data will be sent to api default is 1000 milliseconds ingestoptions frequencymillis = 3000; //set to true to log debug statements const debug = false; const metering = new metering(apikey, debug, ingestoptions); //initialize and start the ingestion client metering start(); //define dimesions for your meters const dimensions = new map\<string, string>(); dimensions set("region", "midwest"); dimensions set("customertype", "tech"); let j = 0; for (j = 0; j < 50; j++) { let delay = new promise(resolve => settimeout(resolve, 100)); await delay; //queue meter messages for ingestion //in auto flush mode, queue will be flushed when ingestoptions batchsize is exceeded or periodically ingestoptions frequencymillis //params meterapiname string, metervalue number, metertimeinmillis number, customerid string, dimensions map\<string, string> metering meter("typescript apicalls", j + 1, date now(), "123", dimensions); metering meter("typescript bandwidth", j + 1, date now(), "123", dimensions); metering meter("typescript transactions", j + 1, date now(), "123", dimensions); metering meter("typescript cpu", j + 1, date now(), "123", dimensions); } //wait for messages and requests to api to complete await metering flush(); //perform graceful shutdown, flush, stop the timer await metering shutdown(); } runingest(); / this sample illustrates how to setup customer details / export async function runcustomerdetails() { //obtain your amberflo api key const apikey = 'my api key'; const traits = new map\<string, string>(); traits set("stripeid", "cus aj6by3vqcalaes"); traits set("customertype", "tech"); const metering = new metering(apikey, false); await metering addorupdatecustomerdetails('123', 'dell', traits); console log('customer setup completed!'); } runcustomerdetails(); export async function runusage() { //obtain your amberflo api key const apikey = 'my api key'; //initialize the usage client const client = new usageclient(apikey); // start date time represented as seconds since the unix epoch (1970 01 01t00 00 00z) and using utc // following is start time for last 24 hours const starttimeinseconds = math ceil(((new date() gettime()) (24 60 60 1000)) / 1000); let timerange = new timerange(); timerange starttimeinseconds = starttimeinseconds; // example 1 group by customers for a specific meter and all customers // setup usage query params // visit following link for description of payload // https //amberflo readme io/docs/getting started sample data#query the usage data let payload = new usageapipayload(); payload meterapiname = 'typescript apicalls'; payload aggregation = aggregationtype sum; payload timegroupinginterval = aggregationinterval day; //optional group the result by customer payload groupby = \["customerid"]; payload timerange = timerange; //call the usage api let jsonresult = await client getusage(payload); full code block import { ingestoptions, metering, flushmode, usageclient, usageapipayload, aggregationtype, aggregationinterval, timerange } from "amberflo metering typescript"; / this sample illustrates how to ingest your metering data into amberflo in auto flush mode auto is the default mode / export async function runingest() { //obtain your amberflo api key const apikey = 'my api key'; //optional ingest options let ingestoptions = new ingestoptions(); //set flush mode to auto this is also the default, so this step is optional ingestoptions flushmode = flushmode auto; //number of messages posted to the api default is 100 ingestoptions batchsize = 20; //frequency at which queued data will be sent to api default is 1000 milliseconds ingestoptions frequencymillis = 3000; //set to true to log debug statements const debug = false; const metering = new metering(apikey, debug, ingestoptions); //initialize and start the ingestion client metering start(); //define dimesions for your meters const dimensions = new map\<string, string>(); dimensions set("region", "midwest"); dimensions set("customertype", "tech"); let j = 0; for (j = 0; j < 50; j++) { let delay = new promise(resolve => settimeout(resolve, 100)); await delay; //queue meter messages for ingestion //in auto flush mode, queue will be flushed when ingestoptions batchsize is exceeded or periodically ingestoptions frequencymillis //params meterapiname string, metervalue number, metertimeinmillis number, customerid string, dimensions map\<string, string> metering meter("typescript apicalls", j + 1, date now(), "123", dimensions); metering meter("typescript bandwidth", j + 1, date now(), "123", dimensions); metering meter("typescript transactions", j + 1, date now(), "123", dimensions); metering meter("typescript cpu", j + 1, date now(), "123", dimensions); } //wait for messages and requests to api to complete await metering flush(); //perform graceful shutdown, flush, stop the timer await metering shutdown(); } runingest(); / this sample illustrates how to setup customer details / export async function runcustomerdetails() { //obtain your amberflo api key const apikey = 'my api key'; const traits = new map\<string, string>(); traits set("stripeid", "cus aj6by3vqcalaes"); traits set("customertype", "tech"); const metering = new metering(apikey, false); await metering addorupdatecustomerdetails('123', 'dell', traits); console log('customer setup completed!'); } runcustomerdetails(); export async function runusage() { //obtain your amberflo api key const apikey = 'my api key'; //initialize the usage client const client = new usageclient(apikey); // start date time represented as seconds since the unix epoch (1970 01 01t00 00 00z) and using utc // following is start time for last 24 hours const starttimeinseconds = math ceil(((new date() gettime()) (24 60 60 1000)) / 1000); let timerange = new timerange(); timerange starttimeinseconds = starttimeinseconds; // example 1 group by customers for a specific meter and all customers // setup usage query params // visit following link for description of payload // https //amberflo readme io/docs/getting started sample data#query the usage data let payload = new usageapipayload(); payload meterapiname = 'typescript apicalls'; payload aggregation = aggregationtype sum; payload timegroupinginterval = aggregationinterval day; //optional group the result by customer payload groupby = \["customerid"]; payload timerange = timerange; //call the usage api let jsonresult = await client getusage(payload); // to understand the api response, visit following link // https //amberflo readme io/docs/getting started sample data#query the usage data console log(json stringify(jsonresult, null, 4)); //example 2 filter for a meter for specific customer //setup usage query params let payloadforfilteredcustomer = new usageapipayload(); payloadforfilteredcustomer meterapiname = 'typescript apicalls'; payloadforfilteredcustomer aggregation = aggregationtype sum; payloadforfilteredcustomer timegroupinginterval = aggregationinterval day; //optional group the result by customer payloadforfilteredcustomer groupby = \["customerid"]; //filter result for a specific customer by id payloadforfilteredcustomer filter = {customerid \["123"]}; payloadforfilteredcustomer timerange = timerange; //call the usage api let jsonresultforfilteredcustomer = await client getusage(payloadforfilteredcustomer); console log(json stringify(jsonresultforfilteredcustomer, null, 4)); } runusage();