Sample code for 30+ languages & platforms
React Native

Quickbooks Create a New Customer

See more QuickBooks Examples

Demonstrates how to create a new customer 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:

  // {
  //   "FullyQualifiedName": "King Groceries",
  //   "PrimaryEmailAddr": {
  //     "Address": "jdrew@myemail.com"
  //   },
  //   "DisplayName": "King's Groceries",
  //   "Suffix": "Jr",
  //   "Title": "Mr",
  //   "MiddleName": "B",
  //   "Notes": "Here are other details.",
  //   "FamilyName": "King",
  //   "PrimaryPhone": {
  //     "FreeFormNumber": "(555) 555-5555"
  //   },
  //   "CompanyName": "King Groceries",
  //   "BillAddr": {
  //     "CountrySubDivisionCode": "CA",
  //     "City": "Mountain View",
  //     "PostalCode": "94042",
  //     "Line1": "123 Main Street",
  //     "Country": "USA"
  //   },
  //   "GivenName": "James"
  // }
  // 
  // Use the this online tool to generate the code from sample JSON: 
  // Generate Code to Create JSON

  const jsonReq = new JsonObject();
  jsonReq.updateString('FullyQualifiedName', 'King Groceries');
  jsonReq.updateString('PrimaryEmailAddr.Address', 'jdrew@myemail.com');
  jsonReq.updateString('DisplayName', 'King\'s Groceries');
  jsonReq.updateString('Suffix', 'Jr');
  jsonReq.updateString('Title', 'Mr');
  jsonReq.updateString('MiddleName', 'B');
  jsonReq.updateString('Notes', 'Here are other details.');
  jsonReq.updateString('FamilyName', 'King');
  jsonReq.updateString('PrimaryPhone.FreeFormNumber', '(555) 555-5555');
  jsonReq.updateString('CompanyName', 'King Groceries');
  jsonReq.updateString('BillAddr.CountrySubDivisionCode', 'CA');
  jsonReq.updateString('BillAddr.City', 'Mountain View');
  jsonReq.updateString('BillAddr.PostalCode', '94042');
  jsonReq.updateString('BillAddr.Line1', '123 Main Street');
  jsonReq.updateString('BillAddr.Country', 'USA');
  jsonReq.updateString('GivenName', 'James');

  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>/customer', 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

  // {
  //   "Customer": {
  //     "domain": "QBO",
  //     "PrimaryEmailAddr": {
  //       "Address": "jdrew@myemail.com"
  //     },
  //     "DisplayName": "King's Groceries",
  //     "CurrencyRef": {
  //       "name": "United States Dollar",
  //       "value": "USD"
  //     },
  //     "DefaultTaxCodeRef": {
  //       "value": "2"
  //     },
  //     "PreferredDeliveryMethod": "Print",
  //     "GivenName": "James",
  //     "FullyQualifiedName": "King's Groceries",
  //     "BillWithParent": false,
  //     "Title": "Mr",
  //     "Job": false,
  //     "BalanceWithJobs": 0,
  //     "PrimaryPhone": {
  //       "FreeFormNumber": "(555) 555-5555"
  //     },
  //     "Taxable": true,
  //     "MetaData": {
  //       "CreateTime": "2015-07-23T10:58:12-07:00",
  //       "LastUpdatedTime": "2015-07-23T10:58:12-07:00"
  //     },
  //     "BillAddr": {
  //       "City": "Mountain View",
  //       "Country": "USA",
  //       "Line1": "123 Main Street",
  //       "PostalCode": "94042",
  //       "CountrySubDivisionCode": "CA",
  //       "Id": "112"
  //     },
  //     "MiddleName": "B",
  //     "Notes": "Here are other details.",
  //     "Active": true,
  //     "Balance": 0,
  //     "SyncToken": "0",
  //     "Suffix": "Jr",
  //     "CompanyName": "King Groceries",
  //     "FamilyName": "King",
  //     "PrintOnCheckName": "King Groceries",
  //     "sparse": false,
  //     "Id": "67"
  //   },
  //   "time": "2015-07-23T10:58:12.099-07:00"
  // }
  // 

  const customerDomain = jsonResponse.stringOf('Customer.domain');
  const customerPrimaryEmailAddrAddress = jsonResponse.stringOf('Customer.PrimaryEmailAddr.Address');
  const customerDisplayName = jsonResponse.stringOf('Customer.DisplayName');
  const customerCurrencyRefName = jsonResponse.stringOf('Customer.CurrencyRef.name');
  const customerCurrencyRefValue = jsonResponse.stringOf('Customer.CurrencyRef.value');
  const customerDefaultTaxCodeRefValue = jsonResponse.stringOf('Customer.DefaultTaxCodeRef.value');
  const customerPreferredDeliveryMethod = jsonResponse.stringOf('Customer.PreferredDeliveryMethod');
  const customerGivenName = jsonResponse.stringOf('Customer.GivenName');
  const customerFullyQualifiedName = jsonResponse.stringOf('Customer.FullyQualifiedName');
  const customerTitle = jsonResponse.stringOf('Customer.Title');
  const customerBalanceWithJobs = jsonResponse.intOf('Customer.BalanceWithJobs');
  const customerPrimaryPhoneFreeFormNumber = jsonResponse.stringOf('Customer.PrimaryPhone.FreeFormNumber');
  const customerMetaDataCreateTime = jsonResponse.stringOf('Customer.MetaData.CreateTime');
  const customerMetaDataLastUpdatedTime = jsonResponse.stringOf('Customer.MetaData.LastUpdatedTime');
  const customerBillAddrCity = jsonResponse.stringOf('Customer.BillAddr.City');
  const customerBillAddrCountry = jsonResponse.stringOf('Customer.BillAddr.Country');
  const customerBillAddrLine1 = jsonResponse.stringOf('Customer.BillAddr.Line1');
  const customerBillAddrPostalCode = jsonResponse.stringOf('Customer.BillAddr.PostalCode');
  const customerBillAddrCountrySubDivisionCode = jsonResponse.stringOf('Customer.BillAddr.CountrySubDivisionCode');
  const customerBillAddrId = jsonResponse.stringOf('Customer.BillAddr.Id');
  const customerMiddleName = jsonResponse.stringOf('Customer.MiddleName');
  const customerNotes = jsonResponse.stringOf('Customer.Notes');
  const customerBalance = jsonResponse.intOf('Customer.Balance');
  const customerSyncToken = jsonResponse.stringOf('Customer.SyncToken');
  const customerSuffix = jsonResponse.stringOf('Customer.Suffix');
  const customerCompanyName = jsonResponse.stringOf('Customer.CompanyName');
  const customerFamilyName = jsonResponse.stringOf('Customer.FamilyName');
  const customerPrintOnCheckName = jsonResponse.stringOf('Customer.PrintOnCheckName');
  const customerId = jsonResponse.stringOf('Customer.Id');
  const time = jsonResponse.stringOf('time');
}