M-Pesa API Integration - Step-by-step Guide

August 13th, 2022 by Felix Cheruiyot


M-Pesa API integration enables you to add M-Pesa as a payment method on your website or app, among other things. Learn how to integrate M-Pesa on your website with this step-by-step guide.

One of the biggest challenges with running an e-commerce store is that shoppers can go through the trouble of choosing items and filling their carts but then leave the website without completing the purchase.

This is called cart abandonment, the equivalent of a person leaving a trolley full of groceries at the till or checkout point. Every instance of this is a lost sale.

That the shopper went to the store and spent time picking items means they intended to buy. Something at the checkout point prevented them from paying and taking their shopping home. Maybe their bank card did not have enough funds and was declined.

One of the reasons for cart abandonment on e-commerce websites is the lack of accessible and more convenient ways to pay. In Kenya, with pretty much everyone owning an M-Pesa account, adding the mobile wallet to your checkout pages will ensure more Kenyans have a way to shop online. It boosts your sales.

But to add M-Pesa as a payment option on your checkout pages requires authorization and integration with the mobile wallet. It requires M-Pesa API integration.

This article will teach you how to integrate M-Pesa to your website or software application. We will show you the two easiest ways to embed M-Pesa payments on your app or website and make it easier for your customers to pay.

What is an API?

An API, which stands for Application Programming Interface, is a mechanism that allows two software applications or computer programs to communicate with each other. An API is an intermediary that enables two applications to share data in real time.

M-Pesa API integration success (1)

For example, if you sell goods or services on your website and would like to accept payments through M-Pesa, the M-Pesa API would allow you to embed an M-Pesa payment link (with logo) on your checkout page, as shown above.

Embedding M-Pesa on your checkout pages will enable your customers to pay through their M-Pesa mobile wallets when purchasing items on your website.

But the M-Pesa API requires integration on your website or application before you can start accepting payments through it.

Why M-Pesa API Integration is Essential for Businesses in Kenya

The digital payment space is maturing, and users demand a seamless experience across apps and services. This drives the need for enterprises to integrate payments as a feature within their apps or web services.

To achieve this, businesses can leverage APIs to create a frictionless user experience while integrating mobile payments. By using APIs, developers can easily build integrations without storing confidential information.

A good example of this would be the M-Pesa API integration, which lets businesses leverage the power of M-Pesa in their products and services. As far as convenient ways of paying for goods and services in Kenya go, there are no better options than M-Pesa.

How M-Pesa Works

M-Pesa is a mobile wallet system that provides banking services through a mobile phone SIM card. You must have a registered, active Safaricom mobile phone subscription to be eligible for this service.

The M-Pesa mobile wallet operates through a mobile phone menu. You have to register for the M-Pesa service. To open the M-Pesa menu, you have to dial the *334# short code on your mobile phone.

The M-Pesa menu opens options for sending money to friends and family, buying airtime, and paying for goods and services. You can now link your M-Pesa to your PayPal account to receive international payments and top up your account.

Through M-Pesa, people with no bank accounts can access banking services, which has boosted financial inclusion in Kenya.

To top up their M-Pesa mobile wallets, subscribers can use any of the service’s agents scattered all over Kenya. Those agents take cash from M-Pesa users and convert it to a digital balance that gets credited to the depositor’s mobile wallet.

How to Integrate M-Pesa into Your Website or App

You can integrate the M-Pesa API into your website in a few ways. This article will cover the two that will set you up to accept M-Pesa payments on your website or app quickly and painlessly.

Method #1 - M-Pesa Integration through Daraja API

Since Safaricom launched M-Pesa in 2007, the mobile wallet service has won the hearts of many in Kenya and Tanzania. M-Pesa’s allure is that it affords every user a seamless deposit, withdrawal, transfer, and even airtime recharge through its M-Shwari or Fuliza service.

More conveniently, M-Pesa allows users to pay for goods and services in shops and online with their mobile wallets. Almost every business organization has a Lipa na M-Pesa till number or pay bill number to accept payments from customers and other business organizations.

But M-Pesa was slow to wise up to the potential of e-commerce and did not make it easy for developers to embed the service in their web applications. And that slowed its adoption in e-commerce.

Software developers wanted an API to embed M-Pesa directly on websites and apps. Safaricom eventually heeded the call by developing the M-Pesa G2 API. However, this API was a pain to integrate. Some developers found the API integration too complex, while others lacked the infrastructure and expertise to make it work.

The M-Pesa G2 API integration had stringent requirements and was far from seamless. Integration required a VPN connection and the use of the SOAP protocol, to mention but a few of the issues.

Following the difficulties people faced integrating the M-Pesa G2 API, Safaricom introduced a new M-Pesa API called the ‘Daraja API’. Daraja means bridge in the Swahili language, implying an attempt to bridge the gap between the web developers who lacked the know-how to integrate API integration know-how and those that did not have the infrastructure.

With this new Daraja API, you now have access to the public internet space - a significant improvement from the G2 API that required a VPN connection and soap protocol for integration.

The Daraja API incorporated the good features of the previous M-Pesa API. It also introduced interesting new ones, such as transaction status, account balance, and Lipa na M-Pesa Online.

How to integrate the Daraja M-Pesa API into your website, step-by-step

Below we will take you through a step-by-step process of integrating M-Pesa into your website or app using the Daraja API.

1. Create an account on the M-Pesa G2 Portal

Creating an account on the M-Pesa G2 portal allows you to get a One-Time Password (OTP) that you will use to test if the API integration was a success. This procedure is necessary if one must access pay bill number transactions.

Note that setting up the M-Pesa portal requires a Windows internet explorer browser that is certified by Safaricom. So, you need to request a certificate before using your Windows internet explorer browser.

To get the certification for your web browser from Safaricom, send a blank email to [email protected].

Safaricom will respond to the email automatically with a link for you to request a certificate. Click on the link to install the certificate in Windows Internet Explorer.

2. Create an app on the M-Pesa Developer portal

The process for this is simple. You go to M-Pesa Developer Portal and register for an account. After that, you will be led through the process of creating the app. This app will be used to access the M-Pesa API.

3. Write C2B, B2C, or B2B code

This is where things get a little complicated because you have to write the code yourself. But the good news is you can pick any programming language you are familiar with to write the code.

If you have never written code before, this is the part where you call in the experts. You should hire an M-Pesa API developer who will go through all the steps in this tutorial and do all the processes for you.

If you engage a developer for the M-Pesa API integration, you will only be required to apply for a pay bill account. The developer will take care of the rest.

4. It’s time to go live

This is where you go live. However, ensure that all your codes are running properly before you do this. The smallest errors can cause a disaster to your integration process.

Go to the developer portal and toggle the go live button. With that, the M-Pesa API will be live. After this, you will be required to verify the API's ownership. If it is yours, you should have no issues with going live.

To verify ownership, you must follow the steps below:

Once this is done, input the URL endpoint to the action button with which a client will interact on your website. Test if the integration was successful by clicking on the button to initiate payments and pay before making your site or app live for customers to make payments.

Pros of using Daraja M-Pesa API

Here are some advantages to using the Daraja M-Pesa API:

The above pros only apply if having a direct relationship with Safaricom is a business requirement.

Cons of using Daraja M-Pesa API

There are also a few disadvantages to using the Daraja M-Pesa API. Let’s quickly talk about those:

Using your own pay bill number can delay time to market. You must register for B2C and C2B pay bill numbers based on your use case. This can take some time to complete.

Lengthy process to get started. You need to register directly with Safaricom M-Pesa, upload test cases, and get approval, which can take longer than the expected 2 weeks to 1 month,

Lack of SDKs means developers have to do a lot of work to get started and make the API integration work.

In light of all this, we propose a second option:

Method #2 - M-Pesa API Integration using IntaSend SDKs

Integrating any API is never easy, especially when using a process designed by people who lack a genuine desire to make things easy for you.

With IntaSend, integrating the M-Pesa API into your websites is shorter and much less complicated. Essentially, we’ve done the heavy lifting for you.

With our process, you will use the SDKs we provide you to connect the M-Pesa API to your mobile/web app. This way, you can start taking payments through M-Pesa on your website or app as quickly as possible.

If you choose to integrate the M-Pesa API through IntaSend, you will also have access to the M-Pesa B2B API, which enables B2B payments within your application for easy operation.

So, what if you encounter issues while trying to integrate? Our team of developers is just a phone call or live chat away. They will fix any issues you may encounter.

Why use IntaSend for M-Pesa API integration?

Besides those we have discussed above, we can think of other reasons you must use IntaSend for M-Pesa API integration:

1. Easy setup and activation

It is usually not possible to set up an account, integrate the API, and go live the same day with the Daraja M-Pesa API integration process. But you absolutely can with IntaSend.

IntaSend allows you to set up your account and activate it as soon as possible to start collecting and disbursing payments without waiting an extra day. Our tools and SDKs will enable you to initiate transactions, track them, and know their statuses.

Below is a list of our M-Pesa API SDKs;

WordPress and WooCommerce (Collect money - M-Pesa STK push)

PHP SDK (Send money, M-Pesa B2B API, Collect money - M-Pesa STK push)

No-code Bubble.io (Send money, M-Pesa B2B API, Collect money, M-Pesa STK push)

Python SDK (Send money, M-Pesa B2B API, Collect money - M-Pesa STK push)

REST API (Send money, M-Pesa B2B API, Collect money - M-Pesa STK push)

Javascript Inline SDK (Collect money - M-Pesa STK push).

2. Ability to send single and bulk payments seamlessly

One of the things our customers love about the IntaSend M-Pesa API is how you can perform up to 5000 transactions in a single request. Whether you’re settling payroll payments or suppliers’ bills, you can do it all with a single request.

3. M-Pesa STK push

Our M-Pesa Express STK push is a good alternative to card payments. You can use the API to push payment requests to your clients.

4. Secure and scalable

IntaSend has some of the best payment security standards in the payments processing industry. IntaSend’s APIs (including the M-Pesa API) are PCI DSS compliant. IntaSend also deploys PSD2 standards to provide extra security and authentication of payment requests. As far as payment security goes, that’s as good as it gets.

5. Easy documentation

You don’t need to wait longer than necessary to set up your API. And you can do it without the complicated and laborious processes. You can go to your sandbox to test your API keys and begin your API implementation as soon as possible.

To learn more about this, read our Developers API Documentation.

What Can You Do With IntaSend’s M-Pesa API Integration?

So, what does integrating the M-Pesa API to your website via IntaSend do for you? Here’s what it allows your business:

1. Pay salaries straight to employees M-Pesa mobile wallets

IntaSend's M-Pesa API Integration provides an ideal mechanism for employers to pay their staff salaries and other ad-hoc expenses through their mobile phones. The API comes with bulk and single payment options for your business disbursements.

M-Pesa makes salary payments easy for business owners and convenient for their employees. Better still, it significantly reduces the cost of making salary payments.

M-Pesa offers a secure way of paying staff members who do not have a bank account. Such employees can easily use their M-Pesa mobile wallets to receive their salary and keep their savings. They can withdraw or pay for goods and services using their M-Pesa Visa card if they wish.

2. Automate bill payment

IntaSend M-Pesa API integration allows you to automate bill payments. This is made easy through our B2B API which removes the pain that comes with making repeated payments to suppliers and vendors.

You can simply set your account to pay certain vendors a set amount at a specific date every month.

This process is automated so that all payments received through M-Pesa are instantly reconciled on the platform, providing an option to add payments manually.

3.Enable M-Pesa Express (STK Push) API for e-commerce

With IntaSend's M-Pesa express STK Push API, merchants can conduct mobile money transactions and push their details to a customer's mobile phone.

This API enables a merchant to initiate an M-Pesa transaction on a customer's behalf. The customer does not need to enter the merchant’s information or any other transaction details.

The M-Pesa Express API simplifies M-Pesa transactions for your customers. The customer must hit one button to approve the payment instead of a long, complicated transaction process.

Another benefit of the M-Pesa Express API is the real-time functionality it offers. Transactions take place instantly. There is no waiting time after customer details are punched in.

Lastly, the M-Pesa Express API ensures maximum security throughout your transactions. It protects both your systems and your customers’ funds.

Pros of using IntaSend to integrate M-Pesa API on your website

Faster time to market. You don’t need your own pay bill to get started. IntaSend allows you to use its own pay bill numbers.

With the managed service, your business gets better support. IntaSend APIs are made scalable and fault tolerant. The support team is available 24/7 to attend to any issues you may encounter.

  1. IntaSend provides SDKs and no-code tools for developers and businesses to get started easily. IntaSend has been built from the ground up by developers who understand the actual pain points of the MPesa API Integration.
  2. Second SMS and Webhooks service enable businesses to brand responses and receipts for their clients.
  3. Get access to other payment options and give users more ways to pay and get paid. This includes card and bank payments.

Learn more about the benefits of using IntaSend for your business payments here.

Cons of using IntaSend API to integrate M-Pesa API on your website

Integrate M-Pesa On Your Website And Streamline Your Business Payments With IntaSend

IntaSend provides businesses an easier and faster way to automate M-Pesa payments on their web applications. We offer the easiest way to integrate the M-Pesa API on websites or apps. You don’t need any coding skills to complete the API integration with our platform.

IntaSend M-Pesa API integration helps you reduce time to market. Developers can embed M-Pesa payments and go live without the delays that might be caused by the long Daraja API approval and onboarding processes.

In addition to M-Pesa support, IntaSend offers other standard payment options with our business accounts. These payment methods include Visa and Mastercard card payments, bank transfers, and Bitcoin payments.

Take the pain out of receiving payments from clients and making payments to vendors, suppliers, and employees with IntaSend. Sign up today to enjoy fast and smooth money transfers and business payments.

© 2024 IntaSend. All rights reserved.