Command-line tool for M-Pesa Daraja v3 — STK Push, B2C, balance, config management.
Project description
pesa-cli
Command-line tool for M-Pesa Daraja v3 — STK Push, B2C, balance check, config management.
Test integrations, trigger payments, and check balances from your terminal — without opening the Safaricom portal or writing a script.
Install
pip install pesa-cli
Setup
# Set credentials once — stored in ~/.pesa/config.json (mode 600)
pesa config set --key consumer-key --value YOUR_CONSUMER_KEY
pesa config set --key consumer-secret --value YOUR_CONSUMER_SECRET
pesa config set --key shortcode --value 174379
pesa config set --key passkey --value YOUR_PASSKEY
pesa config set --key environment --value sandbox # or production
# Or use environment variables (useful in CI/CD)
export DARAJA_CONSUMER_KEY=...
export DARAJA_CONSUMER_SECRET=...
# Test credentials
pesa auth
Commands
pesa auth
Test credentials and verify connectivity.
✓ Authenticated
Environment: sandbox
Token: mock_token_ab...3456
Expires in: ~60 minutes
pesa stk push PHONE AMOUNT
Initiate an STK Push payment request. The customer receives a PIN prompt on their phone.
pesa stk push 0712345678 500
pesa stk push 0712345678 500 --ref "Invoice001" --desc "Monthly contribution"
✓ STK Push sent
Phone: +254712345678
Amount: KES 500
Reference: Invoice001
Checkout ID: ws_CO_150120241430221234567890
Run: pesa stk query ws_CO_150120241430221234567890
pesa stk query CHECKOUT_ID
Poll the status of an STK Push.
pesa stk query ws_CO_150120241430221234567890
✓ The service request is processed successfully.
Result Code: 0
pesa b2c PHONE AMOUNT
Send money from your shortcode to a phone number.
pesa b2c 0712345678 1000 --remarks "Chama disbursement"
pesa balance
Request account balance (result posted to your ResultURL asynchronously).
pesa balance
pesa config show
Display current configuration with secrets masked.
┌──────────────────────────────────────────────────┐
│ pesa configuration │
├──────────────────┬─────────────┬─────────────────┤
│ consumer_key │ config file │ ************3456 │
│ consumer_secret │ config file │ ************abcd │
│ shortcode │ config file │ 174379 │
│ environment │ config file │ sandbox │
└──────────────────┴─────────────┴─────────────────┘
Credentials
Credentials are resolved in this order: environment variable → config file.
| Credential | Config key | Environment variable |
|---|---|---|
| Consumer Key | consumer_key |
DARAJA_CONSUMER_KEY |
| Consumer Secret | consumer_secret |
DARAJA_CONSUMER_SECRET |
| Shortcode | shortcode |
DARAJA_SHORTCODE |
| Passkey | passkey |
DARAJA_PASSKEY |
| Initiator Name | initiator_name |
DARAJA_INITIATOR_NAME |
| Security Credential | security_credential |
DARAJA_SECURITY_CREDENTIAL |
| Environment | environment |
DARAJA_ENVIRONMENT |
Config file is stored at ~/.pesa/config.json with mode 600 (owner read/write only).
Override location with PESA_CONFIG=/path/to/config.json.
Use with daraja-mock
# Start daraja-mock
python -m daraja_mock --port 8765
# Point pesa-cli at the mock
export PESA_BASE_URL=http://localhost:8765
pesa auth
pesa stk push 0712345678 100 --ref "Test"
Design decisions
Credentials resolved env → file. CI/CD environments should use env vars. Developer machines use the config file. The CLI never asks for credentials interactively — that would break scripting.
Secrets masked at 600. The config file is created with chmod 600 so
other users on shared systems cannot read Daraja credentials.
AccountReference truncated silently. Daraja rejects AccountReference > 12 chars
with a cryptic error. The CLI truncates to 12 and shows what was sent.
Rich output, not JSON by default. For human use. Add --output json flag
if scripting against pesa-cli output is needed.
Part of the nairobi-stack East Africa engineering ecosystem. Maintained by Gabriel Mahia. Kenya × USA.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pesa_cli-1.0.0.tar.gz.
File metadata
- Download URL: pesa_cli-1.0.0.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7b3626265cfb88d036032cf69e695e4126c83ba591df482d0fe9c846fd7642b
|
|
| MD5 |
80c215a2265d5bc84dd39790719b73f5
|
|
| BLAKE2b-256 |
32f395c0cb8c959f2f8d5721dba56c778cddd39721f841d8c8ebede32acda90a
|
Provenance
The following attestation bundles were made for pesa_cli-1.0.0.tar.gz:
Publisher:
publish.yml on gabrielmahia/pesa-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pesa_cli-1.0.0.tar.gz -
Subject digest:
e7b3626265cfb88d036032cf69e695e4126c83ba591df482d0fe9c846fd7642b - Sigstore transparency entry: 1123029204
- Sigstore integration time:
-
Permalink:
gabrielmahia/pesa-cli@f080bc54498755c12c7aadd395d7854630a9ae97 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/gabrielmahia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f080bc54498755c12c7aadd395d7854630a9ae97 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pesa_cli-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pesa_cli-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3814b0648bdcf2a279d0a87d49dfaa564b8a046563b39476e2535a39c600053e
|
|
| MD5 |
53504c206ace96e3cee2efb90fb07d1c
|
|
| BLAKE2b-256 |
734b4b4cceb2a77b2971493423739e8f52e72db06eaab5be3e37e309cb752660
|
Provenance
The following attestation bundles were made for pesa_cli-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on gabrielmahia/pesa-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pesa_cli-1.0.0-py3-none-any.whl -
Subject digest:
3814b0648bdcf2a279d0a87d49dfaa564b8a046563b39476e2535a39c600053e - Sigstore transparency entry: 1123029212
- Sigstore integration time:
-
Permalink:
gabrielmahia/pesa-cli@f080bc54498755c12c7aadd395d7854630a9ae97 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/gabrielmahia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f080bc54498755c12c7aadd395d7854630a9ae97 -
Trigger Event:
push
-
Statement type: