Initialize the Mpesa instance
masterThe library is modular and allows for multiple instances. At a minimum, you must provide consumerKey and consumerSecret. You can also prefill configuration options like environment, shortCode, and certPath to avoid re-entering them for every call.
All methods in the Mpesa class return a Promise and can be used with .then() or await. Responses follow the Axios response structure.
const Mpesa = require('mpesa-node')
// Basic initialization
const mpesaApi = new Mpesa({
consumerKey: '<your consumer key>',
consumerSecret: '<your consumer secret'
})
// Full configuration example
const mpesaApiFull = new Mpesa({
consumerKey: '<your consumer key>',
consumerSecret: '<your consumer secret>',
environment: 'sandbox', // or 'production'
shortCode: '600111',
initiatorName: 'Test Initiator',
lipaNaMpesaShortCode: 123456,
lipaNaMpesaShortPass: '<some key here>',
securityCredential: '<credential here>',
certPath: path.resolve('keys/myKey.cert')
})