Skip to main content

MpesaKit

Open-source Python M-Pesa SDK built by the community, for the community. A reliable third-party library that simplifies M-Pesa integration with comprehensive validation, full sync/async support, and developer-friendly design.

Open Source99%+ Test Coverage500+ Test CasesSync & Async10,000+ Downloads
quick_start.py
import os
from dotenv import load_dotenv
from mpesakit import MpesaClient
from mpesakit.mpesa_express import TransactionType

# load enviroment variables
load_dotenv()

client = MpesaClient(
consumer_key=os.getenv("MPESA_CONSUMER_KEY"),
consumer_secret=os.getenv("MPESA_CONSUMER_SECRET"),
environment="sandbox",
)

# Send STK Push โ€” Coffee Purchase
response = client.stk_push(
business_short_code=int(os.getenv("MPESA_SHORTCODE")),
passkey=os.getenv("MPESA_PASSKEY"),
transaction_type=TransactionType.CUSTOMER_PAYBILL_ONLINE,
amount=250,
party_a=os.getenv("MPESA_PHONE_NUMBER"),
party_b=os.getenv("MPESA_SHORTCODE"),
phone_number=os.getenv("MPESA_PHONE_NUMBER"),
callback_url="https://example.com/callback",
account_reference="NairobiCafe-Order123",
transaction_desc="Coffee โ€” Nairobi Cafe (Order #123)",
)
99%+
Test Coverage
500+
Test Cases
100%
Type Hints
10,000+
PyPI Downloads
24/7
API Monitoring

Why Choose MpesaKit?

Community-built features designed for real-world M-Pesa integration challenges

๐Ÿ”

Advanced Data Validation

Comprehensive request and response schema validation ensures data integrity before hitting M-Pesa APIs.

  • Pydantic-powered schema validation
  • Phone number format validation
  • Amount range verification
  • Callback URL security checks
๐Ÿ›ก๏ธ

Production Security

Enterprise-grade security features to protect your transactions and sensitive data.

  • IP whitelist for callback endpoints
  • SSL certificate validation
  • Request signing & verification
  • Token refresh automation
๐ŸŽฏ

Developer Experience

Designed for developers who value clean code, type safety, and comprehensive documentation.

  • 100% type hints coverage
  • IntelliSense-friendly APIs
  • Detailed error messages
  • Rich debugging information
โšก

Full Async Support

Every service ships with an async counterpart built on an async HTTP client, so you're never blocking your event loop.

  • AsyncMpesaClient mirrors the sync client 1:1
  • Drop-in async services (AsyncStkPush, AsyncB2C, etc.)
  • Built for FastAPI, Starlette, and asyncio apps
  • asyncio.gather-friendly for batch requests
๐Ÿ””

Built-in Callback Processing

Stop hand-parsing webhook payloads โ€” validate and parse callbacks with one call, on either client.

  • client.process_stk_callback() for STK Push results
  • Dedicated helpers for B2C, Reversal, Tax and more
  • Typed Pydantic objects, not raw dicts
  • Identical on MpesaClient and AsyncMpesaClient
๐Ÿ“Š

Complete API Coverage

Full support for all M-Pesa Daraja APIs, in both sync and async, with real-time status monitoring.

  • STK Push (Lipa Na M-Pesa)
  • C2B, B2C, B2B transactions
  • Transaction status queries
  • Account balance checks
๐Ÿงช

Battle-Tested Quality

Extensive testing ensures reliability in production environments.

  • 500+ comprehensive test cases
  • 99%+ code coverage
  • Integration test suite
  • Performance benchmarks

Security & Reliability

MpesaKit implements industry best practices to ensure your M-Pesa integrations are secure and reliable in production environments.

๐Ÿ›ก๏ธ

IP Whitelisting

Restrict callback URLs to trusted IP addresses

๐Ÿ”

SSL Validation

Enforce HTTPS and validate certificates

๐Ÿ”‘

Token Management

Secure token storage and automatic refresh, for both sync and async clients

๐Ÿ“

Request Signing

Cryptographic signing of requests for authenticity

Security Layers

Network Security
TLS encryption, IP filtering, HTTPS enforcement
Authentication
OAuth2, token validation, credential management
Data Validation
Schema validation, input sanitization, type checking

API Status Monitor

Real-time status of M-Pesa Daraja APIs and MpesaKit services

Authorization

OAuth token issuance and refresh endpoints operational (sync & async)

Dynamic QR

QR generation and validation working as expected

M-Pesa Express

STK Push / Lipa Na M-Pesa flow operational, including callback processing

Customer To Business (C2B)

C2B payment endpoints accepting requests

Business To Customer (B2C)

B2C payouts processing normally

Transaction Status

Status queries returning timely responses

Account Balance

Account balance checks operational

Reversals

Reversal endpoints functional

Tax Remittance

Tax remittance integration working

Business Pay Bill

PayBill payment flows operational

Business Buy Goods

BuyGoods payment flows operational

Bill Manager

On hold per Safaricom API support

B2B Express Checkout

On hold per Safaricom API support

B2C Account Top Up

Account top-up flows functioning normally

M-Pesa Ratiba

On hold per Safaricom API support

Comprehensive Documentation

Everything you need to get started and scale your M-Pesa integration

๐Ÿ“š

Quick Start Guide

Get up and running in minutes with our step-by-step guide and code examples.

Start Building
๐Ÿ”ง

API Reference

Complete API documentation with parameters, responses, and error codes.

View APIs

Ready to Build?

Join thousands of developers who trust MpesaKit for their M-Pesa integrations. Start building secure, reliable payment solutions today โ€” sync or async.

Installation

$ pip install mpesakit