Deriv API
Documentation

Buy

TradingAuth Required

Purchase a contract. The loginid parameter has been removed and buy field validation relaxed.

Quick comparison

AspectLegacy APINew APIAction
EndpointbuybuyNone
Auth RequiredYesYesNone
loginid ParameterOptionalRemovedRemove from requests
buy Field PatternStrict (32-128 chars)RelaxedReview validation

Breaking changes

1. loginid parameter removed

The loginid parameter has been removed in New. This parameter was previously used for multi-account authentication.

Required change: Remove the loginid field from your buy requests. Account selection is now handled through the authorization token.

2. buy field validation relaxed

The buy field no longer enforces the strict pattern ^(:[\w-]{32,128}|1)$.

Required change: If you have client-side validation matching the legacy pattern, you may relax it.

3. buy object now required in response

The buy object is now a required field in the response.

Required change: Update error handling to expect the buy object to always be present in successful responses.

Request structure

Legacy APIrequest example
{
  "buy": "uw2mk7no3oktoRVVsB4Dz7TQnFfAB",
  "price": 10.50,
  "loginid": "CR123456"
}
New APIrequest example
{
  "buy": "uw2mk7no3oktoRVVsB4Dz7TQnFfAB",
  "price": 10.50
}

Response structure

Legacy APIresponse example
{
  "buy": {
    "balance_after": 9989.50,
    "buy_price": 10.50,
    "contract_id": 123456789,
    "longcode": "Win payout if Volatility 100 Index is strictly higher than entry spot at 5 minutes after contract start time.",
    "payout": 20.00,
    "purchase_time": 1234567890,
    "shortcode": "CALL_R_100_20_1234567890_1234568190_S0P_0",
    "start_time": 1234567890,
    "transaction_id": 987654321
  },
  "echo_req": { "buy": "uw2mk7no3oktoRVVsB4Dz7TQnFfAB", "price": 10.50 },
  "msg_type": "buy"
}
New APIresponse example
{
  "buy": {
    "balance_after": 9989.50,
    "buy_price": 10.50,
    "contract_id": 123456789,
    "longcode": "Win payout if Volatility 100 Index is strictly higher than entry spot at 5 minutes after contract start time.",
    "payout": 20.00,
    "purchase_time": 1234567890,
    "shortcode": "CALL_R_100_20_1234567890_1234568190_S0P_0",
    "start_time": 1234567890,
    "transaction_id": 987654321
  },
  "echo_req": { "buy": "uw2mk7no3oktoRVVsB4Dz7TQnFfAB", "price": 10.50 },
  "msg_type": "buy"
}

Code examples

async function buyContract(proposalId, price, loginid) {
  const request = {
    buy: proposalId,
    price: price,
    // loginid used for multi-account support
    loginid: loginid
  };

  ws.send(JSON.stringify(request));

  ws.onmessage = (event) => {
    const response = JSON.parse(event.data);
    if (response.msg_type === "buy") {
      // buy object may be missing in some cases
      if (response.buy) {
        console.log("Contract ID:", response.buy.contract_id);
      }
    }
  };
}
Click to open live chat support. Get instant help from our support team.