Skip to main content

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.

Python 3.12+ License: Apache 2.0


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

  1. Visit developer.safaricom.co.ke
  2. Create account and verify your email
  3. Create a new app to get your credentials:
    • Consumer Key
    • Consumer Secret

2. Obtain Test Credentials (Sandbox)

  1. Navigate to Test Credentials Page
  2. 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:

  1. Get Paybill/Till Number from Safaricom
  2. Generate Security Credential using Safaricom's public certificate
  3. Switch environment to "production" in your client initialization
  4. Update callback URLs to your production domain

Security Best Practices

  1. Never commit credentials to version control
  2. Use environment variables for sensitive data
  3. Implement webhook validation for callbacks
  4. Log transactions for audit trails
  5. Monitor rate limits and implement backoff strategies
  6. 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


🙏 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

⭐ Star this repo | 🐛 Report Issue | 💡 Request Feature

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpesakit-1.0.0.tar.gz (49.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mpesakit-1.0.0-py3-none-any.whl (81.8 kB view details)

Uploaded Python 3

File details

Details for the file mpesakit-1.0.0.tar.gz.

File metadata

  • Download URL: mpesakit-1.0.0.tar.gz
  • Upload date:
  • Size: 49.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for mpesakit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e4b612aabf32e0c79bfefe8f1393cf20005cbc91a175cbd597f27de38c46ab3f
MD5 d7d208a1a07cce03380df062abb911bc
BLAKE2b-256 aa3383db026af27d1fe13e7dbd9f8b22ead7175d8ecc0fa8feee73fbbc9293c1

See more details on using hashes here.

File details

Details for the file mpesakit-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: mpesakit-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 81.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for mpesakit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bba0cd6a8362f66590d63ffefcd3280adc90144e1c42fbe05d1da371c9fe5e9b
MD5 60c5f871ac63264a861a7130c4ae84a0
BLAKE2b-256 96526298f7a86559e1a464747be3ff28b482a09c2541bfe4d18b77e4d1d29bda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page