Lago JavaScript API Client

1.45.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Lago client, configure it for Node.js compatibility (if needed), and perform an asynchronous API call to create a billable metric with proper error handling.

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();

view raw JSON →