Xendit Node.js SDK
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
-
TypeError: Cannot read properties of undefined (reading 'createInvoice')
cause The Xendit client was not instantiated with a valid `secretKey`, or `secretKey` was undefined/null.fixEnsure `secretKey` is a non-empty string when initializing `new Xendit({ secretKey: 'YOUR_SECRET_KEY' })`. -
Error: "invoice_duration" must be a number
cause Since v7.0.0, the `invoice_duration` field for invoice creation expects a numeric value (in seconds) but received a string or other non-numeric type.fixConvert `invoice_duration` to a `number` type. For example, `invoice_duration: 86400` instead of `invoice_duration: '86400'`. -
Error: Cannot find module 'xendit-node' or its corresponding type declarations.
cause The package `xendit-node` is not installed, or the import path is incorrect, or TypeScript cannot find its type definitions.fixRun `npm install xendit-node@latest` (or `yarn add xendit-node@latest`). Ensure your `tsconfig.json` includes `node_modules` in `typeRoots` if custom configurations are used. -
TypeError: Xendit is not a constructor
cause Attempting to instantiate `Xendit` using CommonJS `require` syntax or `import Xendit from 'xendit-node'` (default import) when the package expects a named import.fixUse the correct ESM named import: `import { Xendit } from 'xendit-node';`
Warnings
- breaking The `invoice_duration` field in `CreateInvoiceAPI` has changed its type from `string` to `number`.
- breaking The `payment_request_id` field in `RefundCallbackData` was renamed to `payment_id`.
- breaking The `failure_code` field type in `PaymentCallback` was fixed to `String` (it might have been inferred incorrectly before).
- gotcha The SDK requires Node.js version 18.0.0 or later. Using older Node.js versions may lead to runtime errors or unexpected behavior.
- gotcha The `secretKey` for Xendit client instantiation must be your actual secret API key obtained from the Xendit Dashboard, not the publishable key. Never expose your secret key directly in client-side code.
Install
-
npm install xendit-node -
yarn add xendit-node -
pnpm add xendit-node
Imports
- Xendit
const Xendit = require('xendit-node')import { Xendit } from 'xendit-node' - XenditClient
const xenditClient = new XenditClient({ /* ... */ })import { Xendit } from 'xendit-node'; const xenditClient = new Xendit({ /* ... */ }); - Invoice and other API modules
import { Invoice } from 'xendit-node'import { Xendit } from 'xendit-node'; // Access via client instance: xenditClient.Invoice.createInvoice
Quickstart
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();