Lago JavaScript API Client
The `lago-javascript-client` is an official JavaScript wrapper for interacting with the Lago API, providing a structured way to manage billing and invoicing operations. It is currently at version 1.45.0, with frequent minor releases indicated by the recent changelog, often involving version bumps. This client is notable for its broad compatibility, designed to function across various JavaScript runtimes including Node.js (specifically Node.js >= 18 for native support), Cloudflare Workers, and Deno. It is automatically generated from the Lago OpenAPI document, ensuring it stays synchronized with the API's latest specifications. A key architectural decision is its reliance on the Fetch API for HTTP requests, which means users on older Node.js environments (pre-18) must either enable `--experimental-fetch` or provide a custom Fetch implementation like `node-fetch`. The library also ships with TypeScript types, facilitating robust development and error handling using its `getLagoError` utility.
Common errors
-
ReferenceError: fetch is not defined
cause Node.js version is older than 18, or the `--experimental-fetch` flag is not used, and no polyfill for `fetch` is provided.fixUpgrade Node.js to version 18 or higher, or install `node-fetch` and pass it as a `customFetch` option to the `Client` constructor. Alternatively, run Node.js with `node --experimental-fetch your-script.js`. -
TypeError: (0 , lago_javascript_client__WEBPACK_IMPORTED_MODULE_0__.Client) is not a function
cause This error often occurs in environments where CommonJS `require()` is implicitly or explicitly trying to load an ES Module library that primarily uses named exports, or when bundlers mishandle the module type.fixEnsure you are using ES Module `import { Client } from 'lago-javascript-client';` syntax throughout your project. Verify your `tsconfig.json` (if TypeScript) and bundler configuration (`module` and `moduleResolution`) are set for `ESNext` or `NodeNext` to properly handle ESM.
Warnings
- breaking The Lago JavaScript Client relies on the Fetch API. Node.js versions prior to 18 do not support Fetch natively. Attempting to use the client without a polyfill or the `--experimental-fetch` flag will result in runtime errors.
- gotcha The package is built with `dnt` to support multiple runtimes (Node.js, Deno, Cloudflare Workers). This means its primary module format is ESM. While some interoperability exists, mixing `require()` with this library can lead to unexpected behavior or require additional configuration.
- deprecated The previous `lago-nodejs-client` package is deprecated in favor of `lago-javascript-client` which offers TypeScript support and broader runtime compatibility. Users of the old package should migrate.
- breaking The underlying Lago API, which this client wraps, has undergone significant changes in how 'groups' are handled, replaced by 'filters', and has made `external_subscription_id` mandatory for events. Several legacy API fields have also been deprecated. These changes will impact how you define and send billing data.
Install
-
npm install lago-javascript-client -
yarn add lago-javascript-client -
pnpm add lago-javascript-client
Imports
- Client
const { Client } = require('lago-javascript-client');import { Client } from 'lago-javascript-client'; - getLagoError
const { getLagoError } = require('lago-javascript-client');import { getLagoError } from 'lago-javascript-client'; - Client
import { Client } from 'https://deno.land/x/lago/mod.ts';
Quickstart
import { Client, getLagoError } from 'lago-javascript-client';
import fetch from 'node-fetch'; // Required for Node.js < 18 or without --experimental-fetch
// Initialize the client with your API key.
// For Node.js < 18, you might need to pass a custom fetch instance.
const lagoClient = Client(process.env.LAGO_API_KEY ?? '', { customFetch: fetch });
async function createBillableMetric() {
const billableMetric = {
billableMetric: {
name: 'api_calls',
code: 'api_calls',
aggregation_type: 'count',
description: 'Number of API calls made.'
}
};
try {
// Example: Creating a new billable metric
const { data } = await lagoClient.billableMetrics.createBillableMetric(billableMetric);
console.log('Successfully created billable metric:', data);
} catch (error) {
// Use getLagoError for structured error handling and type inference
const lagoError = await getLagoError(error);
console.error('Error creating billable metric:', lagoError.message, lagoError.response);
}
}
createBillableMetric();