Nile PayNile Pay

Your First Payment

Collect your first payment with the Nile Pay SDK

This guide walks through a complete payment flow from initialization to event handling.

Complete Example

import { createNilePay } from "@nilepay/sdk";
import { randomUUID } from "crypto";

const nilePay = createNilePay({
  environment: "sandbox",
  apiKey: "npk_sandbox_...",
  apiSecret: "nps_sandbox_...",
});

// Start a payment
const payment = await nilePay.collectPayment({
  amount: 5000,
  currency: "UGX",
  customer: { phone: "256700000000" },
  reference: randomUUID(),
  description: "Order #1234",
});

// Listen for status updates
payment.on("processing", (data) => {
  console.log("Payment processing...", data.reference);
});

payment.on("success", (data) => {
  console.log("Payment successful!", data.transactionId);
});

payment.on("failed", (data) => {
  console.log("Payment failed:", data.message);
});

// Or use promise-based waiting
const result = await payment.wait();

How It Works

  1. Request sent: SDK authenticates with HMAC-SHA256 and sends payment request to Nile Pay backend
  2. Processing: Backend forwards to provider (MTN MoMo, Airtel Money, etc.)
  3. Customer action: Provider sends USSD prompt to customer's phone
  4. Confirmation: Customer enters PIN on their phone
  5. Webhook: Provider confirms to Nile Pay
  6. Events: SDK emits success or failed based on outcome

Payment Options

Required Fields

FieldTypeDescription
amountnumberAmount in smallest currency unit (UGX = cents)
currencystringISO 4217 currency code (e.g., "UGX")
customer.phonestringCustomer phone number with country code
referencestringUnique order reference (UUID recommended)

Optional Fields

FieldTypeDescription
descriptionstringPayment description shown to customer
customer.namestringCustomer full name
customer.emailstringCustomer email address
metadataobjectCustom key-value pairs for your records

Event Handling

EventEmitter Pattern

payment.on("processing", handler);
payment.on("success", handler);
payment.on("failed", handler);
payment.on("cancelled", handler);

Promise Pattern

const result = await payment.wait();

// result.status: true with data, or false with message
if (result.status) {
  console.log(result.data.transactionId);
} else {
  console.error(result.message);
}

Sandbox Mode

Sandbox mode simulates the payment flow without charging real money. Use test phone numbers and expect consistent responses for testing your integration.

Idempotency

Use a UUID v4 as your reference to ensure payment requests are idempotent. If a request is retried, the same reference returns the existing payment status instead of creating duplicates.

On this page