Sample code for 30+ languages & platforms
React Native

Quickbooks Query an Invoice

See more QuickBooks Examples

Demonstrates how to query for invoices matching a SELECT statement via the Quickbooks REST API.

Chilkat React Native Downloads

React Native
import { JsonObject, Rest, StringBuilder } from '@chilkat/react-native'

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

  // First get our previously obtained OAuth2 access token.
  const jsonToken = new JsonObject();
  jsonToken.loadFile('qa_data/tokens/qb-access-token.json');

  const rest = new Rest();

  const bTls = true;
  const port = 443;
  const bAutoReconnect = true;
  await rest.connectAsync('sandbox-quickbooks.api.intuit.com', port, bTls, bAutoReconnect);

  const sbAuth = new StringBuilder();
  sbAuth.append('Bearer ');
  sbAuth.append(jsonToken.stringOf('access_token'));
  rest.authorization = sbAuth.getAsString();

  // --------------------------------------------------------------------------
  // Note: The above REST connection and setup of the AWS credentials
  // can be done once.  After connecting, any number of REST calls can be made.
  // The "auto reconnect" property passed to rest.Connect indicates that if
  // the connection is lost, a REST method call will automatically reconnect
  // if needed.
  // --------------------------------------------------------------------------

  // This is a GET request, so there should be no Content-Type
  // This line of code is just to make sure..
  rest.removeHeader('Content-Type');

  rest.addHeader('Accept', 'application/json');
  rest.allowHeaderFolding = false;

  // Add a SELECT statement 
  rest.addQueryParam('query', 'select * from Invoice where id = \'239\'');

  const sbResponseBody = new StringBuilder();
  try {
    await rest.fullRequestNoBodySbAsync('GET', '/v3/company/<realmID>/invoice', sbResponseBody);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  const respStatusCode = rest.responseStatusCode;
  if (respStatusCode >= 400) {
    console.log(`Response Status Code = ${respStatusCode}`);
    console.log('Response Header:');
    console.log(rest.responseHeader);
    console.log('Response Body:');
    console.log(sbResponseBody.getAsString());
    return;
  }

  // Success is indicated by a 200 response status.
  console.log(`response status code = ${respStatusCode}`);

  const json = new JsonObject();
  json.loadSb(sbResponseBody);
  json.emitCompact = false;
  console.log(json.emit());

  // Sample output:
  // Use the this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  // {
  //   "QueryResponse": {
  //     "startPosition": 1, 
  //     "totalCount": 1, 
  //     "maxResults": 1, 
  //     "Invoice": [
  //       {
  //         "DocNumber": "1070", 
  //         "SyncToken": "0", 
  //         "domain": "QBO", 
  //         "Balance": 150.0, 
  //         "BillAddr": {
  //           "City": "Bayshore", 
  //           "Line1": "4581 Finch St.", 
  //           "PostalCode": "94326", 
  //           "Lat": "INVALID", 
  //           "Long": "INVALID", 
  //           "CountrySubDivisionCode": "CA", 
  //           "Id": "2"
  //         }, 
  //         "TxnDate": "2015-07-24", 
  //         "TotalAmt": 150.0, 
  //         "CustomerRef": {
  //           "name": "Amy's Bird Sanctuary", 
  //           "value": "1"
  //         }, 
  //         "ShipAddr": {
  //           "City": "Bayshore", 
  //           "Line1": "4581 Finch St.", 
  //           "PostalCode": "94326", 
  //           "Lat": "INVALID", 
  //           "Long": "INVALID", 
  //           "CountrySubDivisionCode": "CA", 
  //           "Id": "109"
  //         }, 
  //         "LinkedTxn": [], 
  //         "DueDate": "2015-08-23", 
  //         "PrintStatus": "NeedToPrint", 
  //         "Deposit": 0, 
  //         "sparse": false, 
  //         "EmailStatus": "NotSet", 
  //         "Line": [
  //           {
  //             "LineNum": 1, 
  //             "Amount": 150.0, 
  //             "SalesItemLineDetail": {
  //               "TaxCodeRef": {
  //                 "value": "NON"
  //               }, 
  //               "ItemRef": {
  //                 "name": "Services", 
  //                 "value": "1"
  //               }
  //             }, 
  //             "Id": "1", 
  //             "DetailType": "SalesItemLineDetail"
  //           }, 
  //           {
  //             "DetailType": "SubTotalLineDetail", 
  //             "Amount": 150.0, 
  //             "SubTotalLineDetail": {}
  //           }
  //         ], 
  //         "ApplyTaxAfterDiscount": false, 
  //         "CustomField": [
  //           {
  //             "DefinitionId": "1", 
  //             "Type": "StringType", 
  //             "Name": "Crew #"
  //           }
  //         ], 
  //         "Id": "239", 
  //         "TxnTaxDetail": {
  //           "TotalTax": 0
  //         }, 
  //         "MetaData": {
  //           "CreateTime": "2015-07-24T10:35:08-07:00", 
  //           "LastUpdatedTime": "2015-07-24T10:35:08-07:00"
  //         }
  //       }
  //     ]
  //   }, 
  //   "time": "2015-07-24T10:38:50.01-07:00"
  // }

  let docNumber = '';
  let syncToken = '';
  let domain = '';
  let balance = '';
  let billAddrCity = '';
  let billAddrLine1 = '';
  let billAddrPostalCode = '';
  let billAddrLat = '';
  let billAddrLong = '';
  let billAddrCountrySubDivisionCode = '';
  let billAddrId = '';
  let txnDate = '';
  let totalAmt = '';
  let customerRefName = '';
  let customerRefValue = '';
  let shipAddrCity = '';
  let shipAddrLine1 = '';
  let shipAddrPostalCode = '';
  let shipAddrLat = '';
  let shipAddrLong = '';
  let shipAddrCountrySubDivisionCode = '';
  let shipAddrId = '';
  let dueDate = '';
  let printStatus = '';
  let deposit = 0;
  let emailStatus = '';
  let id = '';
  let txnTaxDetailTotalTax = 0;
  let metaDataCreateTime = '';
  let metaDataLastUpdatedTime = '';
  let j = 0;
  let countJ = 0;
  let lineNum = 0;
  let amount = '';
  let salesItemLineDetailTaxCodeRefValue = '';
  let salesItemLineDetailItemRefName = '';
  let salesItemLineDetailItemRefValue = '';
  let detailType = '';
  let definitionId = '';
  let invType = '';
  let name = '';

  const queryResponseStartPosition = json.intOf('QueryResponse.startPosition');
  const queryResponseTotalCount = json.intOf('QueryResponse.totalCount');
  const queryResponseMaxResults = json.intOf('QueryResponse.maxResults');
  const time = json.stringOf('time');
  let i = 0;
  const countI = json.sizeOfArray('QueryResponse.Invoice');
  while (i < countI) {
    json.i = i;
    docNumber = json.stringOf('QueryResponse.Invoice[i].DocNumber');
    syncToken = json.stringOf('QueryResponse.Invoice[i].SyncToken');
    domain = json.stringOf('QueryResponse.Invoice[i].domain');
    balance = json.stringOf('QueryResponse.Invoice[i].Balance');
    billAddrCity = json.stringOf('QueryResponse.Invoice[i].BillAddr.City');
    billAddrLine1 = json.stringOf('QueryResponse.Invoice[i].BillAddr.Line1');
    billAddrPostalCode = json.stringOf('QueryResponse.Invoice[i].BillAddr.PostalCode');
    billAddrLat = json.stringOf('QueryResponse.Invoice[i].BillAddr.Lat');
    billAddrLong = json.stringOf('QueryResponse.Invoice[i].BillAddr.Long');
    billAddrCountrySubDivisionCode = json.stringOf('QueryResponse.Invoice[i].BillAddr.CountrySubDivisionCode');
    billAddrId = json.stringOf('QueryResponse.Invoice[i].BillAddr.Id');
    txnDate = json.stringOf('QueryResponse.Invoice[i].TxnDate');
    totalAmt = json.stringOf('QueryResponse.Invoice[i].TotalAmt');
    customerRefName = json.stringOf('QueryResponse.Invoice[i].CustomerRef.name');
    customerRefValue = json.stringOf('QueryResponse.Invoice[i].CustomerRef.value');
    shipAddrCity = json.stringOf('QueryResponse.Invoice[i].ShipAddr.City');
    shipAddrLine1 = json.stringOf('QueryResponse.Invoice[i].ShipAddr.Line1');
    shipAddrPostalCode = json.stringOf('QueryResponse.Invoice[i].ShipAddr.PostalCode');
    shipAddrLat = json.stringOf('QueryResponse.Invoice[i].ShipAddr.Lat');
    shipAddrLong = json.stringOf('QueryResponse.Invoice[i].ShipAddr.Long');
    shipAddrCountrySubDivisionCode = json.stringOf('QueryResponse.Invoice[i].ShipAddr.CountrySubDivisionCode');
    shipAddrId = json.stringOf('QueryResponse.Invoice[i].ShipAddr.Id');
    dueDate = json.stringOf('QueryResponse.Invoice[i].DueDate');
    printStatus = json.stringOf('QueryResponse.Invoice[i].PrintStatus');
    deposit = json.intOf('QueryResponse.Invoice[i].Deposit');
    json.boolOf('QueryResponse.Invoice[i].sparse');
    emailStatus = json.stringOf('QueryResponse.Invoice[i].EmailStatus');
    json.boolOf('QueryResponse.Invoice[i].ApplyTaxAfterDiscount');
    id = json.stringOf('QueryResponse.Invoice[i].Id');
    txnTaxDetailTotalTax = json.intOf('QueryResponse.Invoice[i].TxnTaxDetail.TotalTax');
    metaDataCreateTime = json.stringOf('QueryResponse.Invoice[i].MetaData.CreateTime');
    metaDataLastUpdatedTime = json.stringOf('QueryResponse.Invoice[i].MetaData.LastUpdatedTime');
    j = 0;
    countJ = json.sizeOfArray('QueryResponse.Invoice[i].LinkedTxn');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('QueryResponse.Invoice[i].Line');
    while (j < countJ) {
      json.j = j;
      lineNum = json.intOf('QueryResponse.Invoice[i].Line[j].LineNum');
      amount = json.stringOf('QueryResponse.Invoice[i].Line[j].Amount');
      salesItemLineDetailTaxCodeRefValue = json.stringOf('QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value');
      salesItemLineDetailItemRefName = json.stringOf('QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name');
      salesItemLineDetailItemRefValue = json.stringOf('QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value');
      id = json.stringOf('QueryResponse.Invoice[i].Line[j].Id');
      detailType = json.stringOf('QueryResponse.Invoice[i].Line[j].DetailType');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('QueryResponse.Invoice[i].CustomField');
    while (j < countJ) {
      json.j = j;
      definitionId = json.stringOf('QueryResponse.Invoice[i].CustomField[j].DefinitionId');
      invType = json.stringOf('QueryResponse.Invoice[i].CustomField[j].Type');
      name = json.stringOf('QueryResponse.Invoice[i].CustomField[j].Name');
      j++;
    }

    i++;
  }
}