Sample code for 30+ languages & platforms
Objective-C

Quickbooks Create a New Customer

See more QuickBooks Examples

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

Chilkat Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <CkoRest.h>
#import <CkoStringBuilder.h>
#import <NSString.h>

BOOL success = NO;

// 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.
CkoJsonObject *jsonToken = [[CkoJsonObject alloc] init];
success = [jsonToken LoadFile: @"qa_data/tokens/qb-access-token.json"];

CkoRest *rest = [[CkoRest alloc] init];

// Connect to the REST server.
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"sandbox-quickbooks.api.intuit.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];

CkoStringBuilder *sbAuth = [[CkoStringBuilder alloc] init];
[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

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

CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[jsonReq EmitSb: sbRequestBody];

[rest AddHeader: @"Content-Type" value: @"application/json"];
[rest AddHeader: @"Accept" value: @"application/json"];
rest.AllowHeaderFolding = NO;

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestSb: @"POST" uriPath: @"/v3/company/<realmID>/customer" requestBody: sbRequestBody responseBody: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

int respStatusCode = [rest.ResponseStatusCode intValue];

// Success is indicated by a 200 response status code.
NSLog(@"%@%d",@"response status code = ",respStatusCode);

CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse LoadSb: sbResponseBody];
jsonResponse.EmitCompact = NO;
NSLog(@"%@",[jsonResponse Emit]);

if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@",@"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"
// }
// 

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