Skip to main content

bytegigs-daraja

A typed Python client for Safaricom's Daraja (M-PESA) API, sync and async, built from the daraja-openapi spec and tested against it. Python 3.10+, one dependency (httpx).

pip install bytegigs-daraja
# B2C, B2B, reversal, status and balance encrypt your initiator password:
pip install "bytegigs-daraja[credential]"

STK Push

from daraja import Daraja

daraja = Daraja(
    os.environ["DARAJA_CONSUMER_KEY"],
    os.environ["DARAJA_CONSUMER_SECRET"],
    environment="sandbox",
    short_code="174379",
    passkey=os.environ["DARAJA_PASSKEY"],
)

push = daraja.stk_push(
    amount=150,
    phone="0712 345 678",  # any Kenyan format
    callback_url="https://your-app.example/daraja/callback",
    account_reference="INV-1001",
    description="Invoice 1001",
)

# Callbacks are not signed: confirm before you deliver.
status = daraja.wait_for_stk_result(push["CheckoutRequestID"])
if status.state == "paid":
    mark_paid()
elif status.state == "failed":
    print(status.meaning)  # "The customer cancelled the payment"

FastAPI and other asyncio apps use AsyncDaraja with the same methods:

async with AsyncDaraja(key, secret, short_code="174379", passkey=passkey) as daraja:
    push = await daraja.stk_push(...)

Read the callback at your callback_url:

from daraja import parse_stk_callback

result = parse_stk_callback(request.json())
if result and result.paid:
    print(result.receipt, result.amount)

Payouts and the other APIs

daraja = Daraja(
    key,
    secret,
    short_code="600996",
    initiator_name="testapi",
    initiator_password=os.environ["DARAJA_INITIATOR_PASSWORD"],
    certificate=open("SandboxCertificate.cer", "rb").read(),
)

daraja.b2c_payment(
    originator_conversation_id="refund-order-77",  # reuse on retries
    amount=150,
    phone="0712345678",
    remarks="Refund order 77",
    result_url="https://your-app.example/daraja/result",
    timeout_url="https://your-app.example/daraja/timeout",
)

# At result_url:
outcome = parse_async_result(request.json())
if outcome and outcome.ok:
    print(outcome.transaction_id, outcome.params["TransactionAmount"])

Also: c2b_register_urls, c2b_simulate, b2b_payment, reversal, transaction_status, account_balance (read with outcome.account_balances) and dynamic_qr (returns PNG bytes). parse_c2b_transaction reads C2B confirmations.

Errors are DarajaError (.status, .error_code, .is_pending, .is_rate_limited, .is_product_missing); bad input raises DarajaValidationError before anything is sent. STK Push and payouts are never retried automatically.

Testing without the sandbox

Run @bytegigs/daraja-mock and point base_url at it:

npx @bytegigs/daraja-mock --port 4010
Daraja(
    "mock-consumer-key",
    "mock-consumer-secret",
    base_url="http://127.0.0.1:4010",
    short_code="174379",
    passkey="bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919",
)

License

Apache 2.0. See NOTICE.

Metadata

Release files for bytegigs-daraja 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bytegigs-daraja 0.2.0
File Size Uploaded Provenance Actions
bytegigs_daraja-0.2.0.tar.gz 21.4 kB With provenance Details for bytegigs_daraja-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bytegigs-daraja 0.2.0
File Interpreter ABI Platform Actions
bytegigs_daraja-0.2.0-py3-none-any.whl Python 3 none any Details for bytegigs_daraja-0.2.0-py3-none-any.whl

Total release size: 40.7 kB

Release files / bytegigs_daraja-0.2.0.tar.gz

Download URL bytegigs_daraja-0.2.0.tar.gz
Size 21.4 kB
Tags Source
SHA-256 checksum
How to use checksums
15416e1032c1dee665ccdb26646a10569d22081305361221fd2ad0bdb483144c
BLAKE2b-256 checksum
How to use checksums
6a8aa44185b8a5bd65e09ea76581cc234f2c0e14b67b5d6fbf7a9cf7fab63f2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / bytegigs_daraja-0.2.0-py3-none-any.whl

Download URL bytegigs_daraja-0.2.0-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08ee8690a478e093a0a2d2cd9f80cbeffefd2e280c150dd787195ca4dc63276d
BLAKE2b-256 checksum
How to use checksums
7fbdf50f9ccfb927a7256b7a38937f9a49761e6739e314a24771c07237d4f826
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Security for bytegigs-daraja 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bytegigs_daraja-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page