Idempotent Requests
Network errors and timeouts happen. If your client does not get an answer, it cannot know whether the request was processed or not. Sending it again could, for example, create the same copier twice.
The optional Idempotency-Key header makes POST requests safe to retry: the request is processed only once, and a retry returns the response of the first request.
Without the header, all endpoints behave exactly as before. The header is optional.
How It Works
- Generate a unique key for each operation, for example a UUID.
- Send the
POSTrequest with the headerIdempotency-Key: <your key>. - If you get no answer or a timeout, send the same request with the same key again.
POST /rest/api/v1/accounts/{accountId}/copiers HTTP/1.1
Host: api.metacopier.io
X-API-KEY: <your API key>
Idempotency-Key: 3f8b5c7e-2d4a-4e1b-9c6f-8a0d2e7b1c45
Content-Type: application/json
{ ... }
What happens with the retry depends on the state of the first request:
| State of the first request | Answer to the retry |
|---|---|
| Still running | 409 with [IDEMPOTENCY_REQUEST_IN_PROGRESS] and a Retry-After header (seconds). Wait and try again. |
Succeeded (2xx) | The stored response of the first request, with the header Idempotency-Replayed: true. The request is not processed again. |
Failed (not 2xx) | Nothing was stored. The request is processed again as a new request. |
Rules
- The key must be 1-255 printable ASCII characters. A UUID is recommended. An empty header is ignored.
- Keys are scoped to the API key. Two different API keys can use the same key without affecting each other.
- A key belongs to exactly one request (method, path, query parameters and body). Reusing it for a different request gives
422with[IDEMPOTENCY_KEY_REUSED_WITH_DIFFERENT_REQUEST]. - Successful responses are stored for 15 minutes. After that the key expires and can be reused.
- The stored response contains the status code, the
Content-Typeand the body. Other response headers are not replayed. - Keys are not kept across maintenance restarts of the API. Use the key as protection against retries within a short time, not as permanent deduplication.
Supported URLs
The header is supported for POST requests on the global API URL https://api.metacopier.io.
The regional Trading API URLs (api-newyork, api-london, ...) ignore the header. To open positions and orders safely there, use the requestId field described below.
The TradingView Webhook API has its own duplicate protection and ignores the header.
Opening Positions and Orders: requestId
POST /accounts/{accountId}/positions and POST /accounts/{accountId}/orders have their own protection against duplicate trades, which works on all URLs, including the regional ones.
Set the requestId field in the request body. Count it up from 0 to 999 per account, then start again at 0. When the same requestId is sent again:
| State of the first request | Answer to the retry |
|---|---|
| Still running | Error [REQUEST_IN_PROGRESS] |
| Position opened | Error [REQUEST_ALREADY_PROCESSED]. No second position is opened. |
| Failed without opening a position | The request is executed again. |
The requestId is also checked against the open positions at the broker. Even after a timeout, a position that reached the broker is not opened a second time.
Tip: On the global URL you can combine both: the
requestIdprotects the trade, and theIdempotency-Keyreturns the original success response instead of[REQUEST_ALREADY_PROCESSED].
Errors
| Status | Error code | Meaning |
|---|---|---|
400 | [IDEMPOTENCY_KEY_INVALID] | The key is longer than 255 characters or contains characters that are not printable ASCII. |
409 | [IDEMPOTENCY_REQUEST_IN_PROGRESS] | A request with this key is still running. Retry after Retry-After seconds. |
413 | [IDEMPOTENCY_REQUEST_TOO_LARGE] | The JSON body is larger than 1 MB. |
422 | [IDEMPOTENCY_KEY_REUSED_WITH_DIFFERENT_REQUEST] | The key was already used for a different request. Use a new key. |
429 | [TOO_MANY_IDEMPOTENCY_KEYS] | Too many active keys for this API key (10,000 within 15 minutes). Do not send a new key for every request if you do not need it. |