Sample code for 30+ languages & platforms
React Native

ETrade Cancel Order

See more ETrade Examples

The cancel order API is used to cancel an existing order.

Chilkat React Native Downloads

React Native
import { Http, HttpResponse, JsonObject, Xml } from '@chilkat/react-native'

async function chilkatExample() {
  // This requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  http.oAuth1 = true;
  http.oAuthVerifier = '';
  http.oAuthConsumerKey = 'ETRADE_CONSUMER_KEY';
  http.oAuthConsumerSecret = 'ETRADE_CONSUMER_SECRET';

  // Load the access token previously obtained via the OAuth1 Authorization
  const jsonToken = new JsonObject();
  try {
    jsonToken.loadFile('qa_data/tokens/etrade.json');
  } catch {
    console.log('Failed to load OAuth1 token');
    return;
  }

  http.oAuthToken = jsonToken.stringOf('oauth_token');
  http.oAuthTokenSecret = jsonToken.stringOf('oauth_token_secret');

  const sandboxUrl = 'https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders/cancel';
  const liveUrl = 'https://api.etrade.com/v1/accounts/{$accountIdKey}/orders/cancel';

  http.setUrlVar('accountIdKey', '6_Dpy0rmuQ9cu9IbTfvF2A');

  // Send a PUT with the following XML body

  // Use this online tool to generate the code from sample XML: 
  // Generate Code to Create XML

  // <CancelOrderRequest>
  //    <orderId>11</orderId>
  // </CancelOrderRequest>

  const xml = new Xml();
  xml.tag = 'CancelOrderRequest';
  xml.updateChildContent('orderId', '11');
  xml.emitCompact = true;

  const httpRequestBody = xml.getXml();
  const resp = new HttpResponse();
  try {
    await http.httpStrAsync('PUT', sandboxUrl, httpRequestBody, 'utf-8', 'application/xml', resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  // Make sure a successful response was received.
  if (resp.statusCode > 200) {
    console.log(resp.statusLine);
    console.log(resp.header);
    console.log(resp.bodyStr);
    return;
  }

  // Sample XML response:

  // Use this online tool to generate parsing code from sample XML: 
  // Generate Parsing Code from XML

  // <CancelOrderResponse>
  //    <accountId>63438617</accountId>
  //    <orderId>11</orderId>
  //    <cancelTime>1529563499081</cancelTime>
  //    <Messages>
  //       <Message>
  //          <code>5011</code>
  //          <description>200|Your request to cancel your order is being processed.</description>
  //          <type>WARNING</type>
  //       </Message>
  //    </Messages>
  // </CancelOrderResponse>

  xml.loadXml(resp.bodyStr);
  console.log(xml.getXml());

  const accountId = xml.getChildIntValue('accountId');
  const orderId = xml.getChildIntValue('orderId');
  const cancelTime = xml.getChildContent('cancelTime');
  const code = xml.getChildIntValue('Messages|Message|code');
  const description = xml.getChildContent('Messages|Message|description');
  const vType = xml.getChildContent('Messages|Message|type');

  console.log('Success.');
}