Skip to Content
Mpesajs 1.0 is released šŸŽ‰
API ReferenceAPI Reference

API Reference

Welcome to the MPesaJS API reference documentation. This SDK provides a simple and intuitive way to integrate M-Pesa payment services into your Node.js applications.

Core Components

The SDK is organized into several core components:

Auth

Handles authentication with the M-Pesa API by generating OAuth tokens using your consumer credentials.

STK Push

Initiates mobile payments by sending payment requests directly to customers’ phones.

Payout

Manages Business-to-Customer (B2C) payments, allowing businesses to send money to customer phone numbers.

URL Registration

Registers callback URLs for receiving transaction notifications and validation requests.

Error Handling

Provides specialized error types and handlers for different scenarios.

Getting Started

  1. Install the package:
npm install mpesajs
  1. Import the required components:
import { Auth, StkPush, Payout, RegisterUrl } from 'mpesajs';
  1. Initialize with your credentials:
// Using environment variables (recommended) const auth = new Auth(); const stkPush = new StkPush(auth); // Or with explicit credentials const auth = new Auth('your-consumer-key', 'your-consumer-secret'); const stkPush = new StkPush(auth, true); // true for sandbox

Environment Variables

The SDK supports configuration through environment variables:

VariableDescriptionDefault
MPESA_CONSUMER_KEYYour M-Pesa consumer key-
MPESA_CONSUMER_SECRETYour M-Pesa consumer secret-
MPESA_SANDBOXUse sandbox environment'true'
MPESA_BUSINESS_SHORTCODEYour business shortcode-
MPESA_PHONE_NUMBERDefault phone number for testing-
MPESA_INITIATOR_NAMEName of the initiator for B2C-
MPESA_SECURITY_CREDENTIALSecurity credential for B2C-
MPESA_CONFIRMATION_URLURL for transaction confirmations-
MPESA_VALIDATION_URLURL for transaction validation-
MPESA_QUEUE_TIMEOUT_URLURL for timeout notifications-
MPESA_RESULT_URLURL for transaction results-

Rate Limiting

The SDK includes built-in rate limiting to prevent API abuse. All API calls are automatically rate-limited based on M-Pesa’s guidelines.

Error Handling

All components use specialized error types that extend from MpesaError:

  • AuthenticationError: For authentication issues
  • NetworkError: For network-related problems
  • ValidationError: For input validation failures
  • StkPushError: For STK Push-specific errors
  • PayoutError: For B2C payment errors
  • RegisterUrlError: For URL registration issues

Example error handling:

try { const result = await stkPush.sendStkPush(/* ... */); } catch (error) { if (error instanceof StkPushError) { console.error('STK Push failed:', error.message); } else if (error instanceof NetworkError) { console.error('Network issue:', error.message); } else { console.error('Unexpected error:', error); } }

For detailed information about each component, please refer to their respective documentation pages.

Last updated on