Skip to content

API keys and scopes ​

Every merchant route takes one secret key as a bearer token:

text
Authorization: Bearer mkp_test_EXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEKEY

A 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 ​

  1. 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.
  2. In the Secret Key card, choose Create A Key (or Replace Key if one exists).
  3. 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 checkouts key.
  4. Enter your PIN. The PIN is bound to the scopes you chose, so it cannot be used to make a key with other ones.
  5. Copy the key. The hub shows it once and stores only a hash of it.
  6. 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.

  1. Choose Replace Key. An Owner chooses the scopes. Enter your PIN and copy the new key.
  2. Deploy the new key to every server that calls MOOKHPay.
  3. Check that calls succeed with the new key. The old key's Last Used stops moving once nothing sends it.
  4. 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 ​

ActionPINWhat happens
Replace KeyYesA 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
RevokeNoEvery 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.

ScopeLets the keyWho can issue it
checkoutsCreate hosted checkouts that pay the key's owner, and read themAnyone who can make a key
charges:writeCreate, read, list and cancel direct chargesIn 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:writeAsk MOOKHPay to refund a payment to the payer, and read refundsIn 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:readRead a platform's accounts, balances, transactions, payouts and business account requestsAn Owner of an organisation MOOKHPay has switched on as a platform
accounts:writeCreate seller accounts, and ask for business accounts for themAn Owner of a platform
accounts:linkAsk an existing MOOKHPay user to link their accountAn Owner of a platform
payments:writeTake payments on behalf of a seller, with a platform feeAn Owner of a platform
payouts:writePay out a seller's wallet to the seller, and read payoutsAn Owner of a platform
login_links:writeMake login links for a sellerAn Owner of a platform
transfers:writePay 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 PocketsAn Owner of a platform
balance:readRead the key owner's own balance and movements with GET /balance and GET /balance/transactions. Read-only. Shown in the hub as Read Your BalanceAn 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:

ScopeFor
checkoutsHosted checkouts that pay your main balance
accounts:writeOpening accounts for your users
accounts:readReading accounts, pocket balances, movements, pay-ins and payouts, and GET /fees
accounts:linkAsking existing MOOKHPay users to connect
transfers:writePay-ins from your main balance into pockets
payouts:writePayouts from pockets, and setting a pocket's payout destination
balance:readOptional. 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 canInstead
Read the owner's own wallet without balance:read. That scope reads the balance and movements only, and moves nothingA 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 movesThe 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 numberThe seller chooses in MOOKHPay, with their PIN
Act as another account by naming it in a headerNothing. Identity headers are refused, see below
Reach a checkout or charge another merchant madeNothing. 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.

  1. Open API Integrations, the API Keys tab, then the Allowed Addresses card.
  2. 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).
  3. Enter your PIN. The PIN is bound to the exact list you saved.
RuleDetail
WhoAn 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
EntriesUp 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
ClearingSave an empty list, with the PIN. The keys then work from any address again
Which keysEvery live key on the account, including one still in its 24 hours after a replace
When it appliesWithin 60 seconds of saving. A call in that minute can still be judged by the previous list
Changes10 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 /charges
  • POST /accounts
  • POST /accounts/:id/payouts
  • POST /accounts/:id/transfers
  • POST /accounts/:id/payout-destination and POST /accounts/:id/payout-destination/verify
  • POST /accounts/:id/login-links
  • POST /accounts/:id/org-requests, and its documents and submit calls
  • POST /checkouts when the body has on_behalf_of or platform_fee
  • POST /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 ​

StepRefusal
1. Identity headers400, code 3014
2. The body parses as JSON400, code 1701
3. The key: present, well formed, known, not revoked, and called from an allowed address if the account has a list401, code 2781; 403, code 3042; 503, code 2782 if the key service did not answer
4. The scope the route needs403, 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:read403, code 2942 or 3013
6. Fields, query parameters and required headers such as Idempotency-Key400, code 1701
7. Rate limits429, 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 grant403, 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 ​

HTTPCodeNameWhenWhat to do
4003014IDENTITY_HEADER_REFUSEDThe request carried an identity or session headerRemove the header
4012781API_KEY_INVALIDThe key is missing, malformed, unknown or revoked, or it was replaced more than 24 hours agoCheck Authorization: Bearer …. Deploy the key that replaced it
4032941API_KEY_SCOPE_REQUIREDThe key lacks the scope this route needsReplace the key with one that has the scope
4033042API_KEY_IP_NOT_ALLOWEDThe account has allowed addresses, and the call came from another addressCall from a listed address, or have an Owner add this one
4032942PLATFORM_NOT_ENABLEDA platform route, and your organisation is not a live platformAsk MOOKHPay to switch your platform on
4033013PLATFORM_IP_NOT_ALLOWEDA platform route, from an address not on your allow-listCall from a listed address, or ask MOOKHPay to update the list
5032782AUTHN_UNAVAILABLEThe key service did not answerRetry with backoff. Nothing was done

The full list is on Errors.

Staging documentation. A checkout made with a secret key on staging charges a real phone. See Environments.