Deriv API
K

Cancel

Trading
Auth Required

Cancel an active contract by providing the contract ID. The loginid parameter has been removed and response fields are now guaranteed.

Quick comparison

AspectLegacyNewAction required
Endpoint
✅ Same
cancelcancelNone
Auth Required
✅ Same
YesYesNone
Scope
✅ Same
tradetradeNone
loginid Parameter
❌ Removed
OptionalRemovedRemove from requests
cancel Object in Response
⚠️ Changed
OptionalRequiredSimplify error handling
Response Fields
⚠️ Changed
All optionalAll requiredRemove null checks

Breaking changes

1. Removal of loginid parameter

What changed: The loginid parameter is no longer supported in New. In Legacy, this parameter was used to specify which account to cancel a contract from when multiple tokens were provided during authorization.

Migration action: Remove the loginid field from your cancel requests. Account selection is now handled through the authorization token.

2. cancel object now required in response

What changed: The cancel object is now a required field at the root level of the response. In Legacy, only echo_req andmsg_type were required.

Migration action: You can now rely on the cancel object always being present in successful responses. Remove any conditional checks for its existence.

3. All response fields now required

What changed: All fields within the cancel response object are now required: balance_after,contract_id,reference_id,sold_for, andtransaction_id. In Legacy, all these fields were optional.

Migration action: You can simplify your response handling code by removing null/undefined checks for these fields. They are guaranteed to be present in successful responses.

Request structure

Legacy Request Example

1{
2  "cancel": 11542203588,
3  "loginid": "CR123456",  // ❌ Removed in New
4  "passthrough": {
5    "user_action": "manual_cancel"
6  },
7  "req_id": 1
8}

New Request Example

1{
2  "cancel": 11542203588,
3  "passthrough": {
4    "user_action": "manual_cancel"
5  },
6  "req_id": 1
7}

Response structure

Legacy Response Example

1{
2  "cancel": {
3    "balance_after": 10450.50,
4    "contract_id": 11542203588,
5    "reference_id": 11542203587,
6    "sold_for": 23.45,
7    "transaction_id": 11542203589
8  },
9  "echo_req": {
10    "cancel": 11542203588,
11    "req_id": 1
12  },
13  "msg_type": "cancel",
14  "req_id": 1
15}

New Response Example

1{
2  "cancel": {
3    "balance_after": 10450.50,
4    "contract_id": 11542203588,
5    "reference_id": 11542203587,
6    "sold_for": 23.45,
7    "transaction_id": 11542203589
8  },
9  "echo_req": {
10    "cancel": 11542203588,
11    "req_id": 1
12  },
13  "msg_type": "cancel",
14  "req_id": 1
15}

Code examples

Legacy Implementation

1async function cancelContract(contractId, loginId = null) {
2  const request = {
3    cancel: contractId,
4    req_id: 1
5  };
6  
7  // Add loginid if provided (for multi-account scenarios)
8  if (loginId) {
9    request.loginid = loginId;
10  }
11  
12  ws.send(JSON.stringify(request));
13  
14  // Handle response
15  ws.onmessage = (message) => {
16    const response = JSON.parse(message.data);
17    if (response.msg_type === 'cancel') {
18      // Fields may be undefined - need null checks
19      if (response.cancel) {
20        const soldFor = response.cancel.sold_for ?? 0;
21        const balance = response.cancel.balance_after ?? 0;
22        console.log(`Contract cancelled`);
23        console.log(`Sold for: ${soldFor}`);
24        console.log(`New balance: ${balance}`);
25      }
26    }
27  };
28}
29
30// Usage with loginid for multi-account
31cancelContract(11542203588, 'CR123456');

New Implementation

1async function cancelContract(contractId) {
2  const request = {
3    cancel: contractId,
4    req_id: 1
5  };
6  // loginid removed - account selection via auth token
7  
8  ws.send(JSON.stringify(request));
9  
10  // Handle response
11  ws.onmessage = (message) => {
12    const response = JSON.parse(message.data);
13    if (response.msg_type === 'cancel') {
14      // All fields guaranteed - no null checks needed
15      console.log(`Contract ${response.cancel.contract_id} cancelled`);
16      console.log(`Sold for: ${response.cancel.sold_for}`);
17      console.log(`New balance: ${response.cancel.balance_after}`);
18      console.log(`Reference ID: ${response.cancel.reference_id}`);
19      console.log(`Transaction ID: ${response.cancel.transaction_id}`);
20    }
21  };
22}
23
24// Usage without loginid
25cancelContract(11542203588);

Migration checklist

Remove loginid parameter

Remove the loginid field from all cancel requests

Simplify response handling

The cancel object is now guaranteed in successful responses - remove existence checks

Remove null/undefined checks for response fields

All fields (balance_after, contract_id, reference_id, sold_for, transaction_id) are now required

Click to open live chat support. Get instant help from our support team.