Working code for the Diamond Exch API: live exchange odds with back and lay ladders, bookmaker rates, fancy markets and results for cricket, soccer, tennis and more, over REST and Server-Sent Events. Examples in Node.js, Python, PHP and curl, each one a single file you can read in a few minutes.
API reference: https://docs.diamondexchapi.live · Website: https://diamondexchapi.live · Get a sandbox key: https://diamondexchapi.live/telegram
Diamond Exch API is an independent API provider, not run by or affiliated with Diamond Exchange or Betfair.
- What the feed is
- Quickstart
- The examples
- Reading prices: decimal, rate, line and range
- Exchange back/lay ladder and bookmaker rates
- Market status and suspended markets
- Keeping a live odds stream alive
- Polling instead of streaming
- Results and settlement
- Sandbox limits
- Errors
- Running each language
- OpenAPI spec
- Postman collection
- Get an API key
A live exchange odds API for betting sites built on the exchange model. Every market comes as structured JSON: match odds with back and lay ladders up to three levels deep and the amount available at each price (the Betfair-style format exchange sites use), a bookmaker market quoted as rates, and cricket fancy markets. It covers cricket, soccer, tennis and more, from pre-match to full time, and each market's result is published when it settles, so you can settle bets automatically.
- REST for snapshots: list events, fetch one event with every market and price, batch odds for up to 50 events, look up results.
- A live stream (SSE): one long HTTP response that sends each event in full, then only what changes, usually within a fraction of a second. No WebSocket needed; any HTTP client can read it.
- A changes feed for servers that cannot hold a stream open.
- Results pushed on the stream and kept for at least 48 hours.
- Stable ids that never change:
ev_…events,mk_…markets,rn_…runners,cp_…competitions.
The API is for server-to-server use. Keys work only from the server IPs you register, so do not call it from a browser or an app.
export DIAMONDEXCHAPI_KEY="fg_test_…" # your sandbox key, from https://diamondexchapi.live/telegram
cd diamondexchapi-api-examples
node node/list-inplay.mjs # what is in play now
node node/event-markets.mjs # one event, every market, prices decoded
node node/exchange-ladder.mjs # back/lay ladder + bookmaker rate to decimal
node node/stream.mjs # live stream, Ctrl-C to stopNode 18 or newer, nothing to install. The same examples exist in Python, PHP and curl: see Running each language.
Every example reads the key from DIAMONDEXCHAPI_KEY and the base URL from DIAMONDEXCHAPI_BASE_URL, which defaults to the sandbox, https://api-sandbox.diamondexchapi.live. For live, set DIAMONDEXCHAPI_BASE_URL=https://api.diamondexchapi.live and use your fg_live_… key.
| What it does | Node.js | Python | PHP | curl + jq |
|---|---|---|---|---|
| List in-play events | list-inplay.mjs | list_inplay.py | list-inplay.php | list-inplay.sh |
| One event: every market, prices decoded per format | event-markets.mjs | event_markets.py | event-markets.php | event-markets.sh |
| Live SSE stream with resume and reconnect | stream.mjs | stream.py | stream.php | stream.sh |
Poll /v1/changes |
poll-changes.mjs | poll_changes.py | poll-changes.php | poll-changes.sh |
| Results, in order, with a saved cursor | results.mjs | results.py | results.php | results.sh |
| Back/lay ladder + bookmaker rate to decimal | exchange-ladder.mjs | exchange_ladder.py | exchange-ladder.php | exchange-ladder.sh |
The shared code is small and meant to be copied: node/lib, python/odds_api.py, php/lib.php, curl/common.sh. It handles the key header, gzip, 429 rate_limited with Retry-After, price decoding and the stream.
Every market has a format. Read it before you use price or size: a bookmaker rate of 13 is decimal 1.13, not 13.
Each runner has back and lay ladders, best price first, as {"price", "size"}:
format |
Used for | price |
size |
How to read it |
|---|---|---|---|---|
decimal |
Match odds, Yes/No, Odd/Even | Decimal odds | Amount available | A stake of 100 at 2.88 returns 288. |
rate |
Bookmaker | Profit per 100 staked | Amount available | Decimal = 1 + rate / 100. Rate 13 is 1.13; rate 625 is 7.25. |
line |
Fancy: runs, wickets, totals | The line | The rate | Back is Yes: wins if the result reaches the line. Lay is No: wins if it stays below. |
range |
Khado | First winning score | The rate | Yes only: wins if the result is one of the width scores from price upward. |
{ "id": "mk_3hb7tq2wc9xzv5ne", "name": "Bookmaker", "type": "bookmaker", "format": "rate", "status": "OPEN",
"runners": [ { "id": "rn_q3m8x2wte6kd7bpa", "name": "Pakistan", "status": "OPEN",
"back": [{ "price": 48, "size": 100000 }], "lay": [{ "price": 52, "size": 100000 }] } ] }Back 48 here means decimal 1.48: 100 staked on Pakistan wins 48.
Match odds are decimal: each runner has a back and a lay ladder, best price first, with the amount available at each price. exchange-ladder prints them as an exchange screen does, back prices reading towards the middle:
MATCH ODDS [OPEN]
Back | Lay
Pakistan 1.47 (310) 1.48 (980) 1.49 (1525) | 1.52 (640) 1.53 (200)
Sri Lanka 2.86 (310) 2.87 (980) 2.88 (258) | 3 (25)
The bookmaker market is quoted in rates, profit per 100 staked. Convert with decimal = 1 + rate / 100:
BOOKMAKER [OPEN]
Runner back rate decimal | lay rate decimal
England Lions 13 1.13 | 16 1.16
West Indies A 625 7.25 | 769 8.69
const rateToDecimal = (rate) => 1 + rate / 100 // 13 -> 1.13, 625 -> 7.25status |
Meaning | What to do |
|---|---|---|
OPEN |
Prices are live. | Take bets. |
SUSPENDED |
Temporarily not tradeable: a goal, a wicket, a review, or a feed check. | Prices may still be shown; take no bets. |
BALL_RUNNING |
Cricket: the ball is in play. | Take no bets until it is dead. |
CLOSED |
Finished. The market leaves the event shortly after; its result follows. | Stop showing it; settle on the result. |
A market can also carry "stale": true when its prices have stopped arriving; it is then shown as SUSPENDED until they flow again. Runners have their own status too. Every example uses the same rule:
const tradeable = (market, runner) =>
market.status === 'OPEN' && !market.stale && (!runner || runner.status === 'OPEN')GET /v1/stream?events=ev_a,ev_b is a Server-Sent Events stream. On connect you get a snapshot of each event, then ready, then only changes:
| Message | Data | What to do |
|---|---|---|
snapshot |
A whole event, as from /v1/events/{id} |
Store it (replace any copy you hold). |
ready |
The opening snapshots are done. | |
market-update |
{"event", "market"}, the whole market |
Replace your copy of that market. Do not merge. |
market-remove |
{"event", "market": "mk_…"} |
Drop the market; its result follows. |
event-update |
The event's name, start or state | Update the event (for example it went in play). |
event-remove |
{"id"} |
Drop the event. |
result |
A result entry | Settle. See Results. |
reconnect |
Close and reconnect now, with your last id. | |
error |
{"code", "message"} |
The stream closes, for example a revoked key. Do not retry blindly. |
To keep one stream running for days, each example does five things:
- Resume with
Last-Event-ID. Every message has anid. Keep the last one and send it back in theLast-Event-IDheader when you reconnect: you receive exactly what you missed, results included. If it is too old to resume, you get fresh snapshots instead, so your copy is always whole. Treat the id as an opaque string. - Reconnect on
reconnect. The server rotates every stream after about 14 minutes (840 s), and before a restart, by sendingreconnect. Resuming loses nothing. - Expect the 15-minute cut. The edge in front of the API closes any single HTTP response at 900 s. The
reconnectmessage normally comes first, but if the connection simply drops, resume withLast-Event-IDin the same way. - Watch the heartbeat. A
: heartbeatcomment line arrives every 15 s. If nothing at all arrives for 45 s, the connection is dead: reconnect. - Back off on failure. Honour the stream's
retry:hint, add jitter, and stop on400,401,403and on anerrormessage, which retrying cannot fix. A429 too_many_streamsright after a reconnect usually means the old connection has not closed yet: wait a few seconds.
If your server falls behind, you are not sent every tick you missed, only the latest state of each market. A wire sample:
event: snapshot
id: 9f2c41d0.18342.17493
data: {"id":"ev_k9wtucdybikxygkh","name":"Pakistan v Sri Lanka","sport":"cricket", … ,"markets":[…]}
event: ready
id: 9f2c41d0.18342.17493
data: {"events":1}
event: market-update
id: 9f2c41d0.18347.17493
data: {"event":"ev_k9wtucdybikxygkh","market":{"id":"mk_vf86825bva46g3qq","name":"Match Odds", … }}
: heartbeat
The Node, Python and PHP examples parse SSE themselves in one short, commented function each, so you can see every step and need no SSE library. Sandbox keys must list event ids (up to 5); events=all needs a live key, so the examples pick the first in-play events for you.
If you cannot hold a stream open: take a snapshot with GET /v1/odds?events=… (up to 50 events), keep its next token, and call GET /v1/changes?since=<token> every second or two. Each call returns the latest state of every market that changed and a new next. A 410 resync means the token is too old: take a fresh snapshot and carry on from its token. See poll-changes.
A stream is cheaper and faster: one stream replaces hundreds of requests a minute. Use REST with Accept-Encoding: gzip and If-None-Match (event snapshots carry an ETag) to keep polling light.
Every result is recorded in order and kept for at least 48 hours. It reaches you three ways: on your stream as result, listed by GET /v1/results?since=, and looked up by market (/v1/markets/{id}/result) or event (/v1/events/{id}/results).
action |
Meaning | What to do |
|---|---|---|
SETTLED |
The market's first result | Settle bets |
CORRECTED |
The result changed | Re-settle to the new result |
VOIDED |
The market was cancelled | Refund stakes |
UNSETTLED |
The result was withdrawn; a new one will follow | Hold settlement |
An entry carries a winner ({"id", "name"}), a number in result (line and range markets), or neither (void). A market can have several entries over time: the one with the highest seq is current. Page with since=<next> and call again at once while more is true; results saves its cursor to .results-cursor, so each run continues from the last.
The sandbox, https://api-sandbox.diamondexchapi.live, carries real, current markets: prices move, markets suspend, results arrive. It is for building, not for running a site, so every fg_test_… key has these limits:
| Sandbox limit | Value |
|---|---|
| Odds delay | Prices are 60 seconds old. Results are not delayed. |
| Open streams | 1 at a time |
| Events per stream | 5, listed by id (events=ev_a,ev_b). events=all needs a live key. |
| Requests | 2 per second |
GET /v1/me shows the limits your key has. The helpers in this repo space requests 0.55 s apart and retry 429 rate_limited after Retry-After, so the examples stay inside these limits. A key works only in its own environment: fg_test_… on the sandbox, fg_live_… on https://api.diamondexchapi.live.
Errors are JSON, {"code", "message"}, and every response carries X-Request-Id: quote it when you contact us.
| Status | code |
Meaning |
|---|---|---|
| 400 | bad_request |
A parameter is wrong; the message says which. |
| 401 | unauthorized |
No key, or a key we do not know. |
| 403 | forbidden |
The IP is not on your allowlist, the key is for the other environment, or the account is suspended. |
| 404 | not_found |
No such id, or not one of yours. |
| 410 | resync |
Your token is too old: take a fresh snapshot. |
| 429 | rate_limited / too_many_streams / quota_exceeded |
Too many requests, all stream slots in use, or an allowance used up. |
| 503 | starting / maintenance |
Retry in a few seconds / planned maintenance. |
Set your key once (or copy .env.example to .env and load it with set -a; . ./.env; set +a):
export DIAMONDEXCHAPI_KEY="fg_test_…"
# optional: export DIAMONDEXCHAPI_BASE_URL=https://api.diamondexchapi.live (live key)
# optional: export SPORT=soccer (default: all sports)Node.js 18+, no dependencies:
node node/list-inplay.mjs [sport]
node node/event-markets.mjs [ev_…]
node node/stream.mjs [ev_a,ev_b | all]
node node/poll-changes.mjs [ev_a,ev_b]
node node/results.mjs [mk_… | ev_…]
node node/exchange-ladder.mjs [ev_…]Python 3.8+, needs only requests:
pip install -r python/requirements.txt
python python/list_inplay.py [sport]
python python/stream.py [ev_a,ev_b | all]
python python/exchange_ladder.py [ev_…]PHP 8.0+ with the curl extension:
php php/list-inplay.php [sport]
php php/stream.php [ev_a,ev_b | all]
php php/exchange-ladder.php [ev_…]curl + bash, needs jq. The key is passed to curl through a header file or stdin, never on the command line:
curl/list-inplay.sh [sport]
curl/stream.sh [ev_a,ev_b | all]
curl/exchange-ladder.sh [ev_…]Or plain curl, no scripts:
curl -s --compressed -H "X-API-Key: $DIAMONDEXCHAPI_KEY" "https://api-sandbox.diamondexchapi.live/v1/events?state=inplay"
curl -N -H "X-API-Key: $DIAMONDEXCHAPI_KEY" "https://api-sandbox.diamondexchapi.live/v1/stream?events=ev_k9wtucdybikxygkh"openapi.json describes every endpoint, parameter and response as OpenAPI 3.1. The same file is served at https://docs.diamondexchapi.live/openapi.json. Use it to generate a client, import it into an API tool, or hand it to a coding assistant.
postman/diamondexchapi.postman_collection.json has every endpoint, ready to send:
- In Postman, choose Import and pick the file.
- Open the collection's Variables and put your key in
apiKey(in Current value, so it is never shared). - Send the requests in order. In-play events saves an
eventId, One event saves amarketId, and the odds and results calls save their tokens, so each next request just works.
Your key works only from the IP addresses on its allowlist, so send from the Postman desktop app on an allowed machine: requests sent through Postman's cloud come from Postman's servers and are refused. Live stream (SSE) shows each message as it arrives; stop it to close the stream.
- Message us at https://diamondexchapi.live/telegram with the server IPs you will call from. Requests are accepted only from addresses on your allowlist.
- We issue a sandbox key (
fg_test_…). It is shown once; we keep only a hash, so store it safely. A lost key is replaced, not recovered. - Build against the sandbox with these examples and the API reference.
- When you are ready, ask for a live key (
fg_live_…) forhttps://api.diamondexchapi.live.
Keep keys on your servers, in an environment variable or a secret store, never in code or in a URL. More at https://diamondexchapi.live.
MIT. Questions about the API: https://diamondexchapi.live/telegram. Problems with these examples: open an issue, see CONTRIBUTING.md.