Sample code for 30+ languages & platforms
React Native

Quickbooks Create an Invoice

See more QuickBooks Examples

Demonstrates how to create an invoice 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();

  // Connect to the REST server.
  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 code to setup the initial REST connection
  // can be done once.  After connecting, any number of REST calls can be made.
  // If the connection is lost, the next REST method call will automatically
  // reconnect if needed.
  // --------------------------------------------------------------------------

  // Create the following JSON:

  // {
  //   "Line": [
  //     {
  //       "DetailType": "SalesItemLineDetail",
  //       "Amount": 100.0,
  //       "SalesItemLineDetail": {
  //         "ItemRef": {
  //           "name": "Services",
  //           "value": "1"
  //         }
  //       }
  //     }
  //   ],
  //   "CustomerRef": {
  //     "value": "1"
  //   }
  // }
  // 
  // Use the this online tool to generate the code from sample JSON: 
  // Generate Code to Create JSON

  const jsonReq = new JsonObject();
  jsonReq.updateString('Line[0].DetailType', 'SalesItemLineDetail');
  jsonReq.updateNumber('Line[0].Amount', '100.0');
  jsonReq.updateString('Line[0].SalesItemLineDetail.ItemRef.name', 'Services');
  jsonReq.updateString('Line[0].SalesItemLineDetail.ItemRef.value', '1');
  jsonReq.updateString('CustomerRef.value', '1');

  const sbRequestBody = new StringBuilder();
  jsonReq.emitSb(sbRequestBody);

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

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

  const respStatusCode = rest.responseStatusCode;

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

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

  if (rest.responseStatusCode !== 200) {
    console.log('Failed.');
    return;
  }

  // Sample output...
  // (See the parsing code below..)
  // 
  // Use the this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  // {
  //   "Invoice": {
  //     "DocNumber": "1069",
  //     "SyncToken": "0",
  //     "domain": "QBO",
  //     "Balance": 100.0,
  //     "BillAddr": {
  //       "City": "Bayshore",
  //       "Line1": "4581 Finch St.",
  //       "PostalCode": "94326",
  //       "Lat": "INVALID",
  //       "Long": "INVALID",
  //       "CountrySubDivisionCode": "CA",
  //       "Id": "2"
  //     },
  //     "TxnDate": "2015-07-24",
  //     "TotalAmt": 100.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": 100.0,
  //         "SalesItemLineDetail": {
  //           "TaxCodeRef": {
  //             "value": "NON"
  //           },
  //           "ItemRef": {
  //             "name": "Services",
  //             "value": "1"
  //           }
  //         },
  //         "Id": "1",
  //         "DetailType": "SalesItemLineDetail"
  //       },
  //       {
  //         "DetailType": "SubTotalLineDetail",
  //         "Amount": 100.0,
  //         "SubTotalLineDetail": {}
  //       }
  //     ],
  //     "ApplyTaxAfterDiscount": false,
  //     "CustomField": [
  //       {
  //         "DefinitionId": "1",
  //         "Type": "StringType",
  //         "Name": "Crew #"
  //       }
  //     ],
  //     "Id": "238",
  //     "TxnTaxDetail": {
  //       "TotalTax": 0
  //     },
  //     "MetaData": {
  //       "CreateTime": "2015-07-24T10:33:39-07:00",
  //       "LastUpdatedTime": "2015-07-24T10:33:39-07:00"
  //     }
  //   },
  //   "time": "2015-07-24T10:33:39.11-07:00"
  // }
  // 

  let lineNum = 0;
  let amount = '';
  let salesItemLineDetailTaxCodeRefValue = '';
  let salesItemLineDetailItemRefName = '';
  let salesItemLineDetailItemRefValue = '';
  let id = '';
  let detailType = '';
  let definitionId = '';
  let invType = '';
  let name = '';

  const invoiceDocNumber = jsonResponse.stringOf('Invoice.DocNumber');
  const invoiceSyncToken = jsonResponse.stringOf('Invoice.SyncToken');
  const invoiceDomain = jsonResponse.stringOf('Invoice.domain');
  const invoiceBalance = jsonResponse.stringOf('Invoice.Balance');
  const invoiceBillAddrCity = jsonResponse.stringOf('Invoice.BillAddr.City');
  const invoiceBillAddrLine1 = jsonResponse.stringOf('Invoice.BillAddr.Line1');
  const invoiceBillAddrPostalCode = jsonResponse.stringOf('Invoice.BillAddr.PostalCode');
  const invoiceBillAddrLat = jsonResponse.stringOf('Invoice.BillAddr.Lat');
  const invoiceBillAddrLong = jsonResponse.stringOf('Invoice.BillAddr.Long');
  const invoiceBillAddrCountrySubDivisionCode = jsonResponse.stringOf('Invoice.BillAddr.CountrySubDivisionCode');
  const invoiceBillAddrId = jsonResponse.stringOf('Invoice.BillAddr.Id');
  const invoiceTxnDate = jsonResponse.stringOf('Invoice.TxnDate');
  const invoiceTotalAmt = jsonResponse.stringOf('Invoice.TotalAmt');
  const invoiceCustomerRefName = jsonResponse.stringOf('Invoice.CustomerRef.name');
  const invoiceCustomerRefValue = jsonResponse.stringOf('Invoice.CustomerRef.value');
  const invoiceShipAddrCity = jsonResponse.stringOf('Invoice.ShipAddr.City');
  const invoiceShipAddrLine1 = jsonResponse.stringOf('Invoice.ShipAddr.Line1');
  const invoiceShipAddrPostalCode = jsonResponse.stringOf('Invoice.ShipAddr.PostalCode');
  const invoiceShipAddrLat = jsonResponse.stringOf('Invoice.ShipAddr.Lat');
  const invoiceShipAddrLong = jsonResponse.stringOf('Invoice.ShipAddr.Long');
  const invoiceShipAddrCountrySubDivisionCode = jsonResponse.stringOf('Invoice.ShipAddr.CountrySubDivisionCode');
  const invoiceShipAddrId = jsonResponse.stringOf('Invoice.ShipAddr.Id');
  const invoiceDueDate = jsonResponse.stringOf('Invoice.DueDate');
  const invoicePrintStatus = jsonResponse.stringOf('Invoice.PrintStatus');
  const invoiceDeposit = jsonResponse.intOf('Invoice.Deposit');
  const invoiceEmailStatus = jsonResponse.stringOf('Invoice.EmailStatus');
  const invoiceId = jsonResponse.stringOf('Invoice.Id');
  const invoiceTxnTaxDetailTotalTax = jsonResponse.intOf('Invoice.TxnTaxDetail.TotalTax');
  const invoiceMetaDataCreateTime = jsonResponse.stringOf('Invoice.MetaData.CreateTime');
  const invoiceMetaDataLastUpdatedTime = jsonResponse.stringOf('Invoice.MetaData.LastUpdatedTime');
  const time = jsonResponse.stringOf('time');
  let i = 0;
  let countI = jsonResponse.sizeOfArray('Invoice.LinkedTxn');
  while (i < countI) {
    jsonResponse.i = i;
    i++;
  }

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

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