M-Pesa Node API
Node.js wrapper for the M-Pesa Mozambique API.
Using the 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_keyMPESA_API_HOST=api.sandbox.vm.co.mzMPESA_API_KEY=example_api_keyMPESA_ORIGIN=developer.mpesa.vm.co.mzMPESA_SERVICE_PROVIDER_CODE=171717 -
In your JavaScript file, import the package using
require()
:const mpesa = ;
Supported Transactions
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:
Customer to Business (C2B)
const mpesa = ; mpesa ;
Business to Customer (B2C)
const mpesa = ; mpesa ;
Planned Support:
- B2B
- Reversal
- Query Transaction Status
Using custom configuration
Optionally, you can also use custom configuration to initialize the API:
const mpesa = ; mpesa;mpesa;
Getting a copy for development
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
Make sure you have installed Node.js, which comes with npm
.
Installing
- 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
.
Contributing
Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Inspired by the mpesa-php-api created by Abdul Mueid.