A node.js wrapper for seamless integration with Mpesa's Daraja API. This library provides a builder-style interface to enhance the ease of use. Visit https://daraja-sdks.github.io to find implementations in other languages and more documentation.
- ✅ Mpesa Customer to Business (C2B)
- ✅ Mpesa Express/Lipa na Mpesa Online/STK Push
- ✅ Lipa na Mpesa Query
- ✅ Mpesa Business to Customer (B2C)
- ✅ Mpesa Transaction Status query
- ✅ Mpesa Account Balance query
- ✅ Mpesa Reversals
More In-depth documentation on library usage can be found here. Also, full-blown examples on library usage can be found at the daraja-sdks github repos.
To get started, create an instance of the Mpesa
class:
// with es-modules syntax
import { Mpesa } from "daraja.js"
// with common-modules syntax
const Mpesa = require("daraja.js").Mpesa
// create an app instance
const app = new Mpesa({
consumerKey: process.env.APP_KEY, // required
consumerSecret: process.env.APP_SECRET, // required
initiatorPassword: "Safaricom999!*!", // required only in production
organizationShortCode: 174379, // optional
certificatePath: "some/path", // optional
securityCredential: "someSecureCredential" // optional
})
// ...
const res = await app
.stkPush()
.amount(1)
.callbackURL("https://example.com/callback")
.phoneNumber(254708374149)
.lipaNaMpesaPassKey(process.env.LNM_PASSKEY)
.send();
console.log(res.isOkay());
// ...
const res = await app
.stkPush()
.shortCode("174379")
.checkoutRequestID("ws_CO_DMZ_123212312_2342347678234")
.lipaNaMpesaPassKey(process.env.LNM_PASSKEY)
.queryStatus(); // sends the query request
// ...
const res = await app
.c2b()
.shortCode("600998")
.accountNumber("Bill payment")
.amount(1)
.phoneNumber(254708374149)
.simulate();
// ...
const res = await app
.c2b()
.shortCode("600998")
.confirmationURL("https://example.com/callback")
.validationURL("https://example.com/callback")
.callbackURL("https://example.com/callback")
.registerURLS();
// ...
const res = await app
.b2c()
.amount(1)
.phoneNumber(254708374149)
.shortCode("600982")
.resultURL("https://example.com/callback")
.timeoutURL("https://example.com/callback")
.send();
// ...
const res = await app
.transactionStatus()
.shortCode("600998")
.transactionID("OEI2AK4Q16")
.timeoutURL("https://example.com/callback")
.resultURL("https://example.com/callback")
.queryStatus();
// ...
const res = await app
.accountBalance()
.shortCode("600998")
.timeoutURL("https://example.com/callback")
.resultURL("https://example.com/callback")
.query();
// ...
const res = await app
.reversal()
.amount(1)
.shortCode("600998")
.initiator("testapi")
.transactionID("OEI2AK4Q16")
.resultURL("https://example.com/callback")
.timeoutURL("https://example.com/callback")
.send();