API Document

Callbacks

An agent in SEAMLESS mode holds its members' money. Every time a game moves a member's money, best-superapi sends a callback to the agent's wallet, and the agent applies it. This page is the contract of those callbacks, version 1. Every field and every example is also in the OpenAPI file under webhooks; the examples named below live there.

Agents in TRANSFER mode receive no callbacks: their members' money is held by best-superapi.

The 10 callbacks

Each callback is POST {callback_url}/api/{path}, where callback_url is the address on the agent's API credential.

Path Money Used when
balance none We need the member's balance
bet debit amount A member places a bet
settle credit amount, or credit or debit amount by direction when single_state is true A bet's result is paid
refund credit the stake your wallet holds for the bet (we send it as amount when we hold the bet) The provider cancels or rejects a bet and gives the stake back
reward credit amount A jackpot or bonus outside any bet
rollback debit what the bet's settle or refund credited The provider undoes a bet's result
void credit or debit amount by direction The provider cancels a settled bet, or a whole round with its rewards
tip debit amount A member tips the dealer
cancel-tip credit amount A tip is given back
adjust credit or debit amount by direction An open bet's stake changes, or a game played without bets (such as fishing games) changes the balance

What each path asks of the wallet, field by field, is in the description of its webhook in the OpenAPI file.

The callback URL

  • https:// only, with no query, no fragment and no user name or password in it.
  • Every address it resolves to must be public.
  • We do not follow redirects: a 3xx answer counts as a failed callback, even though the request reached you.

Headers and signature

Header Value
Content-Type application/json
X-Api-Key The API key of your API credential
X-Timestamp Unix time in seconds when we signed the request
X-Signature Lowercase hex of HMAC-SHA256, keyed with the secret of your API credential, over {X-Timestamp}.{raw body}
X-Contract-Version 1

To check a request:

  1. Read the raw body exactly as received. Never parse and serialize it again before checking.
  2. Compute HMAC-SHA256 of the X-Timestamp value, a dot (.), then the raw body, with your secret as the key. Compare it with X-Signature in constant time.
  3. Answer 30002 when the signature does not match, or when X-Timestamp is more than 300 seconds away from your clock in either direction.
  4. Answer 40003 when X-Api-Key is not your key.

It is the same signature as on the requests you send us. We sign every request when we send it, a resend included, so a resend always carries a fresh X-Timestamp. There is no nonce: the resend rules keep a repeated request from moving money twice.

The request

Every callback carries the same envelope. balance has no txns; every other path has at least one.

Field Meaning
request_id A new id for every request we send, a resend included. For matching logs only, never for spotting a resend.
external_player_id Your id of the member, as you sent it to launch. Find the balance in your wallet by it together with currency.
player_username Our username of the member, for support. Never find the member by it.
currency The member's currency. Every amount in the request is in it.
provider_code The provider's code, the code of the provider in the catalog
timestamp Unix time in milliseconds when we built the request
txns The movements to apply, all or nothing

external_player_id and provider_code are always present, and null when we do not know them. A null external_player_id is the same case as an unknown member: answer 10001, and no money moves. A balance for an external_player_id in a currency you do not hold for it is an unknown member too: answer 10001. player_username is defined in how you name a member.

Amounts are JSON numbers in the major unit of currency, with at most as many decimals as the currency has (THB 2, JPY 0, KWD 3). An amount is never negative: the path or direction says which way it moves. An amount of 0 is valid (a free spin, a lost bet's payout) and must be answered like any other.

The fields of a txn are in the Txn schema. is_feature is on every txn (a free or feature spin). The ones that need care:

  • game_code is our code of the game and provider_game_code the provider's own; both are on every txn, null when unknown. game_code describes the game and is never a key: it can change between sends without making different content. The full rule is in bet records.
  • status is the bet's state as the provider reports it: OPEN and WAITING on bet, SETTLED on settle, REFUND or REJECT on refund, ROLLBACK on rollback, VOID on void and TIPS on tip. It is for your records. How money moves is decided by the path, direction, skip_balance_update and transaction_type, never by status. reward, cancel-tip and adjust may carry no status; that changes nothing. A value you do not know must be accepted.
  • transaction_type, on settle, refund, rollback and void, is BY_TRANSACTION (only the bet of ref_txn_id, or of txn_id when it is null) or BY_ROUND (every bet of round_id). On BY_ROUND, amount is the round's total: move it once, record it against the round, and mark every bet of the round. On a void, the round's reward credits are part of the round too (void and rewards). Absent means BY_TRANSACTION.
  • skip_balance_update: true moves no money: answer 0 with the current balance. On a bet you already applied it confirms that bet: the provider's confirmation of a sports bet comes this way (examples sportsWaiting and sportsConfirmed of bet). On a bet you never applied it is a bet the provider records only, its money moving through other requests (for example a stake and a payout sent as adjust): record the bet with no stake taken, so its settle with skip_balance_update: true finds it, and answer 0 (example recordOnly of bet). Never answer 10002 to it. Such a bet holds only the money that moves through it: a refund of it gives no stake back, and a rollback takes back only what a settle of it paid.
  • single_state: true on settle means the stake and the result come in this one call with no bet before it. Apply amount, which is payout_amount minus bet_amount, in direction, and check that the balance covers bet_amount (example singleState).
  • is_feature: true on settle is a free or feature spin. A free spin with transaction_type: BY_ROUND and a txn_id of its own adds its amount to the round, even when the round is already settled: apply it, and never answer 20001 because the round is settled (example freeSpin). A free spin with transaction_type: BY_TRANSACTION and a txn_id of its own adds its amount to the bet of ref_txn_id like a part does (a bet's life, example freeSpinByTransaction).
  • ref_txn_id on settle is the bet the settle pays, sent whenever we hold that bet: the bet's own txn_id, or the bet a settle under a txn_id of its own pays (a free spin or a part by transaction, or a provider whose settle ids are not its bet ids). Find the bet by it. It is null when we hold no bet for the settle (a single_state settle, or a BY_ROUND settle under a txn_id of its own). A BY_ROUND settle acts on every bet of round_id even when ref_txn_id names one.
  • is_end_round on settle marks a settle that comes in parts: false for a part that more parts follow, true for the last one. Absent means the settle comes in one piece (example firstPart). Each part has a txn_id of its own. A free spin or a single_state settle may carry is_end_round too; it does not close the bet to further parts (a bet's life).
  • rollback_count is how many times the bet was rolled back before this request. The first rollback of a bet carries 0, and the new result that follows it carries 1 (examples rollback and resettle of rollback).
  • callback_id is the provider's id of the callback the txn came in, for your records. It is not part of the resend key: a provider may send a txn again under a new callback_id.
  • ref_txn_id is the bet or tip the request acts on, and it may equal txn_id. Find the bet by ref_txn_id, or by txn_id when ref_txn_id is null; never refuse a request because it is null.
  • On adjust, every change has its own txn_id. A txn with bet_amount changes the stake of the bet in ref_txn_id, and bet_amount is its new total stake (example raiseStake). A txn without bet_amount changes the balance of a game played without bets: apply it, there is no bet to find (example fishingWin). A change of stake with skip_balance_update: true moves no money and leaves the stake your wallet holds for the bet as it was, whatever bet_amount it names: a later refund gives back that stake.

The reply

Answer every outcome with HTTP 200 and a JSON body. Any other HTTP status counts as a failed callback, whatever the body says.

Field When
code Always. An integer from the table below.
balance balance path. Required when code is 0.
balance_before, balance_after Every other path, on every outcome: equal when no money moved, and 0 on 10001, 30002 and a 40003 for a wrong API key
currency Optional echo. A currency other than the request's fails the callback.
request_id Optional echo
message Optional reason for a refusal, most useful with 40003

A success that lacks its balance, or a code that is not an integer, counts as a failed callback. Examples of every reply are under TxnReply in the OpenAPI file.

Codes

The codes are ours and stay the same whichever provider the game comes from. Apart from 0 they have five digits, unlike the four-digit codes of the endpoints you call.

Code Meaning Paths
0 Success, or a resend of something already applied All
10001 No member with this external_player_id, or it is null All
10002 The balance does not cover new money in play bet, tip, settle with single_state: true, adjust with direction: DEBIT
20001 The bet or tip the request acts on is unknown, or not in a state that path applies to (a bet's life) settle, refund, rollback, void, cancel-tip, adjust of a stake
20002 A known key with a different amount or direction Every path with txns
30002 The signature does not match, or X-Timestamp is too far from your clock All
40003 Not your API key (all paths), or new money in play refused by your own rules (a suspended member, a currency you do not hold, an amount you do not accept), with an optional message All for the API key; bet, tip, settle with single_state: true and adjust with direction: DEBIT for your rules
50001 Your wallet could not process the request All

For a member your wallet can find, results and reversals are never refused for lack of balance or by your own rules: settle (unless single_state is true), refund, reward, rollback, void, cancel-tip and adjust with direction: CREDIT must be applied, even for a suspended member. A rollback, or a void with direction: DEBIT, must be applied even when the member's balance goes below zero.

Time to answer

Answer within 3 seconds. We stop waiting after 4 seconds and count the callback as failed. A failed callback is not retried by us: the provider sends it again, so a request can reach you more than once.

A bet's life

A bet is open after its bet, and a settle with single_state: true makes a bet that is already settled. A bet's rollback_count starts at 0, and a rollback that carries n makes it n + 1. First check the resend key: a resend is answered by the resend rules. Otherwise each path applies to a bet in these states only, and anything else is answered 20001 and moves no money:

Path Applies to a bet that is Then the bet is
settle open, or being settled in parts (every part so far had is_end_round: false). A bet settled in one piece, by a free spin or by a single_state settle still takes a part (a settle that carries is_end_round, or a free spin with transaction_type: BY_TRANSACTION) with a txn_id of its own. A bet that a part with is_end_round: true, not a free spin, settled takes none. Any other settle of a new txn_id on a settled bet is answered 20001, except a free spin (is_feature: true) with transaction_type: BY_ROUND and a txn_id of its own, which applies in both cases. settled, or still being settled when is_end_round is false
refund open refunded
rollback settled, being settled in parts, or refunded open again
void open or settled; with transaction_type: BY_ROUND, a round that holds such a bet or a reward not voided yet voided, for good
cancel-tip a tip you applied and have not given back given back
adjust with bet_amount open still open, with the new stake (the stake it had when skip_balance_update is true)

A refund gives back the stake your wallet holds for the bet: what its bet debited, or the new total after an adjust that changed the stake (one with skip_balance_update: true changes nothing). When we hold the bet we send that stake as amount (with transaction_type: BY_ROUND, the sum of the stakes of the round's open bets), so it is the figure to credit. When we hold no such bet, amount is the provider's figure: credit the stake your wallet holds, and answer 20001 when it holds no such bet. bet_amount is the provider's figure and may differ, for example when the provider asks for more than was staked: never credit a different amount than the stake your wallet holds. A resend of a refund you applied carries the same amount as the first, and another amount under the same key is a conflict (resends).

A rollback takes back everything your wallet credited for the bet since it was last open: the payout of every part of a settle in parts and of every free spin received so far, the payout_amount of a single-state settle (never its net), or the refunded stake. Use what your wallet recorded; amount and bet_amount on the rollback are the provider's figures. A settle or refund with skip_balance_update: true credited nothing, so there is nothing of it to take back. When the bet was settled or refunded BY_ROUND, a rollback of any bet of that round takes back the round's amount once and opens every bet of the round.

A request with an older rollback_count than the bet's is answered 20001.

Callbacks of a bet can arrive out of order. Answer by the state your wallet holds, as above, and never move money for a request the table does not allow.

Void and rewards

A reward does not belong to a bet, so a rollback never takes it back. A void with transaction_type: BY_ROUND does: the provider has cancelled the whole round, and the rewards paid on it go with it. Apply it when your wallet holds, for round_id, a bet that is open or settled or a reward that is not voided yet, even when the round has no bet and only rewards (never answer 20001 for that case). A round whose bets are all refunded or voided and that holds no reward to void is answered 20001. Move the one amount and direction we send, and nothing else, and mark every bet and reward of the round as voided. A void with transaction_type: BY_TRANSACTION acts on the bet of txn_id only and leaves the rewards alone.

When a settle or a reward that a void covers came with skip_balance_update: true and credited nothing, the void's payout_amount is the provider's payout but no more than the settled bets and rewards it covers were credited: a reward really paid in the same round is still taken back (example skipSettled: the stake back, nothing taken). In every other case payout_amount is the provider's. This does not apply when what the void covers holds a bet the provider records only (a bet with skip_balance_update: true you never applied, or a settle with single_state: true and skip_balance_update: true and no bet before it): there the provider's figures stand. bet_amount is the provider's stake, and amount and direction are the net of the two. A void with skip_balance_update: true carries the same figures and moves nothing. A void we send again carries the amount and direction of its first send, with the provider's bet_amount and the payout_amount that make that net (when that net gives back more than the stake, bet_amount is the net and payout_amount 0). Move the amount and direction we send, as on every void.

All or nothing

Apply every txn of a request in one transaction. When one txn fails, move no money for any of them and answer that failure with balance_before equal to balance_after.

Resends

The provider sends a callback again when it did not get our answer in time, and we send it to you again. A resend carries the same txns as the first time, with the same txn_id, rollback_count and is_end_round, and a new request_id, timestamp and signature. Its callback_id may differ.

Key each txn, not each request, on path, txn_id and rollback_count.

A txn whose key you have already applied is a resend, and it moves no money. When every txn of a request is a resend, answer:

  • 0 with the current balance (balance_before equal to balance_after), moving no money, when its amount and direction are the ones you applied;
  • 20002, moving no money, when the amount or direction of one of them differs.

A request that mixes resent and new txns applies the new ones and answers 0. One txn with 20002 fails the whole request (all or nothing).

Other fields (status, flags, play_info, game_code, ref_txn_id) may differ under the same key and do not make it a conflict. A settle whose txn_id is not its bet's (a free spin or a part under a txn_id of its own) that we sent before comes again with the rollback_count of its first send, even after its bet or its round was rolled back: it is a resend of that key, not a new result. A txn with skip_balance_update: true moves no money.

skip_balance_update may differ between the sends of one key, and the key is still a resend: it moves no money, whichever send carried the flag. A resend with skip_balance_update: true is never a conflict: answer it 0, whatever its amount or direction. A resend without it, of a key first sent with skip_balance_update: true, moves nothing either and is answered by the rules above. A refund or a void carries the same amount and direction on every send of its key, the one with skip_balance_update: true included.

Why each part of the key matters:

  • path, because a bet's bet, settle and rollback share one txn_id.
  • rollback_count, because after a rollback the new result comes with the same txn_id. A bet can be rolled back more than once, so never accept "one repeat" as a rule.
  • Never callback_id: each part of a settle in parts has a txn_id of its own, and a provider may send a txn again under a new callback_id.

Still being confirmed with providers

A few edge cases depend on how a provider behaves, and we are still confirming them with providers. Build your wallet on what this page says today for each of them:

Case What to implement now
A provider resends a callback: does it reuse its own id? callback_id may differ on a resend. Key on path, txn_id and rollback_count, never on callback_id (resends).
A round settled with transaction_type: BY_ROUND is rolled back: does the rollback cover the whole round, or come per bet? A rollback of any bet of the round takes back the round's amount once, as your wallet recorded it, and opens every bet of the round (a bet's life).
A void with transaction_type: BY_ROUND of a round that has rewards: does its amount include the rewards? Move the one amount and direction we send, and nothing else, and mark every bet and reward of the round as voided (void and rewards).
A reward arrives on a round already voided BY_ROUND: does a second void follow? Apply the reward, as every reward is applied (codes). A later void with transaction_type: BY_ROUND of that round applies while your wallet holds a reward of it not voided yet: move the amount we send (void and rewards).

If a provider's answer differs, we change version 1 by additions only, so X-Contract-Version stays 1, and we tell agents what changed (versions).

Versions

Every request carries X-Contract-Version: 1. Version 1 only grows by additions, such as new fields or new values of status. Ignore any field you do not know, and never refuse a request because of one. A change that would break an agent built on version 1 comes as a new major version, announced ahead.