Recipes
Typescript SDK
4min
1. use the api-key provided by amberflo
TypeScript
1import { IngestOptions, Metering, FlushMode, UsageClient, UsageApiPayload, AggregationType, AggregationInterval, TimeRange } from "amberflo-metering-typescript";
2
3/**
4 * This sample illustrates how to ingest your metering data into Amberflo in auto flush mode.
5 * Auto is the default mode.
6 */
7export async function runIngest() {
8 //obtain your Amberflo API Key
9 const apiKey = 'my-api-key';
2. setup metering client
TypeScript
1import { IngestOptions, Metering, FlushMode, UsageClient, UsageApiPayload, AggregationType, AggregationInterval, TimeRange } from "amberflo-metering-typescript";
2
3/**
4 * This sample illustrates how to ingest your metering data into Amberflo in auto flush mode.
5 * Auto is the default mode.
6 */
7export async function runIngest() {
8 //obtain your Amberflo API Key
9 const apiKey = 'my-api-key';
10
11 //optional ingest options
12 let ingestOptions = new IngestOptions();
13 //set flush mode to auto. This is also the default, so this step is optional.
14 ingestOptions.flushMode = FlushMode.auto;
15 //Number of messages posted to the API. Default is 100.
16 ingestOptions.batchSize = 20;
17 //Frequency at which queued data will be sent to API. Default is 1000 milliseconds.
18 ingestOptions.frequencyMillis = 3000;
19 //Set to true to log debug statements
20 const debug = false;
21
22 const metering = new Metering(apiKey, debug, ingestOptions);
3. ingest meters
TypeScript
1import { IngestOptions, Metering, FlushMode, UsageClient, UsageApiPayload, AggregationType, AggregationInterval, TimeRange } from "amberflo-metering-typescript";
2
3/**
4 * This sample illustrates how to ingest your metering data into Amberflo in auto flush mode.
5 * Auto is the default mode.
6 */
7export async function runIngest() {
8 //obtain your Amberflo API Key
9 const apiKey = 'my-api-key';
10
11 //optional ingest options
12 let ingestOptions = new IngestOptions();
13 //set flush mode to auto. This is also the default, so this step is optional.
14 ingestOptions.flushMode = FlushMode.auto;
15 //Number of messages posted to the API. Default is 100.
16 ingestOptions.batchSize = 20;
17 //Frequency at which queued data will be sent to API. Default is 1000 milliseconds.
18 ingestOptions.frequencyMillis = 3000;
19 //Set to true to log debug statements
20 const debug = false;
21
22 const metering = new Metering(apiKey, debug, ingestOptions);
23
24 //initialize and start the ingestion client
25 metering.start();
26
27 //define dimesions for your meters
28 const dimensions = new Map<string, string>();
29 dimensions.set("region", "Midwest");
30 dimensions.set("customerType", "Tech");
31
32 let j = 0;
33 for (j = 0; j < 50; j++) {
34 let delay = new Promise(resolve => setTimeout(resolve, 100));
35 await delay;
36
37 //Queue meter messages for ingestion.
38 //In auto flush mode, queue will be flushed when ingestOptions.batchSize is exceeded or periodically ingestOptions.frequencyMillis
39 //Params: meterApiName: string, meterValue: number, meterTimeInMillis: number, customerId: string, dimensions: Map<string, string>
40 metering.meter("TypeScript-ApiCalls", j + 1, Date.now(), "123", dimensions);
41 metering.meter("TypeScript-Bandwidth", j + 1, Date.now(), "123", dimensions);
42 metering.meter("TypeScript-Transactions", j + 1, Date.now(), "123", dimensions);
43 metering.meter("TypeScript-CPU", j + 1, Date.now(), "123", dimensions);
44 }
4. Shutdown the metering client
TypeScript
1import { IngestOptions, Metering, FlushMode, UsageClient, UsageApiPayload, AggregationType, AggregationInterval, TimeRange } from "amberflo-metering-typescript";
2
3/**
4 * This sample illustrates how to ingest your metering data into Amberflo in auto flush mode.
5 * Auto is the default mode.
6 */
7export async function runIngest() {
8 //obtain your Amberflo API Key
9 const apiKey = 'my-api-key';
10
11 //optional ingest options
12 let ingestOptions = new IngestOptions();
13 //set flush mode to auto. This is also the default, so this step is optional.
14 ingestOptions.flushMode = FlushMode.auto;
15 //Number of messages posted to the API. Default is 100.
16 ingestOptions.batchSize = 20;
17 //Frequency at which queued data will be sent to API. Default is 1000 milliseconds.
18 ingestOptions.frequencyMillis = 3000;
19 //Set to true to log debug statements
20 const debug = false;
21
22 const metering = new Metering(apiKey, debug, ingestOptions);
23
24 //initialize and start the ingestion client
25 metering.start();
26
27 //define dimesions for your meters
28 const dimensions = new Map<string, string>();
29 dimensions.set("region", "Midwest");
30 dimensions.set("customerType", "Tech");
31
32 let j = 0;
33 for (j = 0; j < 50; j++) {
34 let delay = new Promise(resolve => setTimeout(resolve, 100));
35 await delay;
36
37 //Queue meter messages for ingestion.
38 //In auto flush mode, queue will be flushed when ingestOptions.batchSize is exceeded or periodically ingestOptions.frequencyMillis
39 //Params: meterApiName: string, meterValue: number, meterTimeInMillis: number, customerId: string, dimensions: Map<string, string>
40 metering.meter("TypeScript-ApiCalls", j + 1, Date.now(), "123", dimensions);
41 metering.meter("TypeScript-Bandwidth", j + 1, Date.now(), "123", dimensions);
42 metering.meter("TypeScript-Transactions", j + 1, Date.now(), "123", dimensions);
43 metering.meter("TypeScript-CPU", j + 1, Date.now(), "123", dimensions);
44 }
45
46 //wait for messages and requests to API to complete
47 await metering.flush();
5. create customer
TypeScript
1import { IngestOptions, Metering, FlushMode, UsageClient, UsageApiPayload, AggregationType, AggregationInterval, TimeRange } from "amberflo-metering-typescript";
2
3/**
4 * This sample illustrates how to ingest your metering data into Amberflo in auto flush mode.
5 * Auto is the default mode.
6 */
7export async function runIngest() {
8 //obtain your Amberflo API Key
9 const apiKey = 'my-api-key';
10
11 //optional ingest options
12 let ingestOptions = new IngestOptions();
13 //set flush mode to auto. This is also the default, so this step is optional.
14 ingestOptions.flushMode = FlushMode.auto;
15 //Number of messages posted to the API. Default is 100.
16 ingestOptions.batchSize = 20;
17 //Frequency at which queued data will be sent to API. Default is 1000 milliseconds.
18 ingestOptions.frequencyMillis = 3000;
19 //Set to true to log debug statements
20 const debug = false;
21
22 const metering = new Metering(apiKey, debug, ingestOptions);
23
24 //initialize and start the ingestion client
25 metering.start();
26
27 //define dimesions for your meters
28 const dimensions = new Map<string, string>();
29 dimensions.set("region", "Midwest");
30 dimensions.set("customerType", "Tech");
31
32 let j = 0;
33 for (j = 0; j < 50; j++) {
34 let delay = new Promise(resolve => setTimeout(resolve, 100));
35 await delay;
36
37 //Queue meter messages for ingestion.
38 //In auto flush mode, queue will be flushed when ingestOptions.batchSize is exceeded or periodically ingestOptions.frequencyMillis
39 //Params: meterApiName: string, meterValue: number, meterTimeInMillis: number, customerId: string, dimensions: Map<string, string>
40 metering.meter("TypeScript-ApiCalls", j + 1, Date.now(), "123", dimensions);
41 metering.meter("TypeScript-Bandwidth", j + 1, Date.now(), "123", dimensions);
42 metering.meter("TypeScript-Transactions", j + 1, Date.now(), "123", dimensions);
43 metering.meter("TypeScript-CPU", j + 1, Date.now(), "123", dimensions);
44 }
45
46 //wait for messages and requests to API to complete
47 await metering.flush();
48 //perform graceful shutdown, flush, stop the timer
49 await metering.shutdown();
50}
51
52runIngest();
53
54/**
55 * This sample illustrates how to setup customer details.
56 */
57export async function runCustomerDetails() {
58 //obtain your Amberflo API Key
59 const apiKey = 'my-api-key';
60
61 const traits = new Map<string, string>();
62 traits.set("stripeId", "cus_AJ6bY3VqcaLAEs");
63 traits.set("customerType", "Tech");
64
65 const metering = new Metering(apiKey, false);
6. query usage
TypeScript
1import { IngestOptions, Metering, FlushMode, UsageClient, UsageApiPayload, AggregationType, AggregationInterval, TimeRange } from "amberflo-metering-typescript";
2
3/**
4 * This sample illustrates how to ingest your metering data into Amberflo in auto flush mode.
5 * Auto is the default mode.
6 */
7export async function runIngest() {
8 //obtain your Amberflo API Key
9 const apiKey = 'my-api-key';
10
11 //optional ingest options
12 let ingestOptions = new IngestOptions();
13 //set flush mode to auto. This is also the default, so this step is optional.
14 ingestOptions.flushMode = FlushMode.auto;
15 //Number of messages posted to the API. Default is 100.
16 ingestOptions.batchSize = 20;
17 //Frequency at which queued data will be sent to API. Default is 1000 milliseconds.
18 ingestOptions.frequencyMillis = 3000;
19 //Set to true to log debug statements
20 const debug = false;
21
22 const metering = new Metering(apiKey, debug, ingestOptions);
23
24 //initialize and start the ingestion client
25 metering.start();
26
27 //define dimesions for your meters
28 const dimensions = new Map<string, string>();
29 dimensions.set("region", "Midwest");
30 dimensions.set("customerType", "Tech");
31
32 let j = 0;
33 for (j = 0; j < 50; j++) {
34 let delay = new Promise(resolve => setTimeout(resolve, 100));
35 await delay;
36
37 //Queue meter messages for ingestion.
38 //In auto flush mode, queue will be flushed when ingestOptions.batchSize is exceeded or periodically ingestOptions.frequencyMillis
39 //Params: meterApiName: string, meterValue: number, meterTimeInMillis: number, customerId: string, dimensions: Map<string, string>
40 metering.meter("TypeScript-ApiCalls", j + 1, Date.now(), "123", dimensions);
41 metering.meter("TypeScript-Bandwidth", j + 1, Date.now(), "123", dimensions);
42 metering.meter("TypeScript-Transactions", j + 1, Date.now(), "123", dimensions);
43 metering.meter("TypeScript-CPU", j + 1, Date.now(), "123", dimensions);
44 }
45
46 //wait for messages and requests to API to complete
47 await metering.flush();
48 //perform graceful shutdown, flush, stop the timer
49 await metering.shutdown();
50}
51
52runIngest();
53
54/**
55 * This sample illustrates how to setup customer details.
56 */
57export async function runCustomerDetails() {
58 //obtain your Amberflo API Key
59 const apiKey = 'my-api-key';
60
61 const traits = new Map<string, string>();
62 traits.set("stripeId", "cus_AJ6bY3VqcaLAEs");
63 traits.set("customerType", "Tech");
64
65 const metering = new Metering(apiKey, false);
66 await metering.addOrUpdateCustomerDetails('123', 'Dell', traits);
67
68 console.log('customer setup completed!');
69}
70
71runCustomerDetails();
72
73export async function runUsage() {
74 //obtain your Amberflo API Key
75 const apiKey = 'my-api-key';
76
77 //initialize the usage client
78 const client = new UsageClient(apiKey);
79
80 // start date time represented as seconds since the Unix Epoch (1970-01-01T00:00:00Z) and using UTC.
81 // following is Start time for last 24 hours
82 const startTimeInSeconds = Math.ceil(((new Date().getTime()) - (24 * 60 * 60 * 1000)) / 1000);
83
84 let timeRange = new TimeRange();
85 timeRange.startTimeInSeconds = startTimeInSeconds;
86
87 // Example 1: group by customers for a specific meter and all customers
88 // setup usage query params
89 // visit following link for description of payload:
90 // https://amberflo.readme.io/docs/getting-started-sample-data#query-the-usage-data
91 let payload = new UsageApiPayload();
92 payload.meterApiName = 'TypeScript-ApiCalls';
93 payload.aggregation = AggregationType.sum;
94 payload.timeGroupingInterval = AggregationInterval.day;
95 //optional: group the result by customer
96 payload.groupBy = ["customerId"];
97 payload.timeRange = timeRange;
98
99 //Call the usage API
100 let jsonResult = await client.getUsage(payload);
Full code block:
TypeScript
1import { IngestOptions, Metering, FlushMode, UsageClient, UsageApiPayload, AggregationType, AggregationInterval, TimeRange } from "amberflo-metering-typescript";
2
3/**
4 * This sample illustrates how to ingest your metering data into Amberflo in auto flush mode.
5 * Auto is the default mode.
6 */
7export async function runIngest() {
8 //obtain your Amberflo API Key
9 const apiKey = 'my-api-key';
10
11 //optional ingest options
12 let ingestOptions = new IngestOptions();
13 //set flush mode to auto. This is also the default, so this step is optional.
14 ingestOptions.flushMode = FlushMode.auto;
15 //Number of messages posted to the API. Default is 100.
16 ingestOptions.batchSize = 20;
17 //Frequency at which queued data will be sent to API. Default is 1000 milliseconds.
18 ingestOptions.frequencyMillis = 3000;
19 //Set to true to log debug statements
20 const debug = false;
21
22 const metering = new Metering(apiKey, debug, ingestOptions);
23
24 //initialize and start the ingestion client
25 metering.start();
26
27 //define dimesions for your meters
28 const dimensions = new Map<string, string>();
29 dimensions.set("region", "Midwest");
30 dimensions.set("customerType", "Tech");
31
32 let j = 0;
33 for (j = 0; j < 50; j++) {
34 let delay = new Promise(resolve => setTimeout(resolve, 100));
35 await delay;
36
37 //Queue meter messages for ingestion.
38 //In auto flush mode, queue will be flushed when ingestOptions.batchSize is exceeded or periodically ingestOptions.frequencyMillis
39 //Params: meterApiName: string, meterValue: number, meterTimeInMillis: number, customerId: string, dimensions: Map<string, string>
40 metering.meter("TypeScript-ApiCalls", j + 1, Date.now(), "123", dimensions);
41 metering.meter("TypeScript-Bandwidth", j + 1, Date.now(), "123", dimensions);
42 metering.meter("TypeScript-Transactions", j + 1, Date.now(), "123", dimensions);
43 metering.meter("TypeScript-CPU", j + 1, Date.now(), "123", dimensions);
44 }
45
46 //wait for messages and requests to API to complete
47 await metering.flush();
48 //perform graceful shutdown, flush, stop the timer
49 await metering.shutdown();
50}
51
52runIngest();
53
54/**
55 * This sample illustrates how to setup customer details.
56 */
57export async function runCustomerDetails() {
58 //obtain your Amberflo API Key
59 const apiKey = 'my-api-key';
60
61 const traits = new Map<string, string>();
62 traits.set("stripeId", "cus_AJ6bY3VqcaLAEs");
63 traits.set("customerType", "Tech");
64
65 const metering = new Metering(apiKey, false);
66 await metering.addOrUpdateCustomerDetails('123', 'Dell', traits);
67
68 console.log('customer setup completed!');
69}
70
71runCustomerDetails();
72
73export async function runUsage() {
74 //obtain your Amberflo API Key
75 const apiKey = 'my-api-key';
76
77 //initialize the usage client
78 const client = new UsageClient(apiKey);
79
80 // start date time represented as seconds since the Unix Epoch (1970-01-01T00:00:00Z) and using UTC.
81 // following is Start time for last 24 hours
82 const startTimeInSeconds = Math.ceil(((new Date().getTime()) - (24 * 60 * 60 * 1000)) / 1000);
83
84 let timeRange = new TimeRange();
85 timeRange.startTimeInSeconds = startTimeInSeconds;
86
87 // Example 1: group by customers for a specific meter and all customers
88 // setup usage query params
89 // visit following link for description of payload:
90 // https://amberflo.readme.io/docs/getting-started-sample-data#query-the-usage-data
91 let payload = new UsageApiPayload();
92 payload.meterApiName = 'TypeScript-ApiCalls';
93 payload.aggregation = AggregationType.sum;
94 payload.timeGroupingInterval = AggregationInterval.day;
95 //optional: group the result by customer
96 payload.groupBy = ["customerId"];
97 payload.timeRange = timeRange;
98
99 //Call the usage API
100 let jsonResult = await client.getUsage(payload);
101 // To understand the API response, visit following link:
102 // https://amberflo.readme.io/docs/getting-started-sample-data#query-the-usage-data
103 console.log(JSON.stringify(jsonResult, null, 4));
104
105
106 //Example 2: filter for a meter for specific customer
107 //setup usage query params
108 let payloadForFilteredCustomer = new UsageApiPayload();
109 payloadForFilteredCustomer.meterApiName = 'TypeScript-ApiCalls';
110 payloadForFilteredCustomer.aggregation = AggregationType.sum;
111 payloadForFilteredCustomer.timeGroupingInterval = AggregationInterval.day;
112 //optional: group the result by customer
113 payloadForFilteredCustomer.groupBy = ["customerId"];
114 //Filter result for a specific customer by ID
115 payloadForFilteredCustomer.filter = {customerId: ["123"]};
116 payloadForFilteredCustomer.timeRange = timeRange;
117
118 //Call the usage API
119 let jsonResultForFilteredCustomer = await client.getUsage(payloadForFilteredCustomer);
120 console.log(JSON.stringify(jsonResultForFilteredCustomer, null, 4));
121}
122
123runUsage();
Updated 11 Sep 2024
Did this page help you?