working with mpesa daraja Api, C2B
Requirements for this project
First you need to have registered in safaricom dev. follow this link
create your app. To get started, create an app on Sandbox. You can copy your credentials
have python installed.
install django using pip.
pip install django-mpesa
a good computer and basic python understanding.
a cup of coffee and we are good to go.
cd mysite.
python manage.py runserver.
we can see that our website is up and running on localhost/
You have 18 unapplied migrations. now lets run command.
api url In order to make an API call to Mpesa APIs, we will need to authenticate our app.
Safaricom provides an OAuth for generating an access token, which supports client_credentials grant type.
This API generates the tokens for authenticating your API calls. This is the first API you will engage with within the set of APIs available because all the other APIs require authentication information from this API to work.
Since we will be making an HTTP request to mpesa sandbox, we need a python library to make HTTP requests.
// we are going to use python request library.
pip install requests
//so far so good.
A sample code for generating and validating access token in python.
def getAccessToken(request):
consumer_key = 'your key'
consumer_secret = 'your consumer secret key'
api_URL = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'
r = requests.get(api_URL, auth=HTTPBasicAuth(consumer_key, consumer_secret))<br>
mpesa_access_token = json.loads(r.text)<br>
validated_mpesa_access_token = mpesa_access_token['access_token']<br>
return HttpResponse(validated_mpesa_access_token)<br>
For detailed explanation follow the safaricom
// working with postman.
Download and install postman
Used to test and access APIs.