Skip to content

safcom

M-Pesa API — the way it should be.


One line.

from safcom import Mpesa

mpesa = Mpesa("consumer_key", "consumer_secret", "passkey", "174379")

# That's it. One line. STK push. Done.
resp = mpesa.stk_push(phone="254712345678", amount=100, account_ref="INV-001")

No raw JSON. No hunting for the right endpoint. No wondering why sandbox wants different keys than production. Just clean, typed Python.


Why safcom?

The M-Pesa API is powerful but painful. Every Kenyan developer hits the same wall:

Manual authentication — Generate a token, track expiry, refresh. Every. Single. Time.

Inconsistent responses — Sandbox and production disagree on field names, error codes, and response shapes.

Copy-paste code — Every project starts by copying a Medium post from 2022 that's already outdated.

safcom fixes all of that. One package. One pip install. Sensible defaults. Clear errors. Done.


Features

Feature Description
STK Push Initiate Lipa Na M-Pesa Online payments in one call
Payment Query Check if a payment went through
B2C Send money to customers
Account Balance Query your M-Pesa balance
Auto Auth Tokens refresh automatically, you never think about it
Typed Responses Every response is a dataclass, not a raw dict
CLI Included Test payments right from the terminal
100% Private Your keys never leave your machine

Quick Install

pip install safcom

That's it. No extra dependencies to manage. No system requirements. Works on every platform Python runs on.


Next Steps


License

MIT — use it freely in personal and commercial projects.