Skip to main content

A Python SDK for integrating with M-Pesa APIs

Project description

mpesakit

Effortless M-Pesa integration using Safaricom's Daraja API — built for developers, by developers.

Python 3.12+ License: Apache 2.0 Build Status

PyPI version Downloads

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

mpesakit 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 (Lipa na M-Pesa) Ready Initiate customer payment prompts (mpesa-express/stk-push.mdx)
STK Query Ready Query STK push/payment status (mpesa-express/stk-query.mdx)
C2B Payments Ready Customer-to-Business payments (c2b.mdx)
B2C Payments Ready Business-to-Customer payouts (b2c.mdx)
B2C Account Top-up Ready Account top-up flows for B2C (b2c-account-top-up.mdx)
Business Paybill Ready Paybill integrations for business collections (business-paybill.mdx)
Business BuyGoods Ready Till/BuyGoods integrations (business-buygoods.mdx)
Token Management / Auth Ready Automatic OAuth2 handling and auth utilities (auth.mdx)
Account Balance Ready Check account balances (account-balance.mdx)
Transaction Reversal Ready Reverse transactions (reversal.mdx)
Transaction Status Ready Query transaction status (transaction-status.mdx)
Dynamic QR Ready Generate and manage dynamic QR payments (dynamic-qr.mdx)
Tax Remittance Ready Tax remittance flows and docs (tax-remittance.mdx)

Built on top of Arlus/mpesa-py with ❤️ — modernized, cleaned up, and restructured for today's developer needs.


Quick Start

Installation (already here :) )

pip install mpesakit

📖 Complete Setup Guide


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/mpesakit.git
cd mpesakit

# 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/unit

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-2.0.5.tar.gz (56.4 kB view details)

Uploaded Source

Built Distribution

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

mpesakit-2.0.5-py3-none-any.whl (90.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mpesakit-2.0.5.tar.gz
  • Upload date:
  • Size: 56.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.12.12 HTTPX/0.28.1

File hashes

Hashes for mpesakit-2.0.5.tar.gz
Algorithm Hash digest
SHA256 dc163e9bb352a11d1a7ca5876278095994967921a1b28cdb6293b042d292e808
MD5 c286aa402963b22eadb45c2e1f866323
BLAKE2b-256 d47361d34b8d0eb2bc817714ffff1d32a4b8e95da1955f3df4a7e754646fa062

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mpesakit-2.0.5-py3-none-any.whl
  • Upload date:
  • Size: 90.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.12.12 HTTPX/0.28.1

File hashes

Hashes for mpesakit-2.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 99f7c77a1974a50c9c7bcca3d019ec1cf8df317f11817678324bbd82bcb7ae71
MD5 892de77c622155a13bd703155353141f
BLAKE2b-256 bbbb2e4e800f39985370288594faa10d216d5ca4a71edeeebc5d7b72005e5050

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