SDK

Node.js SDK

Fast and lightweight SDK for integrating STK Push and payment verification in Node apps.

Installation

bash
npm install paynexus

Quick Start

javascript
import PayNexus from 'paynexus';

const paynexus = new PayNexus({
  secretKey: 'sk_your_secret_key_here',
  webhookSecret: 'whsec_your_webhook_secret',
});

// 1) Initiate an STK Push. payment_account_id is OPTIONAL - omit it and
//    the API auto-selects your default M-Pesa account (same as Browser SDK).
const { data: payment } = await paynexus.payments.initiate({
  // payment_account_id: 1, // optional
  amount: 100,
  phone: '0746990866',
  description: 'Order #12345',
  account_reference: 'ORDER123',
});

console.log(payment.reference);
console.log(payment.checkout_request_id);

// 2) Poll for the result (pending -> completed/failed/expired/cancelled).
const status = await paynexus.payments.getByReference(payment.reference);
console.log(status.data.status);

// 3) Verify an incoming webhook (the signature header is "sha256="):
const payload = paynexus.verifyWebhook(
  rawBody,
  req.headers['x-paynexus-signature'],
  req.headers['x-paynexus-timestamp']
);
console.log(payload.event, payload.data);