Zig
Zig
Quickbooks Create a New Customer
See more QuickBooks Examples
Demonstrates how to create a new customer via the Quickbooks REST API.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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 json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
try json_token.loadFile("qa_data/tokens/qb-access-token.json");
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the REST server.
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
try rest.connect("sandbox-quickbooks.api.intuit.com", port, b_tls, b_auto_reconnect);
const sb_auth = try chilkat.StringBuilder.init();
defer sb_auth.deinit();
sb_auth.append("Bearer ") catch {};
sb_auth.append(try json_token.stringOf(alloc, "access_token")) catch {};
rest.setAuthorization(try sb_auth.getAsString(alloc));
// --------------------------------------------------------------------------
// 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 json_req = try chilkat.JsonObject.init();
defer json_req.deinit();
json_req.updateString("FullyQualifiedName", "King Groceries") catch {};
json_req.updateString("PrimaryEmailAddr.Address", "jdrew@myemail.com") catch {};
json_req.updateString("DisplayName", "King's Groceries") catch {};
json_req.updateString("Suffix", "Jr") catch {};
json_req.updateString("Title", "Mr") catch {};
json_req.updateString("MiddleName", "B") catch {};
json_req.updateString("Notes", "Here are other details.") catch {};
json_req.updateString("FamilyName", "King") catch {};
json_req.updateString("PrimaryPhone.FreeFormNumber", "(555) 555-5555") catch {};
json_req.updateString("CompanyName", "King Groceries") catch {};
json_req.updateString("BillAddr.CountrySubDivisionCode", "CA") catch {};
json_req.updateString("BillAddr.City", "Mountain View") catch {};
json_req.updateString("BillAddr.PostalCode", "94042") catch {};
json_req.updateString("BillAddr.Line1", "123 Main Street") catch {};
json_req.updateString("BillAddr.Country", "USA") catch {};
json_req.updateString("GivenName", "James") catch {};
const sb_request_body = try chilkat.StringBuilder.init();
defer sb_request_body.deinit();
json_req.emitSb(sb_request_body) catch {};
rest.addHeader("Content-Type", "application/json") catch {};
rest.addHeader("Accept", "application/json") catch {};
rest.setAllowHeaderFolding(false);
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestSb("POST", "/v3/company/<realmID>/customer", sb_request_body, sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const resp_status_code = rest.getResponseStatusCode();
// Success is indicated by a 200 response status code.
std.debug.print("response status code = {d}\n", .{resp_status_code});
const json_response = try chilkat.JsonObject.init();
defer json_response.deinit();
json_response.loadSb(sb_response_body) catch {};
json_response.setEmitCompact(false);
std.debug.print("{s}\n", .{try json_response.emit(alloc)});
if (rest.getResponseStatusCode() != 200) {
std.debug.print("Failed.\n", .{});
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"
// }
//
_ = try json_response.stringOf(alloc, "Customer.domain");
_ = try json_response.stringOf(alloc, "Customer.PrimaryEmailAddr.Address");
_ = try json_response.stringOf(alloc, "Customer.DisplayName");
_ = try json_response.stringOf(alloc, "Customer.CurrencyRef.name");
_ = try json_response.stringOf(alloc, "Customer.CurrencyRef.value");
_ = try json_response.stringOf(alloc, "Customer.DefaultTaxCodeRef.value");
_ = try json_response.stringOf(alloc, "Customer.PreferredDeliveryMethod");
_ = try json_response.stringOf(alloc, "Customer.GivenName");
_ = try json_response.stringOf(alloc, "Customer.FullyQualifiedName");
_ = json_response.boolOf("Customer.BillWithParent");
_ = try json_response.stringOf(alloc, "Customer.Title");
_ = json_response.boolOf("Customer.Job");
_ = json_response.intOf("Customer.BalanceWithJobs");
_ = try json_response.stringOf(alloc, "Customer.PrimaryPhone.FreeFormNumber");
_ = json_response.boolOf("Customer.Taxable");
_ = try json_response.stringOf(alloc, "Customer.MetaData.CreateTime");
_ = try json_response.stringOf(alloc, "Customer.MetaData.LastUpdatedTime");
_ = try json_response.stringOf(alloc, "Customer.BillAddr.City");
_ = try json_response.stringOf(alloc, "Customer.BillAddr.Country");
_ = try json_response.stringOf(alloc, "Customer.BillAddr.Line1");
_ = try json_response.stringOf(alloc, "Customer.BillAddr.PostalCode");
_ = try json_response.stringOf(alloc, "Customer.BillAddr.CountrySubDivisionCode");
_ = try json_response.stringOf(alloc, "Customer.BillAddr.Id");
_ = try json_response.stringOf(alloc, "Customer.MiddleName");
_ = try json_response.stringOf(alloc, "Customer.Notes");
_ = json_response.boolOf("Customer.Active");
_ = json_response.intOf("Customer.Balance");
_ = try json_response.stringOf(alloc, "Customer.SyncToken");
_ = try json_response.stringOf(alloc, "Customer.Suffix");
_ = try json_response.stringOf(alloc, "Customer.CompanyName");
_ = try json_response.stringOf(alloc, "Customer.FamilyName");
_ = try json_response.stringOf(alloc, "Customer.PrintOnCheckName");
_ = json_response.boolOf("Customer.sparse");
_ = try json_response.stringOf(alloc, "Customer.Id");
_ = try json_response.stringOf(alloc, "time");
}