API v2

Errors

What the API returns when something is wrong, and which failures are worth retrying.

Both halves of v2 — the Account API and each account's Trading API — answer errors in the same shape, so one handler covers both:

{
  "error": "NO_SLOT_AVAILABLE",
  "message": "Account limit reached — 5 of 5 in use. Upgrade your plan or contact support."
}

error is a stable code worth branching on. message is written for a human and may change; do not match on it. Trading API errors also carry a command_id — quote it when you report a problem.

Status codes

StatusWhenRetry?
400The request was malformed — a missing field, an unknown platform, a symbol the broker does not offer, a ticket that does not existNo, fix the request
401No API key, or one that is not validNo, check the key
403Authenticated, but not allowed — no free account slotNo, free a slot or upgrade
404No such account, or no access to itNo
409The clientOrderId was already used for a different orderNo, use a new id
429Too many different accounts connected on your slots recentlyYes, once the window frees up
500The account could not answer this one — see the Trading API codesDepends on the code
503The connection is not answering or is still starting, or v2 is not available for your account yetYes, with backoff
504The connection did not answer in timeOn orders, depends on the code — see Timeouts on orders

404 also means “no access”

An account you have no access to answers 404, the same as one that does not exist.

Codes

Account API

CodeMeaning
UNAUTHORIZEDMissing or invalid API key
INVALID_REQUESTThe body failed validation; message names the field
NO_SLOT_AVAILABLEYour plan's account slots are all in use
NOT_FOUNDNo such account, or no access to it
TOO_MANY_NEW_ACCOUNTSConnection limit reached — see below
UNAVAILABLEv2 cannot serve this request right now
TIMEOUTThe connection did not answer in time

Trading API

CodeStatusMeaningRetry?
UNAUTHORIZED401Missing or invalid API keyNo
MRPC_VALIDATION400A parameter is missing or invalid; message names itNo
MRPC_UNKNOWN_SYMBOL400The broker has no such symbolNo
MRPC_UNKNOWN_TICKET400No such order or positionNo
MRPC_BAD_TIMEFRAME400The timeframe is not recognisedNo
MRPC_UNSUPPORTED400The platform cannot do this — stop-limit orders on MT4No
MRPC_CLIENT_ORDER_ID_REUSED409The clientOrderId was already used for a different orderNo
MRPC_SYMBOL_NOT_QUOTING500The symbol has no price yet — prepare it with POST /SymbolSelectYes
MRPC_TICKET_BUSY500Another request for this ticket is still running; nothing was sentYes
MRPC_TRADE_EA_STOPPED500Trading stopped on the connection before the order was sent; nothing was executedYes, with backoff
MRPC_HISTORY_UNAVAILABLE, MRPC_PRICE_HISTORY_UNAVAILABLE, MRPC_CALC_FAILED500The broker did not deliver the dataYes, with backoff
MRPC_INTERNAL, MRPC_UNKNOWN_COMMAND500An internal fault — report it with the command_idNo
MRPC_TRADE_EA_NOT_READY503The connection is still starting up and does not take orders yetYes, with backoff
MRPC_ABANDONED504The order never reached the broker — not executedYes
MRPC_TIMEOUT504No answer in time; the request was not executedYes
MRPC_TRADE_OUTCOME_UNKNOWN504The order reached the broker and no verdict came back — it may have executedOnly with the same clientOrderId

Timeouts on orders

Order calls (/OrderSend, /OrderModify, /OrderClose) wait up to 60 seconds for the broker. When that runs out, three 504s mean different things, so branch on the code, never on the status alone:

  • MRPC_ABANDONED and MRPC_TIMEOUT — nothing was executed. Sending the order again is safe.
  • MRPC_TRADE_OUTCOME_UNKNOWN — the order was handed to the broker, so it may be live. Do not simply send it again. Either repeat it with the same clientOrderId (below) — that answers with the order's real result once it is known, and never places it twice — or check GET /OpenedOrders and GET /OrderHistory first, matching on your own expertId (magic in the rows) and comment.

Retrying an order safely

Give every POST /OrderSend a clientOrderId of your own, 1–64 characters. Within 10 minutes, a request with the same id places nothing new: it is answered with the first request's result — waiting for it if it is still running — and carries the header Idempotent-Replayed: true. The same id with a different order is refused with 409 MRPC_CLIENT_ORDER_ID_REUSED. After MRPC_ABANDONED the id is free again.

Node.js — one order, retried safely
import { randomUUID } from 'node:crypto'

const ACCOUNT = 'your-account-uuid'
const BASE = `https://api.api2trade.com/v2/${ACCOUNT}`

async function placeOrder(order) {
  // The body — and with it the id — stays the same on every attempt.
  const body = JSON.stringify({ ...order, clientOrderId: randomUUID() })
  for (let attempt = 0; attempt < 4; attempt++) {
    const res = await fetch(`${BASE}/OrderSend`, {
      method: 'POST',
      headers: { 'X-API-Key': process.env.API2TRADE_API_KEY, 'Content-Type': 'application/json' },
      body,
    })
    if (res.status !== 503 && res.status !== 504) return res.json()
    await new Promise((r) => setTimeout(r, 1000 * 2 ** attempt))
  }
  throw new Error('no answer — check /OpenedOrders before sending this order again')
}

Ids are kept for 10 minutes, in the connection

A retry later than that — or after the connection itself restarted — is treated as a new order. If you cannot retry within a few minutes, check /OpenedOrders and /OrderHistory instead.

Check the version first

clientOrderId works from version 0.17.0 on MT5 and 0.15.0 on MT4 — GET /status reports it as bridge.version. An older version ignores the field, so a retry there places a second order. Connections move to the new version when they restart; until then, reconcile before you resend.

The connection limit

Your slots cap how many accounts run at once. Separately, there is a limit on how many different accounts you may connect within a window — ten per slot per hour — so a single slot cannot be used to serve an endless series of accounts.

{
  "error": "TOO_MANY_NEW_ACCOUNTS",
  "message": "You have connected 10 different accounts in the last 1 hours, which is the limit for your plan. …"
}

Reconnecting an account you already used is free

The limit counts distinct account numbers, not attempts. If you disconnect and reconnect the same account — after fixing a password, say — it does not count against the limit, however many times you do it.

Failures that are not HTTP errors

A connected account can be unhealthy while every request still returns 200. This is the one thing worth building for deliberately.

A connection that has not logged in to the broker answers GET /AccountSummary with 200 and all zeros — a balance of 0 is indistinguishable from a real empty account unless you look at the session:

GET /status on an account that never logged in
{
  "status": "degraded",
  "broker": { "connected": false, "server": "VTMarkets-Demo" },
  "account": { "loggedIn": false, "login": 1082118 }
}

So before trusting any figure, check that broker.connected and account.loggedIn are both true. GET /status always returns 200; the state is in the body.

An order the broker rejects is a 200 too: the call worked, the broker said no. Check success and retcode in the body of every order call.

While an account is still starting

Connecting an account returns straight away, but the connection needs a few minutes to start and log in. Poll the account on the Account API until terminalStatus settles:

terminalStatusMeaning
provisioningStarting up or logging in. Wait.
readyLogged in and serving
errorIt will not come up on its own — terminalLastError says why

A rejected login is reported as soon as the broker rejects it, with the broker's own wording:

{
  "terminalStatus": "error",
  "terminalLastError": "VTMarkets-Demo rejected this login: Invalid account. Check account number, password and server."
}

That is not worth retrying — the account number, password or server name is wrong. Fix it and connect again.