Sample code for 30+ languages & platforms
Dart

Quickbooks Create a New Customer

See more QuickBooks Examples

Demonstrates how to create a new customer via the Quickbooks REST API.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final jsonToken = CkJsonObject();
  jsonToken.loadFile('qa_data/tokens/qb-access-token.json');

  final rest = CkRest();

  // Connect to the REST server.
  final bTls = true;
  final port = 443;
  final bAutoReconnect = true;
  rest.connect('sandbox-quickbooks.api.intuit.com', port, bTls, bAutoReconnect);

  final sbAuth = CkStringBuilder();
  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

  final jsonReq = CkJsonObject();
  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');

  final sbRequestBody = CkStringBuilder();
  jsonReq.emitSb(sbRequestBody);

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

  final sbResponseBody = CkStringBuilder();
  try {
    rest.fullRequestSb('POST', '/v3/company/<realmID>/customer', sbRequestBody, sbResponseBody);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final respStatusCode = rest.responseStatusCode;

  // Success is indicated by a 200 response status code.
  print('response status code = $respStatusCode');

  final jsonResponse = CkJsonObject();
  jsonResponse.loadSb(sbResponseBody);
  jsonResponse.emitCompact = false;
  print(jsonResponse.emit());

  if (rest.responseStatusCode != 200) {
    print('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"
  // }
  // 

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