A Python SDK for integrating with M-Pesa APIs
Project description
mpesa-daraja-sdk
⚡ Effortless M-Pesa integration using Safaricom's Daraja API — built for developers, by developers.
The Problem
Integrating Safaricom's M-Pesa Daraja API directly is notoriously complex:
- Confusing and inconsistent documentation
- Manual handling of OAuth2 tokens and security credentials
- Complex encryption and certificate management
- Different endpoints for sandbox vs production environments
- STK Push, C2B, B2C, balance — all feel like separate APIs
- Time-consuming setup that delays your time-to-market
For many developers and startups, this becomes a huge barrier to adopting M-Pesa payments in Kenya and beyond.
The Solution
mpesa-daraja-sdk eliminates the complexity with a clean, developer-friendly Python SDK that:
- Zero-config setup — just add your credentials and go
- Handles authentication automatically — OAuth2, tokens, and security
- Seamless environment switching — sandbox ↔ production with one parameter
- Pythonic interface — clean methods that feel natural to Python developers
- Batteries included — everything you need for M-Pesa integration
- Production-ready — end goal is to be used by startups and enterprises across Kenya
Supported Features
| Feature | Status | Description |
|---|---|---|
| STK Push | Ready | Lipa na M-Pesa Online payments |
| C2B Payments | Ready | Customer to Business transactions |
| B2C Payments | Ready | Business to Customer payouts |
| Token Management | Ready | Automatic OAuth2 handling |
| Account Balance | Coming Soon | Check account balances |
| Transaction Reversal | Coming Soon | Reverse transactions |
| 🎣 Webhook Validation | Coming Soon | Secure callback handling |
Built on top of Arlus/mpesa-py with ❤️ — modernized, cleaned up, and restructured for today's developer needs.
Quick Start
Installation (coming soon)
pip install mpesa-daraja-sdk
📖 Complete Setup Guide
1. Get Safaricom Developer Account
- Visit developer.safaricom.co.ke
- Create account and verify your email
- Create a new app to get your credentials:
- Consumer Key
- Consumer Secret
2. Obtain Test Credentials (Sandbox)
- Navigate to Test Credentials Page
- Copy the following credentials:
- Shortcode (Business number)
- Initiator Name (API operator username)
- Initiator Password (API operator password)
- Security Credential (Encrypted password)
3. Production Setup
For production deployment:
- Get Paybill/Till Number from Safaricom
- Generate Security Credential using Safaricom's public certificate
- Switch environment to
"production"in your client initialization - Update callback URLs to your production domain
Security Best Practices
- Never commit credentials to version control
- Use environment variables for sensitive data
- Implement webhook validation for callbacks
- Log transactions for audit trails
- Monitor rate limits and implement backoff strategies
- Use HTTPS for all callback URLs
🤝 Contributing
We welcome contributions from the community! Here's how you can help:
Ways to Contribute
- 🐛 Report bugs via GitHub Issues
- 💡 Suggest features for the roadmap
- 📖 Improve documentation and examples
- 🔧 Submit pull requests with fixes/features
- ⭐ Star the repo to show support
Development Setup
# Clone the repository
git clone https://github.com/rafaeljohn9/mpesa-daraja-sdk.git
cd mpesa-daraja-sdk
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest tests/
Code Standards
- Follow PEP 8 style guidelines
- Include type hints where appropriate
- Write comprehensive tests for new features
- Update documentation for any API changes
📞 Support & Community
- 📖 Documentation: [Full API docs coming soon]
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
- 📧 Email: johnmkagunda@gmail.com
🙏 Attribution & Thanks
This project began as a fork of the fantastic Arlus/mpesa-py by @Arlus.
What we've added:
- 🏗️ Modular architecture for better maintainability
- 🎯 Developer-first design with intuitive APIs
- 🧪 Comprehensive testing suite
- 📚 Better documentation and examples
- 🚀 Production-ready features and error handling
Special thanks to the original contributors and the broader Python community in Kenya.
📄 License
Licensed under the Apache 2.0 License — free for commercial and private use.
Made with ❤️ for the Kenyan developer community
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 mpesakit-2.0.0.tar.gz.
File metadata
- Download URL: mpesakit-2.0.0.tar.gz
- Upload date:
- Size: 54.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3729e8b4a88ad4c586a5339b17d2693051b05747b5771356e85f0ce3c892594c
|
|
| MD5 |
db2fbaa6ea7e0a4fc18ea5004d872bde
|
|
| BLAKE2b-256 |
8feb0d20b97e11eb05f908d301b7e1c8b80bb8fd155af76394acf2e048bf716c
|
File details
Details for the file mpesakit-2.0.0-py3-none-any.whl.
File metadata
- Download URL: mpesakit-2.0.0-py3-none-any.whl
- Upload date:
- Size: 87.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56e1d73cfc8a8b8f2300a7b9160be5166a197eede41d169085a9cd4973d37c64
|
|
| MD5 |
8aa945cbd7046841fd6610ce63e2cc00
|
|
| BLAKE2b-256 |
b387bb0468cb08929f5f8538d3ab3968bf538295bf3b37eb1b83847638546061
|