Skip to content

Instantly share code, notes, and snippets.

Last active March 23, 2021 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M-Abdullahi/44d43583f96b894f723ccd1641f275e0 to your computer and use it in GitHub Desktop.
Save M-Abdullahi/44d43583f96b894f723ccd1641f275e0 to your computer and use it in GitHub Desktop.
This gist describes the processe of creating m-pesa test application allowing us to access the m-pesa api.

Access Safaricom's M-Pesa API

This gist describes the processes of allowing us to access the Safaricom's M-Pesa API. At the end of the process we will obtain all variable needed to start using their API. We will be using the Customer to Business (C2B) API.

This gist has been created as an Appendix to this article (part 1).

Start the Engine

⚡ Start by accessing the M-Pesa Developer Portal and create a new account if you don't already have one. Click the build new app button to scaffold a new testing application.

mpesa developer portal

Create The Application


Now within the Products category of the application creation page, you need to check the two boxes; Lipa na M-Pesa sandbox and M-Pesa sandbox to enable the APIs


After you've successfully created the application, you would be able to see your Consumer Key and Consumer Secret under the keys tab

And TADA 🎉, here are your MPESA_CONSUMER_KEY and MPESA_CONSUMER_SECRET environment variables.

Okay, what are keys for?

M-Pesa APIs are built on REST and to communicate with the API, you will need a Basic Auth over HTTPS authorization token (OAuth token). You use the keys to obtain the token.

Next, we need to get the Lipa Na Mpesa Online Shortcode and Lipa Na Mpesa Online Passkey test credentials from the this link.

You now have yourMPESA_SHORT_CODE and MPESA_PASS_KEY environment variables.

The mpesa shortcode and mpesa passkey are reset every 3 days. Don't foget to request new copies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment