Nylon PayNylon Pay

Your First Payment

Collect your first payment with the Nylon Pay SDK

Prerequisites: You have already installed the SDK and have your sandbox API keys.

Complete Example

import { createNylonPay } from '@nile-squad/nylonpay-ts';

const nylonpay = createNylonPay({
  apiKey: 'npk_sandbox_...',
  apiSecret: 'nps_sandbox_...',
});

// Start a payment
const payment = await nylonpay.collectPayment({
  amount: 5000,
  currency: 'UGX',
  customer: { name: 'John Doe', phoneNumber: '256700000000' },
  description: 'Order #1234',
  reference: crypto.randomUUID(),
});

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

payment.on('processing', () => {
  console.log('Awaiting customer PIN...');
});

payment.on('success', ({ transaction }) => {
  console.log('Payment successful!', transaction?.id);
});

payment.on('failed', ({ error }) => {
  console.log('Payment failed:', error);
});

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

How It Works

  1. Request sent: SDK authenticates and sends payment request to Nylon Pay backend
  2. Processing: Backend forwards to provider (MTN MoMo, Airtel Money, etc.)
  3. Customer action: Provider sends USSD/STK prompt to customer's phone
  4. Confirmation: Customer enters PIN on their phone
  5. Webhook: Provider confirms to Nylon 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.namestringCustomer full name
customer.phoneNumberstringCustomer phone number with country code (e.g., +256...)
descriptionstringPayment description shown to customer

Optional Fields

FieldTypeDescription
referencestringno
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

try {
  const transaction = await payment.wait();
  console.log('Payment successful:', transaction.id);
} catch (error) {
  console.error('Payment failed:', error);
}

Sandbox Mode

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

See Testing guide for how to simulate different outcomes (success, failure, timeout).

Idempotency

The reference field prevents duplicate charges. If a request fails and you retry with the same reference, the SDK returns the existing payment instead of creating a new one.

A supplied reference must be a valid UUID of any version. The SDK generates a UUID v4 when you omit reference. Never reuse the same reference for different payments.

On this page