Node.js wrapper for the M-Pesa Mozambique API.
-
Install it using npm:
npm install mpesa-node-api
-
Create the configuration
.env
file on your root directory based on.env.example
. -
Use your favorite text editor to edit the
.env
file and fill in the blank lines with configuration you got from the M-Pesa Developer Portal. See an example:MPESA_PUBLIC_KEY=example_public_key MPESA_API_HOST=api.sandbox.vm.co.mz MPESA_API_KEY=example_api_key MPESA_ORIGIN=developer.mpesa.vm.co.mz MPESA_SERVICE_PROVIDER_CODE=171717
-
In your JavaScript file, import the package using
require()
:const mpesa = require('mpesa-node-api');
All transactions return a Promise.
So you can either use then/catch
or async/await
to get the transaction response.
Responses match the ones specified on the M-Pesa API Documentation.
Example C2B response:
{
"output_ConversationID": "f02f957c19f4499faf6a6f19c0307e69",
"output_ResponseCode": "INS-0",
"output_ResponseDesc": "Request processed successfully",
"output_ThirdPartyReference": "ZXVM9H",
"output_TransactionID": "f449abol7j38"
}
const mpesa = require('mpesa-node-api');
mpesa.initiate_c2b(/* amount */ 10, /* msisdn */ 258843330333, /* transaction ref */ 'T12344C', /*3rd party ref*/ 'ref1')
.then(function(response) {
// logging the response
console.log(response);
})
.catch(function(error) {
// TODO: handle errors
});
const mpesa = require('mpesa-node-api');
mpesa.initiate_b2c(/* amount */ 10, /* msisdn */ 258843330333, /* transaction ref */ 'T12344C', /*3rd party ref*/ 'ref1')
.then(function(response) {
// logging the response
console.log(response);
})
.catch(function(error) {
// TODO: handle errors
});
- B2B
- Reversal
- Query Transaction Status
Optionally, you can also use custom configuration to initialize the API:
const mpesa = require('mpesa-node-api');
mpesa.initializeApi({
baseUrl: "YOUR_MPESA_API_HOST",
apiKey: "YOUR_MPESA_API_KEY",
publicKey: "YOUR_MPESA_PUBLIC_KEY",
origin: "YOUR_MPESA_ORIGIN",
serviceProviderCode: "YOUR_MPESA_SERVICE_PROVIDER_CODE"
});
mpesa.initiate_c2b( 10, 258843330333, 'T12344C', 'ref1');
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Make sure you have installed Node.js, which comes with npm
.
- Fork the GitHub repository.
- Clone it to your local machine using
git clone https://github.com/<YOUR-USERNAME>/mpesa-node-api.git
- Navigate into the project's directory using
cd mpesa-node-api
; - Install dependencies using
npm run install
.
Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.
This project is licensed under the MIT License - see the LICENSE file for details.
Inspired by the mpesa-php-api created by Abdul Mueid.