How to Generate M-Pesa Lipa na M-Pesa Access Token

To generate an M-Pesa Lipa na M-Pesa access token, you need to follow these steps:

  1. Get Consumer Key and Consumer Secret: These are provided by Safaricom once you register your application on the Safaricom Developer portal.
  2. Generate the Access Token: Make an HTTP request to the M-Pesa API to generate the access token. You will use your consumer key and secret for Basic Authentication.

Here’s a step-by-step guide to help you through the process:

Step 1: Register Your Application

  • Go to the Safaricom Developer Portal.
  • Register an account or log in if you already have one.
  • Create a new app, and you will be provided with a Consumer Key and Consumer Secret.

Step 2: Generate the Access Token

You can use tools like curl, Postman, or write a script in your preferred programming language to make the HTTP request.

Using curl

curl -X GET \
  -H "Authorization: Basic <Base64_encoded_ConsumerKey:ConsumerSecret>" \
  -H "Content-Type: application/json" \
  "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"

Replace <Base64_encoded_ConsumerKey:ConsumerSecret> with your Base64-encoded consumer key and secret.

Using Python

Here is a Python example:

import requests
from requests.auth import HTTPBasicAuth

consumer_key = "YOUR_CONSUMER_KEY"
consumer_secret = "YOUR_CONSUMER_SECRET"
api_url = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"

response = requests.get(api_url, auth=HTTPBasicAuth(consumer_key, consumer_secret))
access_token = response.json().get('access_token')

print(f"Access Token: {access_token}")

Using PHP

Here is a PHP example:

<?php
$consumerKey = 'YOUR_CONSUMER_KEY';
$consumerSecret = 'YOUR_CONSUMER_SECRET';
$credentials = base64_encode($consumerKey . ':' . $consumerSecret);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $credentials));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$access_token = json_decode($response)->access_token;
echo "Access Token: " . $access_token;
?>

Notes

  • Sandbox URL: Use https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials for the sandbox environment.
  • Production URL: Use https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials for the production environment.
  • Base64 Encoding: Ensure your consumer key and secret are Base64 encoded for the Authorization header.

Example of Base64 Encoding

If your consumer_key is myConsumerKey and consumer_secret is myConsumerSecret, you can encode them in Base64:

echo -n "myConsumerKey:myConsumerSecret" | base64

This should output the Base64 encoded string which you then use in your Authorization header.

By following these steps, you will be able to generate an M-Pesa Lipa na M-Pesa access token successfully.