Production-ready M-Pesa (Daraja API) SDK for Python
Project description
💳 mpesa-wrapper
Production-ready M-Pesa (Daraja API) SDK for Python.
A type-safe, robust, and modern wrapper for Safaricom's M-Pesa APIs, designed for both synchronous and asynchronous applications.
🚀 Features
- Stripe-level DX: Type-safe responses with Pydantic (full autocomplete).
- Production-First: Automatic phone number masking in logs, RSA security credential generation, and config protection.
- Resilient: Built-in exponential backoff retries for network and server errors.
- Idempotency: Automatic
X-Idempotency-Keymanagement for critical transactions. - Async Support: Native
AsyncMpesaClientfor FastAPI and other async frameworks. - Comprehensive: Supports STK Push, B2C, C2B, Reversal, Transaction Status, and Account Balance.
📦 Install
pip install mpesa-wrapper
For web-related features (FastAPI demo):
pip install "mpesa-wrapper[web]"
⚡ Quick Start
from mpesa_wrapper import MpesaClient
# Initialize from environment variables
client = MpesaClient.from_env()
# STK Push (Lipa Na M-PESA Online)
response = client.stk_push(
phone_number="254712345678",
amount=1,
account_reference="Order123",
transaction_desc="Payment for shoes"
)
# Type-safe access with autocomplete!
if response.response_code == "0":
print(f"Success! CheckoutID: {response.checkout_request_id}")
🔐 Configuration
Create a .env file based on .env.example:
MPESA_CONSUMER_KEY=your_key
MPESA_CONSUMER_SECRET=your_secret
MPESA_SHORTCODE=174379
MPESA_PASSKEY=your_passkey
MPESA_ENV=sandbox
# Required for B2C, Reversal, Balance, Status
MPESA_INITIATOR_NAME=testapi
MPESA_INITIATOR_PASSWORD=your_password
MPESA_CERTIFICATE_PATH=./path/to/cert.cer
MPESA_RESULT_URL=https://your-domain.com/result
MPESA_TIMEOUT_URL=https://your-domain.com/timeout
🐳 Running the Demo (Docker)
docker build -t mpesa-demo .
docker run -p 8000:8000 --env-file .env mpesa-demo
📄 License
Distributed under the MIT License. See LICENSE for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mpesa_wrapper-1.0.0.tar.gz.
File metadata
- Download URL: mpesa_wrapper-1.0.0.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72c486f7973c444ebe1baf17c33910ce8a6cfc84247f2a18737d2d186d227a08
|
|
| MD5 |
581f6127a060b9bb25e9593d8e8c1078
|
|
| BLAKE2b-256 |
689f3548d00be5768be7c4b71a22ba0747461527414c7c0782bd8c39130c93e4
|
File details
Details for the file mpesa_wrapper-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mpesa_wrapper-1.0.0-py3-none-any.whl
- Upload date:
- Size: 21.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10f667f8fc370226ba73001d3db70cc4504539afabaab75ee76438af06cc1cda
|
|
| MD5 |
79e607fdb57559a0c45dbb96095b0466
|
|
| BLAKE2b-256 |
25762f384eb7d0c4290f6e9cd884ebd232ed8eb0b28288b4ef4f3d6212c95213
|