API keys & scopes

Your servers authenticate with API keys. Each key has scopes that decide which endpoints it may call, and can be revoked on its own.

Sending the key

bash
curl https://api.mypayments.co.ke/api/v1/accounts \
  -H "Authorization: Bearer $MYPAYMENTS_KEY"

Keys start with mpk_. Keep them on your server, in an environment variable or a secret store. Never put one in a web page, a mobile app or a git repository: anyone holding it can prompt your customers for money.

Minting and revoking

Create keys in Dashboard → API keys. The full key is shown once; afterwards you see only its prefix and when it was last used. Revoking a key stops it at once. Use one key per system (your website, your billing server) so you can revoke one without touching the others.

You can only give a key scopes your own role has: a member who cannot initiate payments cannot mint a key that does.

Scopes

ScopeAllowsEndpoints
accounts:readList your accounts and read oneGET /accounts, GET /accounts/{id}
stk:initiateSend STK Push promptsPOST /stk
transactions:readLook up and list paymentsGET /transactions, GET /transactions/{reference}
numbers:manageIssue, list and close account numbersPOST /accounts/{id}/numbers, GET …/numbers, GET and DELETE /numbers/{id}
payouts:initiateReserved for the payout API (no endpoint yet)—

Give each key only what that system needs. A server that only checks payment status needs only transactions:read.

When a key is refused

  • 401: no key, an unknown or revoked key, or a malformed header.
  • 403: the key is valid but lacks the scope, for example this API key lacks the "accounts:read" scope.
  • 404: the id is not one of yours. Another business's account or payment is indistinguishable from one that doesn't exist.

Dashboard-only operations

Adding shortcodes and their Daraja credentials, registering C2B URLs, managing webhook endpoints and resending a webhook are done in the dashboard, not with API keys: they change where money and payment data go, so they need a signed-in person.

See responses & errors for the error envelope and rate limits.