### Build the Client Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Build the client for distribution using Deno. ```bash deno task build ``` -------------------------------- ### Run Tests Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Execute the test suite using Deno. ```bash deno task test ``` -------------------------------- ### Install Lago JavaScript Client with npm Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Install the package via npm before importing it in your project. ```bash npm install lago-javascript-client ``` -------------------------------- ### Publish to npm Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Build the package and publish it to npm from the npm directory. ```bash deno task build cd npm npm publish ``` -------------------------------- ### Webhook Payload Types - Indexed Lookup by webhook_type Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Use LagoWebhookPayloads to get fully typed webhook payloads by indexing with the webhook_type string, e.g., 'alert.triggered'. ```typescript import type { LagoWebhookPayloads } from 'lago-javascript-client'; app.post('/webhooks', (req, res) => { const event = req.body as LagoWebhookPayloads['alert.triggered']; // event.triggered_alert is fully typed console.log(event.triggered_alert.external_customer_id); res.sendStatus(200); }); ``` -------------------------------- ### Generate Client from OpenAPI Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Run the Deno task to regenerate the client from the OpenAPI document. ```bash deno task generate:openapi ``` -------------------------------- ### Import and Use Client with Error Handling (npm and Deno) Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Shows how to import the Client and getLagoError utility for npm and Deno, then create a billable metric with error handling. Replace '__YOUR_API_KEY__' with your actual API key. ```typescript // npm import { Client, getLagoError } from 'lago-javascript-client'; // Deno import { Client, getLagoError } from 'https://deno.land/x/lago/mod.ts'; const lagoClient = Client('__YOUR_API_KEY__'); try { const { data } = await lagoClient.billableMetrics.createBillableMetric(billableMetric); } catch (error) { const lagoError = await getLagoError(error); } ``` -------------------------------- ### Pass Custom Fetch Instance to Lago Client Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md For Node.js versions below 18, polyfill the Fetch API and pass a custom fetch instance to the client using the 'customFetch' option. ```typescript import { Client } from 'lago-javascript-client'; import fetch from 'node-fetch'; const lagoClient = Client("api_key", { customFetch: fetch }); ``` -------------------------------- ### Error Handling with getLagoError Utility Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Use the getLagoError<>() utility function to extract the error object and TypeScript type when an API call fails. ```typescript try { const { data } = await lagoClient.billableMetrics.createBillableMetric(billableMetric); } catch (error) { const lagoError = await getLagoError(error); } ``` -------------------------------- ### Webhook Payload Types - Discriminated Union Handler Source: https://github.com/getlago/lago-javascript-client/blob/main/README.md Use LagoWebhookPayload as a discriminated union to handle multiple webhook events with type narrowing based on webhook_type. ```typescript import type { LagoWebhookPayload } from 'lago-javascript-client'; function handle(event: LagoWebhookPayload) { switch (event.webhook_type) { case 'alert.triggered': // event.triggered_alert is typed return event.triggered_alert; case 'invoice.created': // event.invoice is typed return event.invoice; case 'subscription.terminated': return event.subscription; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.