Appearance
API keys and scopes
Every merchant route takes one secret key as a bearer token:
text
Authorization: Bearer mkp_test_EXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEKEYA key is mkp_test_ or mkp_live_ followed by 52 characters from A-Z and 2-7. A value of any other shape is refused with 401 and code 2781 before MOOKHPay looks it up.
Keep the key on your server
Store the key in a secret store or an environment variable. Never put it in a web page, a mobile app, a public repository, a log line or a URL. Whoever holds it can do everything its scopes allow. The browser never needs it: the hosted checkout and the card element work without it.
Get a key
- Open the hub while acting for your organisation, then API Integrations, then the API Keys tab. API Integrations is part of an organisation's account; a personal account sees a note that it is for organisations, and cannot make a key in the hub today.
- In the Secret Key card, choose Create A Key (or Replace Key if one exists).
- An Owner chooses what the key can do: each scope the account may issue is listed with what it lets the key do. A Member, Accountant or Developer is not asked, and gets a
checkoutskey. - Enter your PIN. The PIN is bound to the scopes you chose, so it cannot be used to make a key with other ones.
- Copy the key. The hub shows it once and stores only a hash of it.
- Store it as a server secret, for example
MOOKHPAY_SECRET_KEY.
In an organisation, the key belongs to the organisation and pays the organisation. A Viewer cannot make a key. An Owner, Member or Accountant can make a checkouts key, and only an Owner can make a key with any other scope. A Developer can make one on staging (mkp_test_) but not in production, where a Developer's key must be made by an Owner.
The Secret Key card shows the prefix, the last four characters, the scopes, when the key was made and when it was last used.
Replace a key without downtime
Replace Key makes a new key and keeps the old one working for 24 hours, so you can deploy the new key before the old one stops. While both work, the Secret Key card shows both, and says when the old one stops.
- Choose Replace Key. An Owner chooses the scopes. Enter your PIN and copy the new key.
- Deploy the new key to every server that calls MOOKHPay.
- Check that calls succeed with the new key. The old key's Last Used stops moving once nothing sends it.
- Let the old key lapse at the end of the 24 hours. If it must stop sooner, revoke, which ends both keys, and make a new one.
An account holds at most two live keys: the current one and, for 24 hours after a replace, the one it replaced. Replacing again inside those 24 hours ends the older of the two at once, so the account is back to two: the key you just made and the one it replaced. If you lose a key, replace it; the lost one stops working 24 hours later. If a key leaked, revoke instead.
Replace or revoke
| Action | PIN | What happens |
|---|---|---|
| Replace Key | Yes | A new key works at once. The key it replaced keeps working for 24 hours, then stops. A key already counting down its 24 hours stops at once |
| Revoke | No | Every live key on the account is ended, the current one and any key still in its 24 hours. Revoking takes access away, so it does not ask for a PIN |
If either live key has a scope other than checkouts, only an Owner can replace or revoke. A Member or Accountant who tries is refused with 403 and code 2601.
A key that has stopped, by revoking or at the end of its 24 hours, can keep working for up to 60 seconds on most routes. See The 60-second cache.
Scopes
A key carries scopes, fixed when it is made. A key made without choosing scopes has checkouts only. A scope cannot be added to an existing key: replace the key with one that has the scopes you need. The old key keeps its own scopes for its last 24 hours.
| Scope | Lets the key | Who can issue it |
|---|---|---|
checkouts | Create hosted checkouts that pay the key's owner, and read them | Anyone who can make a key |
charges:write | Create, read, list and cancel direct charges | In an organisation, an Owner. The key service also lets a person's own account issue it, but the hub makes keys only for organisations today |
refunds:write | Ask MOOKHPay to refund a payment to the payer, and read refunds | In an organisation, an Owner. The key service also lets a person's own account issue it, but the hub makes keys only for organisations today |
accounts:read | Read a platform's accounts, balances, transactions, payouts and business account requests | An Owner of an organisation MOOKHPay has switched on as a platform |
accounts:write | Create seller accounts, and ask for business accounts for them | An Owner of a platform |
accounts:link | Ask an existing MOOKHPay user to link their account | An Owner of a platform |
payments:write | Take payments on behalf of a seller, with a platform fee | An Owner of a platform |
payouts:write | Pay out a seller's wallet to the seller, and read payouts | An Owner of a platform |
login_links:write | Make login links for a seller | An Owner of a platform |
transfers:write | Pay from your platform's main balance into the pockets your platform opened in its accounts' wallets (signed), and read those pay-ins. Shown in the hub as Pay Into Pockets | An Owner of a platform |
balance:read | Read the key owner's own balance and movements with GET /balance and GET /balance/transactions. Read-only. Shown in the hub as Read Your Balance | An Owner. Not a platform scope: any organisation's Owner can issue it. The key service also lets a person's own account issue it, but the hub makes keys only for organisations today |
The hub lists only the scopes your role and account may issue. Asking the key service for a scope you cannot issue refuses the whole key: 403 and code 2664. Calling a route whose scope the key lacks answers 403 and code 2941.
Which scopes a platform key needs
A key carries all the scopes it needs, and an account holds one current key. A platform that takes payments with hosted checkouts and pays into its users' pockets uses one key with:
| Scope | For |
|---|---|
checkouts | Hosted checkouts that pay your main balance |
accounts:write | Opening accounts for your users |
accounts:read | Reading accounts, pocket balances, movements, pay-ins and payouts, and GET /fees |
accounts:link | Asking existing MOOKHPay users to connect |
transfers:write | Pay-ins from your main balance into pockets |
payouts:write | Payouts from pockets, and setting a pocket's payout destination |
balance:read | Optional. Reading your own main balance to reconcile your books |
This is not the checkouts-only key the checkout quickstart recommends for a business that only takes payments. Only an Owner can make it, and it must be made with every scope at once: a scope cannot be added to a key later.
What no key can do
| No key can | Instead |
|---|---|
Read the owner's own wallet without balance:read. That scope reads the balance and movements only, and moves nothing | A key with balance:read, or a read-only key |
Send money out of the owner's own wallet to anyone it chooses. A transfers:write key pays only into the pockets your platform opened in its own accounts' wallets, and every pay-in is signed. A refunds:write key only asks for a refund; MOOKHPay reviews every one before money moves | The MOOKHPay app or hub, with the PIN |
| Send a seller's payout to a number the seller does not hold. Outside pocket mode the payout goes where the seller chose in MOOKHPay. In pocket mode a platform can set the pocket's payout destination, but only by sending back the code MOOKHPay texted to that number | The seller chooses in MOOKHPay, with their PIN |
| Act as another account by naming it in a header | Nothing. Identity headers are refused, see below |
| Reach a checkout or charge another merchant made | Nothing. Another merchant's id answers the same 404 as an unknown one |
Allowed addresses
An Owner can limit the account's API keys to the addresses your servers call from. Once a list is set, a call made with any of the account's keys from an address not on it is refused with 403 and code 3042, on every merchant route. With no list, a key works from anywhere.
- Open API Integrations, the API Keys tab, then the Allowed Addresses card.
- Enter up to 50 entries, one per line. Each is an IPv4 or IPv6 address (
203.0.113.7,2001:db8::7) or a CIDR range (203.0.113.0/24,2001:db8::/48). - Enter your PIN. The PIN is bound to the exact list you saved.
| Rule | Detail |
|---|---|
| Who | An Owner sets and clears the list, with their PIN. Everyone else who can open API Integrations sees it but cannot change it; the key service refuses anyone else with 403 and code 2601 |
| Entries | Up to 50. An entry that is not an address or a CIDR range refuses the whole list with 400 and code 2763, and nothing is saved |
| Clearing | Save an empty list, with the PIN. The keys then work from any address again |
| Which keys | Every live key on the account, including one still in its 24 hours after a replace |
| When it applies | Within 60 seconds of saving. A call in that minute can still be judged by the previous list |
| Changes | 10 saves an hour per account |
The address MOOKHPay checks is the one your request reaches it from. If your servers call out through a proxy, a NAT gateway or a cloud egress, list that public address, not the servers' private ones.
A platform can also have a list set by MOOKHPay staff, which applies on platform routes and answers 403 and code 3013. The two are separate: on a platform route a call must come from an address on both lists, where both are set.
The 60-second cache
MOOKHPay checks a key with its key service and caches the answer for 60 seconds, keyed by a hash of the key. A refused key is cached for the same minute. So a revoked key, or a replaced one at the end of its 24 hours, can go on working for up to a minute, and a key that was just made can be refused for up to a minute if it was presented, and refused, a moment before it existed. The account's allowed addresses are cached with the key for the same minute; a call refused for its address is never cached, so it is checked afresh each time.
These calls skip the cache and check the key every time, so a revoked key stops at once:
POST /chargesPOST /accountsPOST /accounts/:id/payoutsPOST /accounts/:id/transfersPOST /accounts/:id/payout-destinationandPOST /accounts/:id/payout-destination/verifyPOST /accounts/:id/login-linksPOST /accounts/:id/org-requests, and itsdocumentsandsubmitcallsPOST /checkoutswhen the body hason_behalf_oforplatform_feePOST /refunds
Headers MOOKHPay refuses
A merchant request that carries a session or identity header is refused with 400 and code 3014, before anything else is checked. The key's owner is always the account the call is for.
The refused headers are X-User-ID, X-Username, X-User-Type, X-User-Banned, X-User-Deleted, X-User-Disabled, X-Phone-Verified, X-Email-Verified, X-KYC-Complete, X-KYC-Status, X-Acting-As, X-Actor-ID, X-Actor-Username, X-Staff-ID, X-Staff-Permissions, X-Staff-Reauth, X-Session-Scope, X-Limits-Tier, X-Internal-Secret, X-Internal-Service-Key and X-Gateway-Secret. If a proxy or framework in front of your code adds any of them, remove them on calls to MOOKHPay.
The order a request is checked in
| Step | Refusal |
|---|---|
| 1. Identity headers | 400, code 3014 |
| 2. The body parses as JSON | 400, code 1701 |
| 3. The key: present, well formed, known, not revoked, and called from an allowed address if the account has a list | 401, code 2781; 403, code 3042; 503, code 2782 if the key service did not answer |
| 4. The scope the route needs | 403, code 2941 |
5. Platform routes only (/accounts…, /transactions, /account-links…, /fees): your organisation is a live platform, and the call comes from an allowed address. On POST /checkouts and POST /charges with on_behalf_of, the same checks run later, inside step 8. /balance is not a platform route: it needs only balance:read | 403, code 2942 or 3013 |
6. Fields, query parameters and required headers such as Idempotency-Key | 400, code 1701 |
| 7. Rate limits | 429, code 1804 (or the route's own code) |
| 8. The route's own checks. On a signed route (payouts, pay-ins and payout destinations) the signature comes first, then the account in the path, then (on payouts) the account's grant | 403, code 3022; 401, code 3021; 404, code 2943; 409, code 3061; 403, code 3025; then see the route's page |
Two things follow. A 400 with code 1701 for a field means your key and scope already passed. And because validation answers only "The request contains invalid data", without naming the field, check the body against the route's parameter table when you get one.
An authenticated call
Reading a checkout is the simplest authenticated call.
sh
curl https://payments-ke-staging.mookhpay.co/v1/payments/merchant/checkouts/cs_ke_3f9a1c7e5b2d4a8e6c0b91d7 \
-H "Authorization: Bearer $MOOKHPAY_SECRET_KEY"js
const base = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant";
const res = await fetch(`${base}/checkouts/cs_ke_3f9a1c7e5b2d4a8e6c0b91d7`, {
headers: { Authorization: `Bearer ${process.env.MOOKHPAY_SECRET_KEY}` },
});
const body = await res.json();
if (!body.ok) {
throw new Error(`${res.status} ${body.code}: ${body.description}`);
}
console.log(body.data.status);python
import os
import requests
BASE = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant"
res = requests.get(
f"{BASE}/checkouts/cs_ke_3f9a1c7e5b2d4a8e6c0b91d7",
headers={"Authorization": f"Bearer {os.environ['MOOKHPAY_SECRET_KEY']}"},
timeout=15,
)
body = res.json()
if not body["ok"]:
raise RuntimeError(f"{res.status_code} {body['code']}: {body['description']}")
print(body["data"]["status"])php
<?php
$base = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant";
$ch = curl_init("$base/checkouts/cs_ke_3f9a1c7e5b2d4a8e6c0b91d7");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("MOOKHPAY_SECRET_KEY")],
]);
$raw = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$body = json_decode($raw, true);
if (!$body["ok"]) {
throw new RuntimeException("$status {$body['code']}: {$body['description']}");
}
echo $body["data"]["status"], PHP_EOL;A missing or wrong key answers:
json
{
"ok": false,
"description": "Missing, unknown or revoked API key",
"data": {},
"code": 2781
}Errors
| HTTP | Code | Name | When | What to do |
|---|---|---|---|---|
400 | 3014 | IDENTITY_HEADER_REFUSED | The request carried an identity or session header | Remove the header |
401 | 2781 | API_KEY_INVALID | The key is missing, malformed, unknown or revoked, or it was replaced more than 24 hours ago | Check Authorization: Bearer …. Deploy the key that replaced it |
403 | 2941 | API_KEY_SCOPE_REQUIRED | The key lacks the scope this route needs | Replace the key with one that has the scope |
403 | 3042 | API_KEY_IP_NOT_ALLOWED | The account has allowed addresses, and the call came from another address | Call from a listed address, or have an Owner add this one |
403 | 2942 | PLATFORM_NOT_ENABLED | A platform route, and your organisation is not a live platform | Ask MOOKHPay to switch your platform on |
403 | 3013 | PLATFORM_IP_NOT_ALLOWED | A platform route, from an address not on your allow-list | Call from a listed address, or ask MOOKHPay to update the list |
503 | 2782 | AUTHN_UNAVAILABLE | The key service did not answer | Retry with backoff. Nothing was done |
The full list is on Errors.