MPESA Daraja 2.0 API Python Client Module
This python module provides a simple way to integrate the Safaricom MPESA Daraja 2.0 API into your Python projects. It is designed to handle common operations like generating access tokens, formatting phone numbers, and sending STK Push requests.
Features
- Generate access tokens securely.
- Format and validate phone numbers.
- Send STK Push requests for payments.
- Easily configurable with environment variables.
🛠️ Installation
From PyPI
Install the library directly from PyPI:
pip install daraja-client
From Source
-
Clone the repository:
git clone https://github.com/anomalous254/daraja_client.git
-
Navigate to the project directory:
cd daraja_client
-
Install dependencies:
pip install -r requirements.txt
Configuration
Add the following variables to your .env file in the root directory of your project:
DARAJA_API_CONSUMER_KEY='your_consumer_key'
DARAJA_API_CONSUMER_SECRET='your_consumer_secret'
DARAJA_API_PASS_KEY='your_pass_key'
DARAJA_API_SHORT_CODE='your_shortcode'
Usage
Import the Library
from daraja_client.core import DarajaClient
from decouple import config
# Configuration
auth_url = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"
stk_push_url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest'
call_back_url = 'https://your-callback-url.com' #
phone_number = '+254769507699' # person to receive the prompt
amount = '1'
# Initialize the client
cl = DarajaClient(
auth_url=auth_url,
consumer_key=config('DARAJA_API_CONSUMER_KEY'),
consumer_secret=config('DARAJA_API_CONSUMER_SECRET'),
pass_key=config('DARAJA_API_PASS_KEY'),
shortcode=config('DARAJA_API_SHORT_CODE'),
phone_number=phone_number,
call_back_url=call_back_url,
amount=amount
)
# Send STK Push
response = cl.send_stk_push(stk_push_url=stk_push_url)
print(response)
Response Examples
Success
{
"message": "success",
"stkpushID": "ws_CO_blahblahblah",
"info": "You can use and store this stkpushID in your db model to be used for payment confirm during callback from Safaricom"
}
Failure
{
"message": "failed",
"error": "Wrong credentials"
}
Acknowledgment
This library was developed by Peter Nyando, a passionate software engineer dedicated to creating scalable and efficient solutions. Check out more of my work on my portfolio.
Ensure your .env file is properly configured.
Contributing
- Fork the repository.
- Create a new branch.
- Make your changes and commit them.
- Push to your fork and submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Metadata
Release files for daraja-client 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| daraja_client-0.1.3.tar.gz | 5.7 kB | Without provenance | Details for daraja_client-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| daraja_client-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for daraja_client-0.1.3-py3-none-any.whl |
Total release size: 12.5 kB
Release files / daraja_client-0.1.3.tar.gz
| Download URL | daraja_client-0.1.3.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e9bfeaebfd6b02be5f1c577d680ab1b41faf051ba53dae4d4df08c29dee7709
|
|
BLAKE2b-256 checksum How to use checksums |
c8be926057c62e39baa0d6be06851dfd7148ba10157379f0ac84ef844ae37a9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|
Release files / daraja_client-0.1.3-py3-none-any.whl
| Download URL | daraja_client-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
22357e5f1017343d8a6eae708f68715d15f9ba05e1e92648094660a1f06e3e16
|
|
BLAKE2b-256 checksum How to use checksums |
2b1e50825475dbaa8bdd5ccd935aef732a4bec77f59a325fad06c6c42da20e00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|
Security for daraja-client 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.