Introduction
M-Pesa Box is a reliable, MIT-licensed SDK that wraps the Safaricom Daraja APIs — STK Push, C2B, B2C and reconciliation — so you can accept M-Pesa in any Node.js app. It powers Cashly under the hood and is free to self-host.
Installation
Install from npm. It works in any Node.js environment.
Quickstart
Initialise the client with your Daraja credentials and send your first STK push.
Configuration
Get your Consumer Key, Consumer Secret, Shortcode and Passkey from the Safaricom Daraja portal. Keep these on your server — never ship them to the browser.
Store credentials in environment variables. The SDK fetches and caches the OAuth token for you and refreshes it before expiry.
STK Push
Prompt a customer's phone for their M-Pesa PIN (Lipa na M-Pesa Online). The result is delivered to your callbackUrl.
| Field | Type | Description |
|---|---|---|
| phone | string | Customer MSISDN in 2547XXXXXXXX format. |
| amount | number | Amount in KES (whole shillings). |
| ref | string | Account reference shown to the customer. |
C2B & callbacks
Handle the callback M-Pesa posts after a transaction. The SDK parses and validates the payload so you can fulfil idempotently.
Methods
| Method | Description |
|---|---|
| stkPush() | Trigger a customer STK prompt (Lipa na M-Pesa Online). |
| c2b() | Register URLs and handle customer-to-business payments. |
| b2c() | Send business-to-customer disbursements. |
| query() | Query STK / transaction status for reconciliation. |