{"id":20065945,"url":"https://github.com/allansifuna/flask-mpesa","last_synced_at":"2025-10-09T00:24:01.310Z","repository":{"id":57430556,"uuid":"224614997","full_name":"allansifuna/Flask-Mpesa","owner":"allansifuna","description":"Flask-Mpesa provides a simple intergration for flask Applications with Mpesa Daraja API.","archived":false,"fork":false,"pushed_at":"2024-01-11T21:35:19.000Z","size":159,"stargazers_count":18,"open_issues_count":2,"forks_count":11,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-15T10:54:07.666Z","etag":null,"topics":["allansifuna","cicd","flask-applications","flask-mpesa","githubactions","hacktoberfest","mpesa-daraja-api","pypi","python3"],"latest_commit_sha":null,"homepage":"https://developer.safaricom.co.ke/docs","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/allansifuna.png","metadata":{"files":{"readme":"README.md","changelog":"Changes.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-28T09:17:56.000Z","updated_at":"2025-07-31T07:21:26.000Z","dependencies_parsed_at":"2024-11-13T13:53:45.275Z","dependency_job_id":"a2d216c3-7c1b-4967-89e5-33bb212ccf56","html_url":"https://github.com/allansifuna/Flask-Mpesa","commit_stats":{"total_commits":74,"total_committers":3,"mean_commits":"24.666666666666668","dds":0.2567567567567568,"last_synced_commit":"07b2a2afbd0bdce5e1e0adc71cf35e0660e48c7f"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/allansifuna/Flask-Mpesa","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allansifuna%2FFlask-Mpesa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allansifuna%2FFlask-Mpesa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allansifuna%2FFlask-Mpesa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allansifuna%2FFlask-Mpesa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allansifuna","download_url":"https://codeload.github.com/allansifuna/Flask-Mpesa/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allansifuna%2FFlask-Mpesa/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000718,"owners_count":26082879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["allansifuna","cicd","flask-applications","flask-mpesa","githubactions","hacktoberfest","mpesa-daraja-api","pypi","python3"],"created_at":"2024-11-13T13:53:32.909Z","updated_at":"2025-10-09T00:24:01.276Z","avatar_url":"https://github.com/allansifuna.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"| Key | Badge |\n| ------------- | ------------- |\n| Testing Status | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/95668732c0014077abf06e7826c1becf)](https://www.codacy.com/manual/allansifuna/Flask-Mpesa?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=allansifuna/Flask-Mpesa\u0026amp;utm_campaign=Badge_Grade) [![Coverage Status](https://coveralls.io/repos/github/allansifuna/Flask-Mpesa/badge.svg?branch=master)](https://coveralls.io/github/allansifuna/Flask-Mpesa?branch=master) |\n| Size | ![Code size](https://img.shields.io/github/languages/code-size/allansifuna/Flask-Mpesa?color=dark-green)|\n| Compatibility | ![Top language](https://img.shields.io/github/languages/top/allansifuna/Flask-Mpesa) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/Flask-Mpesa?color=dark-green) ![PyPI - Implementation](https://img.shields.io/pypi/implementation/Flask-Mpesa?color=blue) |\n| Version Info. | ![PyPI](https://img.shields.io/pypi/v/Flask-Mpesa)  ![PyPI-Downloads](https://img.shields.io/pypi/dw/flask-mpesa?color=blue\u0026label=PyPI-Downloads) |\n| Licence | ![GitHub](https://img.shields.io/github/license/allansifuna/Flask-Mpesa?color=dark-green) |\n\n# Flask-Mpesa\nFlask-Mpesa provides a simple intergration for flask Applications with Mpesa Daraja API.\n\n## Installation\n\nUse the package manager [pip](https://pip.pypa.io/en/stable/) to install flask-mpesa.\n\n```bash\npip install Flask-Mpesa\n```\n\n## QuickStart\n\n```python\nfrom flask import Flask\nfrom flask_mpesa import MpesaAPI\n\napp=Flask(__name__)\n\n\nmpesa_api=MpesaAPI(app)\n```\n\n## if you are using blueprints\n```python\nfrom flask_mpesa import MpesaAPI\nmpesa_api=MpesaAPI()\n\nmpesa_api.init_app(app)\n```\n### Be sure to set the following variables in the app.config\n\n```python\napp.config[\"API_ENVIRONMENT\"] = \"sandbox\" #sandbox or production\napp.config[\"APP_KEY\"] = \"...\" # App_key from developers portal\napp.config[\"APP_SECRET\"] = \"...\" #App_Secret from developers portal\n```\n\n## Usage\nFor the api requests to be processed by safaricom, they need to be secure. This means that your urls should\nuse https instead of http protocal. I recommend use of a port tunneling app like Ngrok.\n\n## Sample Credentials\nFor testing your application, You should acquire test cedentials from [Daraja API's Portal](https://developer.safaricom.co.ke)\nbut if you the credentials don't work for you, you can use the credentials below:-\n\n| Key  | Value |\n| ------------- | ------------- |\n| app_key | vbxsneeZ9IMFoyKKIgOIQQZFlawAADnP |\n| app_secret | WAzDhQVhitIXwiTc |\n| initiator_name  | testapi364  |\n| party_a | 600364 |\n| security_credential | TziD/ydlT52Fm6SOH1ebrzUFwy3cP6OGplsrWja+X/1roQy2AzMsj5QGuqu9O+IFR1E6l16Jm87tg4bhnxoIhAufCEWusQI1wJZ6YLzpN0cHZAY/8SN1JfHdgEkrmksAY14pejHyfntyLT9Sg51kBjaj6J7/2+gHl2e64klnJAhlfPJWxC18zwEzsg58zFmypcovPPB6MHkPLyHQNFbu4oXC0e2gkZrIAWXTNN7PpYt4m/w39s5txU7/6P7hTzXgYAgqk4kxfPBIBeEmKhH5tSGxMD+xnSpZIXLovFgopexq8S76pmdLMjr2CdR60GlwXnAPnKJ5U9CIxRRewuoksQ== |\n| business_shortcode | 174379 |\n| passcode | bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919 |\n\nNOTE: These credentials are for a sample sandbox application and cannot be used in production.\n\n### B2C  Api\nThis returns a json response to your result_url.\n\n```python\n@app.route('/transact/b2c',methods=['GET'])\ndef b2c_transact():\n    data={\"initiator_name\": \"[InitiatorName]\",\n            \"security_credential\": \"[SecurityCredential]\",#from developers portal\n            \"amount\": \"1000\",\n            \"command_id\":\"[command_id]\",\n            \"party_a\": \"[PartyA]\",\n            \"party_b\": \"[PartyB]\",\n            \"remarks\": \"[Remarks]\",\n            \"queue_timeout_url\": \"YOUR_URL\" ,\n            \"result_url\": \"YOUR_URL\",\n            \"occassion\": \"[Occassion]\"\n    }\n    mpesa_api.B2C.transact(**data)  # ** unpacks the dictionary\n\n\n```\n\n### B2B  Api\nThis returns a json response to your result_url.\n\n```python\n@app.route('/transact/b2b')\ndef b2b_transact():\n    data={\"initiator\": \"[Initiator]\",\n            \"security_credential\": \"[SecurityCredential]\",#from developers portal\n            \"amount\": \"1000\",\n            \"command_id\":\"[command_id]\",\n            \"sender_identifier_type\":\"[SenderIdentifierType]\",\n            \"receiver_identifier_type\":\"[ReceiverIdentifierType]\",\n            \"party_a\": \"[PartyA]\",\n            \"party_b\": \"[PartyB]\",\n            \"remarks\": \"[Remarks]\",\n            \"queue_timeout_url\": \"YOUR_URL\" ,\n            \"result_url\": \"YOUR_URL\",\n            \"account_reference\": \"[AccountReference]\"\n    }\n    mpesa_api.B2B.transact(**data)  # ** unpacks the dictionary\n\n```\n\n### C2B  api\n\n```python\n@app.route('/transact/c2b')\ndef c2b_transact():\n    reg_data={\"shortcode\": \"600364\",\n          \"response_type\": \"Completed\",\n          \"confirmation_url\": \"https://example.com/confirmation\",\n          \"validation_url\": \"https://example.com/validation\"\n    }\n    v=mpesa_api.C2B.register(**reg_data)  # ** unpacks the dictionary\n    ##use v to capture the response\n\n\n    #This method allows you to test a mock payment and see the result so it can be avoided in production mode.\n    test_data={\"shortcode\": \"600364\",\n           \"command_id\": \"CustomerPayBillOnline\",\n           \"amount\": \"100\",\n           \"msisdn\": \"254708374149\",\n           \"bill_ref_number\": \"account\"\n    }\n    new_v = mpesa_api.C2B.simulate(**test_data)  # ** unpacks the dictionary\n    #use new_v to capture the response\n    return render_template('home.html')\n\n@app.route('/confirmation',methods=[\"POST\"])\ndef c2b_confirmation():\n    #save the data\n    request_data = request.data\n\n    #Perform your processing here e.g. print it out...\n    print(request_data)\n\n```\n\n### MpesaExpress  api\n\n```python\n@app.route('/transact/mpesaexpress')\ndef simulate_stk_push():\n    data = {\n        \"business_shortcode\": \"[BusinessShortcode]\", #from developers portal\n        \"passcode\": \"[Passcode]\",#from developers portal\n        \"amount\": \"[Amount]\", # choose amount preferrably KSH 1\n        \"phone_number\":\"[PhoneNumber]\", #phone number to be prompted to pay\n        \"reference_code\": \"[Reference Code]\",#Code to inform the user of services he/she is paying for.\n        \"callback_url\": \"[YOUR_URL]\", # cllback url should be exposes. for testing putposes you can route on host 0.0.0.0 and set the callback url to be https://youripaddress:yourport/endpoint\n        \"description\": \"[Description]\" #a description of the transaction its optional\n    }\n    resp = mpesa_api.MpesaExpress.stk_push(**data)  # ** unpacks the dictionary\n    ##use resp to capture the response\n    return render_template('home.html')\n\n@app.route('/callback-url',methods=[\"POST\"])\ndef callback_url():\n    #get json data set to this route\n    json_data = request.get_json()\n    #get result code and probably check for transaction success or failure\n    result_code=json_data[\"Body\"][\"stkCallback\"][\"ResultCode\"]\n    message={\n        \"ResultCode\":0,\n        \"ResultDesc\":\"success\",\n        \"ThirdPartyTransID\":\"h234k2h4krhk2\"\n    }\n    #if result code is 0 you can proceed and save the data else if its any other number you can track the transaction\n    return jsonify(message),200\n\n```\n### Balance  api\n\n```python\n@app.route('/transact/balance')\ndef balance():\n    data = {\"initiator\": \"\",\n            \"security_credential\": \"\",\n            \"command_id\": \"AccountBalance\",\n            \"party_a\": \"\",\n            \"identifier_type\": \"\",\n            \"remarks\": \"\",\n            \"queue_timeout_url\": \"\",\n            \"result_url\": \"\"\n            }\n    balance_response = mpesa_api.Balance.get_balance(**data)  # ** unpacks the dictionary\n\n    # use balance_response to capture the response\n\n```\n\n### TransactionStatus  api\n\n```python\n@app.route(\"/transaction-status\")\ndef transaction_status():\n    data = {\"initiator\": \"\",\n            \"transaction_id\": \"\",\n            \"party_a\": \"\",\n            \"security_credential\": \"\",\n            \"identifier_type\": \"\",\n            \"remarks\": \"\",\n            \"queue_timeout_url\": \"\",\n            \"result_url\": \"\",\n            \"occassion\": \"\"\n            }\n    status = mpesa_api.TransactionStatus.check_transaction_status(**data)\n    # use status to capture the response\n\n```\n## Contributing\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\n[MIT](https://github.com/allansifuna/Flask-Mpesa/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallansifuna%2Fflask-mpesa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallansifuna%2Fflask-mpesa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallansifuna%2Fflask-mpesa/lists"}