Skip to main content

An mpesa-py Extension for Flask Applications.

Project description

Requirements Status Codacy Badge Coverage Status


Flask-Mpesa is a mpesa-py extension for flask Applications.


Use the package manager pip to install flask-mpesa.

pip install Flask-Mpesa


from flask import Flask
from flask_mpesa import MpesaAPI



if you are using blueprints

from flask_mpesa import MpesaAPI


Be sure to set the following variables in the app.config

app.config["API_ENVIRONMENT"] = "sandbox" #sandbox or live
app.config["APP_KEY"] = "..." # App_key from developers portal
app.config["APP_SECRET"] = "..." #App_Secret from developers portal

B2C Api

This returns a json response to your result_url.

def b2c_transact():
    data={"initiator_name": "[InitiatorName]",
            "security_credential": "[SecurityCredential]",#from developers portal
            "amount": "1000",
            "party_a": "[PartyA]",
            "party_b": "[PartyB]",
            "remarks": "[Remarks]",
            "queue_timeout_url": "YOUR_URL" ,
            "result_url": "YOUR_URL",
            "occassion": "[Occassion]"
    mpesaapi.B2C.transact(**data)  # ** unpacks the dictionary

B2B Api

This returns a json response to your result_url.

def b2b_transact():
    data={"initiator": "[Initiator]",
            "security_credential": "[SecurityCredential]",#from developers portal
            "amount": "1000",
            "party_a": "[PartyA]",
            "party_b": "[PartyB]",
            "remarks": "[Remarks]",
            "queue_timeout_url": "YOUR_URL" ,
            "result_url": "YOUR_URL",
            "account_reference": "[AccountReference]"
    mpesaapi.B2B.transact(**data)  # ** unpacks the dictionary

C2B api

def c2b_transact():
    reg_data={"shortcode": "600364",
          "response_type": "Completed",
          "confirmation_url": "",
          "validation_url": ""
    v=mpesaapi.C2B.register(**reg_data)  # ** unpacks the dictionary
    ##use v to capture the response

    #This method allows you to test a mock payment and see the result so it can be avoided in production mode.
    test_data={"shortcode": "600364",
           "command_id": "CustomerPayBillOnline",
           "amount": "100",
           "msisdn": "254708374149",
           "bill_ref_number": "account"
    new_v = mpesaapi.C2B.simulate(**test_data)  # ** unpacks the dictionary
    #use new_v to capture the response
    return render_template('home.html')

def c2b_confirmation():
    #save the data
    request_data =

    #Perform your processing here e.g. print it out...

MpesaExpress api

def c2b_transact():
    data = {
        "business_shortcode": "[BusinessShortcode]", #from developers portal
        "passcode": "[Passcode]",#from developers portal
        "amount": "[Amount]", # choose amount preferrably KSH 1
        "phone_number":"[PhoneNumber]", #phone number to be prompted to pay
        "reference_code": "[Reference Code]",#Code to inform the user of services he/she is paying for.
        "callback_url": "[YOUR_URL]", # cllback url should be exposes. for testing putposes you can route on host and set the callback url to be https://youripaddress:yourport/endpoint
        "description": "[Description]" #a description of the transaction its optional
    v = mpesaapi.MpesaExpress.stk_push(**data)  # ** unpacks the dictionary
    ##use v to capture the response
    return render_template('home.html')

def callback_url():
    #get json data set to this route
    json_data = request.get_json()
    #get result code and probably check for transaction success or failure
    #if result code is 0 you can proceed and save the data else if its any other number you can track the transaction
    return jsonify(message),200

Balance api


Reversal api


TransactionStatus api



Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Tests and Examples

Examples are comming soon!.



Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for Flask-Mpesa, version 1.2.1
Filename, size File type Python version Upload date Hashes
Filename, size Flask-Mpesa-1.2.1.tar.gz (8.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page