MagicMarkets Heartbeats API
Heartbeat timers that automatically close all open orders when they expire. Use these as a dead-man's switch for automated trading.
Heartbeat timers that automatically close all open orders when they expire. Use these as a dead-man's switch for automated trading.
Every API here is available over the APIs.io API and to AI agents over MCP.
One button, every client — Claude, Cursor, VS Code and the rest.
https://apis.io/mcp
find_apisBrowse and filter every API in the catalog.get_api_artifactsOne API's artifacts, grouped by type.get_openapiThe primary OpenAPI for this API.find_similar_apisAPIs that look like this one.apis_io_searchSTART HERE — APIs, providers and tags for one query, each with its total.resolveTurn a domain, URL or GitHub org into the provider it belongs to.find_cohortsEvery scored population of providers in the catalog.curl "https://apis.io/api/v1/apis/magicmarkets-com:magicmarkets-com-heartbeats-api"
curl "https://apis.io/api/v1/apis?limit=25"
Calls without X-API-Key need no key. Calls with it need a paid plan — get a key.
Free tier, no form to fill in. Signing in shares your email address with us — we store it to create your key and to recognise you if you sign in with another provider. By signing up you are subscribing to the APIs.io and API Evangelist newsletters — you can unsubscribe from any issue. See our Privacy Policy and Terms.
A second provider on the same verified email joins the account you already have.
openapi: 3.2.0
info:
title: MagicMarkets Heartbeats API
version: 0.1.0
description: 'REST and WebSocket API for MagicMarkets. Place orders, stream real-time
prices, and manage your positions programmatically. ## Authentication
Every request must be authenticated with an **API key**. Pass it in the
`X-Api-Key` header:
```
X-Api-Key:
```
### Getting an API key
API keys are created and managed through the MagicMarkets website at
magicmarkets.com:
1. Go to **Settings -> API**. 2. Click **Add API Key** and give it a name. 3. Copy the key value - it is shown **once at creation**, so store it
somewhere safe immediately. You can also revoke or rename existing keys from the same page. There is
no endpoint in this API to manage keys - all key management happens on
the website. ### Using a key
Send the key on every request:
```bash
curl https://magicmarkets.com/v2/xrates/ \
-H "X-Api-Key: $MAGIC_API_KEY"
```
## Quickstart
This walkthrough goes from zero to a placed bet: stream prices over the
WebSocket, read a `bet_type` off the feed, quote it as a betslip, place
an order, and watch the order update on the same socket. ### Before you start
You need an API key (see **Authentication** above) and Python 3.9+ with
two libraries:
```bash
pip install requests websockets
```
Every snippet reads its configuration from the environment:
```bash
export MAGIC_API_URL="https:///v2"
export MAGIC_WS_URL="wss:///v2/stream"
export MAGIC_API_KEY=""
```
Three concepts carry the whole flow:
- **Two steps to a bet.** A *betslip* registers your interest in one
selection (`POST /v2/betslips/`); its live quote then arrives as
`["pmm", ...]` entries on the WebSocket. An *order* commits a stake
against that quote (`POST /v2/orders/`). You always create the
betslip first. - **`bet_type` comes from the feed.** Offers on the WebSocket carry the
`bet_type` string ready to use - pass it to `POST /v2/betslips/`
verbatim. You never need to construct or parse it (the grammar in
**Sports & bet types** is reference material, not required reading). - **Stakes are USDT.** Wherever money appears it is a
`["USDT", amount]` pair. ### Step 1 - Check your key
Before opening the socket, prove the key works with a cheap REST call -
a bad key fails the WebSocket upgrade with HTTP 401, so verify the key
first:
```bash
curl "$MAGIC_API_URL/xrates/" -H "X-Api-Key: $MAGIC_API_KEY"
```
A `200` with `{"status": "ok", ...}` means you are good to go. ### Step 2 - Connect to the stream
Connect with the key as a query parameter. Every frame the server sends
is a batch envelope `{"ts": ..., "data": [...]}` - iterate `data[]` and
dispatch on each entry''s leading type tag (see the **Streaming API**
endpoint for the full wire format):
```python
import json, os
from websockets.sync.client import connect
ws = connect(f"{os.environ[''MAGIC_WS_URL'']}?api_key={os.environ[''MAGIC_API_KEY'']}")
events, synced = [], False
while not synced:
frame = json.loads(ws.recv())
for entry in frame["data"]:
if entry[0] == "event":
events.append(entry[1]) # {"sport": ..., "event_id": ..., ...}
elif entry[0] == "sync":
synced = True
```
After `["sync", ...]` you hold the list of currently-priced events, e.g.:
```json
["event", {"event_type": "normal", "sport": "fb",
"event_id": "2026-06-15,1001,2002", "competition_id": 1,
"competition_name": "England Premier League",
"competition_country": "XE", "home": "Arsenal", "away": "Chelsea",
"event_name": "Arsenal vs. Chelsea", "ir_status": "pre_event",
"start_time": "2026-06-15T15:00:00Z"}]
```
### Step 3 - Register for offers on an event
Pick an event and register.'
security:
- ApiKeyHeader: []
tags:
- name: Heartbeats
description: Heartbeat timers that automatically close all open orders when they expire. Use these as a dead-man's switch for automated trading.
paths:
/v2/heartbeats/:
post:
tags:
- Heartbeats
summary: Create heartbeat
description: Open a new heartbeat timer. If the timer expires before being refreshed or cancelled, every open order on the account is closed, including orders placed by other sessions or API keys; only orders created after the expiry instant are spared. Closing cancels the unfilled stake and withdraws unmatched liquidity advertised on the exchange; bets that already matched are unaffected. Expiry is evaluated server-side about once per second.
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- timeout
properties:
timeout:
type: integer
minimum: 10
maximum: 300
description: Seconds before the heartbeat expires
example:
timeout: 60
responses:
'401':
$ref: '#/components/responses/Error401'
'403':
$ref: '#/components/responses/Error403'
'429':
$ref: '#/components/responses/Error429'
'500':
$ref: '#/components/responses/Error500'
'400':
description: '`timeout` outside the 10-300 second range'
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: validation_error
data:
validation_errors:
timeout:
- Ensure this value is less than or equal to 300.
'200':
description: Heartbeat created
content:
application/json:
schema:
$ref: '#/components/schemas/HeartbeatEnvelope'
example:
status: ok
data:
heartbeat_id: hb-abc123
expiry_time: '2026-06-15T12:01:00+00:00'
operationId: postV2Heartbeats
x-operation-id-source: derived
get:
tags:
- Heartbeats
summary: List heartbeats
description: Returns all currently open heartbeats. **Note:** unlike other list endpoints (`/v2/orders/`, `/v2/betslips/`) which return a flat array under `data`, this endpoint wraps the array under `data.heartbeats` for historical reasons. Clients must special-case this shape.
responses:
'401':
$ref: '#/components/responses/Error401'
'403':
$ref: '#/components/responses/Error403'
'429':
$ref: '#/components/responses/Error429'
'500':
$ref: '#/components/responses/Error500'
'200':
description: Open heartbeats
content:
application/json:
schema:
$ref: '#/components/schemas/HeartbeatListEnvelope'
example:
status: ok
data:
heartbeats:
- heartbeat_id: hb-abc123
expiry_time: '2026-06-15T12:01:00+00:00'
operationId: getV2Heartbeats
x-operation-id-source: derived
/v2/heartbeats/{heartbeat_id}/:
get:
tags:
- Heartbeats
summary: Get heartbeat
description: Returns information about a single heartbeat.
parameters:
- name: heartbeat_id
in: path
required: true
schema:
type: string
responses:
'401':
$ref: '#/components/responses/Error401'
'403':
$ref: '#/components/responses/Error403'
'429':
$ref: '#/components/responses/Error429'
'500':
$ref: '#/components/responses/Error500'
'404':
description: Heartbeat not found or not visible to this key
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: not_found
data: null
'200':
description: Heartbeat info
content:
application/json:
schema:
$ref: '#/components/schemas/HeartbeatEnvelope'
example:
status: ok
data:
heartbeat_id: hb-abc123
expiry_time: '2026-06-15T12:01:00+00:00'
operationId: getV2HeartbeatsByHeartbeatId
x-operation-id-source: derived
delete:
tags:
- Heartbeats
summary: Cancel heartbeat
description: Cancel an active heartbeat. Cancelling disarms the timer without closing any orders; only expiry triggers the order close-out. The response `data` field is always `null`.
parameters:
- name: heartbeat_id
in: path
required: true
schema:
type: string
responses:
'401':
$ref: '#/components/responses/Error401'
'403':
$ref: '#/components/responses/Error403'
'429':
$ref: '#/components/responses/Error429'
'500':
$ref: '#/components/responses/Error500'
'404':
description: Heartbeat not found or already cancelled
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: not_found
data: null
'200':
description: Heartbeat cancelled
content:
application/json:
schema:
$ref: '#/components/schemas/HeartbeatCancelEnvelope'
example:
status: ok
data: null
operationId: deleteV2HeartbeatsByHeartbeatId
x-operation-id-source: derived
/v2/heartbeats/{heartbeat_id}/refresh/:
post:
tags:
- Heartbeats
summary: Refresh heartbeat
description: Extend the heartbeat expiration timeout. A heartbeat that has already expired cannot be refreshed - its close-out has been triggered; open a new heartbeat instead.
parameters:
- name: heartbeat_id
in: path
required: true
schema:
type: string
responses:
'401':
$ref: '#/components/responses/Error401'
'403':
$ref: '#/components/responses/Error403'
'429':
$ref: '#/components/responses/Error429'
'500':
$ref: '#/components/responses/Error500'
'404':
description: Heartbeat not found, expired, or cancelled
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: not_found
data: null
'200':
description: Heartbeat extended
content:
application/json:
schema:
$ref: '#/components/schemas/HeartbeatEnvelope'
example:
status: ok
data:
heartbeat_id: hb-abc123
expiry_time: '2026-06-15T12:02:00+00:00'
operationId: postV2HeartbeatsByHeartbeatIdRefresh
x-operation-id-source: derived
components:
schemas:
HeartbeatCancelEnvelope:
type: object
description: Cancel response. `data` is always `null`.
required:
- status
- data
properties:
status:
type: string
enum:
- ok
data:
type: 'null'
HeartbeatResponse:
type: object
required:
- heartbeat_id
- expiry_time
properties:
heartbeat_id:
type: string
expiry_time:
type: string
format: date-time
HeartbeatEnvelope:
type: object
required:
- status
- data
properties:
status:
type: string
enum:
- ok
data:
$ref: '#/components/schemas/HeartbeatResponse'
ErrorEnvelope:
type: object
description: Standard error response. The shape of `data` varies by `code` - see the **Errors** section in the introduction and the named examples on each endpoint's error responses.
required:
- status
- code
properties:
status:
type: string
enum:
- error
code:
type: string
description: Stable machine-readable error code (e.g. `validation_error`, `not_found`, `forbidden`).
example: validation_error
data:
description: Variable. Null, string, or object - see examples.
HeartbeatListEnvelope:
type: object
description: List response. Note the data is wrapped under a `heartbeats` key, unlike other list endpoints which return a flat array.
required:
- status
- data
properties:
status:
type: string
enum:
- ok
data:
type: object
required:
- heartbeats
properties:
heartbeats:
type: array
items:
$ref: '#/components/schemas/HeartbeatResponse'
responses:
Error401:
description: Authentication failed - API key missing, malformed, or rejected.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: auth_error
data:
detail: Authentication credentials were not provided.
Error429:
description: Rate limit hit. Honour the `Retry-After` header.
headers:
Retry-After:
description: Seconds to wait before retrying.
schema:
type: integer
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: throttled
data:
message: Request was throttled. Expected available in 12 seconds.
retry_after: 12
Error500:
description: Unexpected internal error. Quote `data[1]` (the token) when contacting support.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: server_error
data:
- 'An error has occurred, token:'
- ABC123abc456DEF789gh
Error403:
description: Authorization failed - the key is valid but cannot perform this action.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorEnvelope'
example:
status: error
code: forbidden
data: null
securitySchemes:
ApiKeyHeader:
type: apiKey
in: header
name: X-Api-Key
description: API key created from the magic-markets website