Xendit Node.js SDK

7.0.0 · active · verified Sun Apr 19

The Xendit Node.js SDK, currently at version 7.0.0, provides an official, convenient, and type-safe interface for integrating Node.js applications with the Xendit REST API. It abstracts direct HTTP requests, offering a structured way to interact with Xendit's various payment and financial services, including invoicing, payment requests, refunds, and balance inquiries. The SDK ships with first-class TypeScript support, enhancing developer experience through strong typing and autocompletion. It requires Node.js 18.0 or later for execution. While specific release cadence for major versions isn't explicitly stated, minor and patch versions are released regularly to add new features, support new payment channels, and fix bugs. A key differentiator is its direct support from Xendit, ensuring up-to-date API coverage and adherence to best practices for secure and reliable payment processing in Southeast Asia.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Xendit client using an environment variable for the API key, create a new invoice, retrieve it by ID, and fetch the account balance.

import { Xendit } from 'xendit-node';

const SECRET_API_KEY = process.env.XENDIT_SECRET_KEY ?? '';

if (!SECRET_API_KEY) {
  console.error("XENDIT_SECRET_KEY environment variable is not set.");
  process.exit(1);
}

const xenditClient = new Xendit({
  secretKey: SECRET_API_KEY,
});

async function createAndGetInvoice() {
  try {
    const externalId = `invoice-${Date.now()}`;
    const amount = 10000;

    const createInvoicePayload = {
      external_id: externalId,
      amount: amount,
      payer_email: 'test@example.com',
      description: 'Test Invoice for Node.js SDK',
      invoice_duration: 86400, // Required as number since v7.0.0
      callback_url: 'https://your-callback-url.com/xendit-webhook'
    };

    console.log(`Creating invoice with external ID: ${externalId}...`);
    const invoice = await xenditClient.Invoice.createInvoice(createInvoicePayload);
    console.log('Invoice created successfully:', invoice);

    console.log(`Getting invoice with ID: ${invoice.id}...`);
    const retrievedInvoice = await xenditClient.Invoice.getInvoiceById({ id: invoice.id });
    console.log('Invoice retrieved successfully:', retrievedInvoice);

    // Example of accessing another service (e.g., Balance)
    const balance = await xenditClient.Balance.getBalance({});
    console.log('Current balance:', balance.balance);

  } catch (error) {
    if (error instanceof Error) {
      console.error('Error interacting with Xendit API:', error.message);
    } else {
      console.error('An unknown error occurred:', error);
    }
  }
}

createAndGetInvoice();

view raw JSON →