Sample code for 30+ languages & platforms
Delphi DLL

Quickbooks Create a New Customer

See more QuickBooks Examples

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

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, Rest, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
jsonToken: HCkJsonObject;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
sbAuth: HCkStringBuilder;
jsonReq: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
jsonResponse: HCkJsonObject;
CustomerDomain: PWideChar;
CustomerPrimaryEmailAddrAddress: PWideChar;
CustomerDisplayName: PWideChar;
CustomerCurrencyRefName: PWideChar;
CustomerCurrencyRefValue: PWideChar;
CustomerDefaultTaxCodeRefValue: PWideChar;
CustomerPreferredDeliveryMethod: PWideChar;
CustomerGivenName: PWideChar;
CustomerFullyQualifiedName: PWideChar;
CustomerBillWithParent: Boolean;
CustomerTitle: PWideChar;
CustomerJob: Boolean;
CustomerBalanceWithJobs: Integer;
CustomerPrimaryPhoneFreeFormNumber: PWideChar;
CustomerTaxable: Boolean;
CustomerMetaDataCreateTime: PWideChar;
CustomerMetaDataLastUpdatedTime: PWideChar;
CustomerBillAddrCity: PWideChar;
CustomerBillAddrCountry: PWideChar;
CustomerBillAddrLine1: PWideChar;
CustomerBillAddrPostalCode: PWideChar;
CustomerBillAddrCountrySubDivisionCode: PWideChar;
CustomerBillAddrId: PWideChar;
CustomerMiddleName: PWideChar;
CustomerNotes: PWideChar;
CustomerActive: Boolean;
CustomerBalance: Integer;
CustomerSyncToken: PWideChar;
CustomerSuffix: PWideChar;
CustomerCompanyName: PWideChar;
CustomerFamilyName: PWideChar;
CustomerPrintOnCheckName: PWideChar;
CustomerSparse: Boolean;
CustomerId: PWideChar;
time: PWideChar;

begin
success := False;

// 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.
jsonToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/qb-access-token.json');

rest := CkRest_Create();

// Connect to the REST server.
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'sandbox-quickbooks.api.intuit.com',port,bTls,bAutoReconnect);

sbAuth := CkStringBuilder_Create();
CkStringBuilder_Append(sbAuth,'Bearer ');
CkStringBuilder_Append(sbAuth,CkJsonObject__stringOf(jsonToken,'access_token'));
CkRest_putAuthorization(rest,CkStringBuilder__getAsString(sbAuth));

// --------------------------------------------------------------------------
// 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

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

sbRequestBody := CkStringBuilder_Create();
CkJsonObject_EmitSb(jsonReq,sbRequestBody);

CkRest_AddHeader(rest,'Content-Type','application/json');
CkRest_AddHeader(rest,'Accept','application/json');
CkRest_putAllowHeaderFolding(rest,False);

sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/v3/company/<realmID>/customer',sbRequestBody,sbResponseBody);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

respStatusCode := CkRest_getResponseStatusCode(rest);

// Success is indicated by a 200 response status code.
Memo1.Lines.Add('response status code = ' + IntToStr(respStatusCode));

jsonResponse := CkJsonObject_Create();
CkJsonObject_LoadSb(jsonResponse,sbResponseBody);
CkJsonObject_putEmitCompact(jsonResponse,False);
Memo1.Lines.Add(CkJsonObject__emit(jsonResponse));

if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

// 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"
// }
// 

CustomerDomain := CkJsonObject__stringOf(jsonResponse,'Customer.domain');
CustomerPrimaryEmailAddrAddress := CkJsonObject__stringOf(jsonResponse,'Customer.PrimaryEmailAddr.Address');
CustomerDisplayName := CkJsonObject__stringOf(jsonResponse,'Customer.DisplayName');
CustomerCurrencyRefName := CkJsonObject__stringOf(jsonResponse,'Customer.CurrencyRef.name');
CustomerCurrencyRefValue := CkJsonObject__stringOf(jsonResponse,'Customer.CurrencyRef.value');
CustomerDefaultTaxCodeRefValue := CkJsonObject__stringOf(jsonResponse,'Customer.DefaultTaxCodeRef.value');
CustomerPreferredDeliveryMethod := CkJsonObject__stringOf(jsonResponse,'Customer.PreferredDeliveryMethod');
CustomerGivenName := CkJsonObject__stringOf(jsonResponse,'Customer.GivenName');
CustomerFullyQualifiedName := CkJsonObject__stringOf(jsonResponse,'Customer.FullyQualifiedName');
CustomerBillWithParent := CkJsonObject_BoolOf(jsonResponse,'Customer.BillWithParent');
CustomerTitle := CkJsonObject__stringOf(jsonResponse,'Customer.Title');
CustomerJob := CkJsonObject_BoolOf(jsonResponse,'Customer.Job');
CustomerBalanceWithJobs := CkJsonObject_IntOf(jsonResponse,'Customer.BalanceWithJobs');
CustomerPrimaryPhoneFreeFormNumber := CkJsonObject__stringOf(jsonResponse,'Customer.PrimaryPhone.FreeFormNumber');
CustomerTaxable := CkJsonObject_BoolOf(jsonResponse,'Customer.Taxable');
CustomerMetaDataCreateTime := CkJsonObject__stringOf(jsonResponse,'Customer.MetaData.CreateTime');
CustomerMetaDataLastUpdatedTime := CkJsonObject__stringOf(jsonResponse,'Customer.MetaData.LastUpdatedTime');
CustomerBillAddrCity := CkJsonObject__stringOf(jsonResponse,'Customer.BillAddr.City');
CustomerBillAddrCountry := CkJsonObject__stringOf(jsonResponse,'Customer.BillAddr.Country');
CustomerBillAddrLine1 := CkJsonObject__stringOf(jsonResponse,'Customer.BillAddr.Line1');
CustomerBillAddrPostalCode := CkJsonObject__stringOf(jsonResponse,'Customer.BillAddr.PostalCode');
CustomerBillAddrCountrySubDivisionCode := CkJsonObject__stringOf(jsonResponse,'Customer.BillAddr.CountrySubDivisionCode');
CustomerBillAddrId := CkJsonObject__stringOf(jsonResponse,'Customer.BillAddr.Id');
CustomerMiddleName := CkJsonObject__stringOf(jsonResponse,'Customer.MiddleName');
CustomerNotes := CkJsonObject__stringOf(jsonResponse,'Customer.Notes');
CustomerActive := CkJsonObject_BoolOf(jsonResponse,'Customer.Active');
CustomerBalance := CkJsonObject_IntOf(jsonResponse,'Customer.Balance');
CustomerSyncToken := CkJsonObject__stringOf(jsonResponse,'Customer.SyncToken');
CustomerSuffix := CkJsonObject__stringOf(jsonResponse,'Customer.Suffix');
CustomerCompanyName := CkJsonObject__stringOf(jsonResponse,'Customer.CompanyName');
CustomerFamilyName := CkJsonObject__stringOf(jsonResponse,'Customer.FamilyName');
CustomerPrintOnCheckName := CkJsonObject__stringOf(jsonResponse,'Customer.PrintOnCheckName');
CustomerSparse := CkJsonObject_BoolOf(jsonResponse,'Customer.sparse');
CustomerId := CkJsonObject__stringOf(jsonResponse,'Customer.Id');
time := CkJsonObject__stringOf(jsonResponse,'time');

CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkJsonObject_Dispose(jsonReq);
CkStringBuilder_Dispose(sbRequestBody);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jsonResponse);

end;