Welcome to the Mpesa Python SDK. This is a Python wrapper for the Safaricom Daraja API. It simplifies the process of integrating your Python applications, frameworks, and libraries with the Daraja API.
The Mpesa Python SDK currently supports the following Mpesa services:
- Mpesa Express (STK Push)
- C2B Transactions
- B2C Transactions
- Transaction Status Queries
- Reverse Transaction Requests
To use the SDK, you need to have:
- A Safaricom Developer account.
- A registered app on the Safaricom Developer portal.
- An app consumer key and consumer secret.
- A registered Mpesa-enabled SIM card.
- Python 3.10 or higher.
- Poetry for dependency management.
To install the SDK, run the following command:
pip install python-mpesa-sdk
or
poetry add mpesa-
This SDK requires the definition of the environment variables as described in the .env.example file. Ensure these variables are defined in your environment before using the SDK.
Here is a simple example of how to use the SDK to make a B2C transaction request:
# external imports
from mpesa_sdk.daraja.b2c import B2CPaymentRequest, B2CPaymentResponseParser
from mpesa_sdk.daraja.enums import CommandID
# create an instance of the B2CPaymentRequest class
b2c = B2CPaymentRequest('100', CommandID.BUSINESS_PAYMENT, 'Operator', 'Jun-17062023-IN-00200C', 'ShortCode', '2547XXXXXXXX', 'Remarks')
# make the request
response = b2c.execute()
print(f'Response: {response}')
# create an instance of the B2CPaymentResponseParser class
b2c_parser = B2CPaymentResponseParser(response)
# parse the response into a python dictionary
parsed_response = b2c_parser.parse()
print(f'Parsed response: {b2c_parser.response}')
For more information about the SDK, check out the Wiki
Contributions to the Python Mpesa SDK are always welcome. If you want to contribute, please take a moment to review the contributing guidelines to get a feel for the coding standard and development workflow.
This project is licensed under the WTFPL License - see the LICENSE file for details.
This SDK is not officially affiliated with Safaricom. Use it at your own risk. Always ensure that you adhere to the terms and conditions provided by Safaricom for use of their Daraja API.