Sample code for 30+ languages & platforms
Zig

Quickbooks Create an Invoice

See more QuickBooks Examples

Demonstrates how to create an invoice via the Quickbooks REST API.

Chilkat Zig Downloads

Zig
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:

    // {
    //   "Line": [
    //     {
    //       "DetailType": "SalesItemLineDetail",
    //       "Amount": 100.0,
    //       "SalesItemLineDetail": {
    //         "ItemRef": {
    //           "name": "Services",
    //           "value": "1"
    //         }
    //       }
    //     }
    //   ],
    //   "CustomerRef": {
    //     "value": "1"
    //   }
    // }
    //
    // 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("Line[0].DetailType", "SalesItemLineDetail") catch {};
    json_req.updateNumber("Line[0].Amount", "100.0") catch {};
    json_req.updateString("Line[0].SalesItemLineDetail.ItemRef.name", "Services") catch {};
    json_req.updateString("Line[0].SalesItemLineDetail.ItemRef.value", "1") catch {};
    json_req.updateString("CustomerRef.value", "1") 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>/invoice", 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

    // {
    //   "Invoice": {
    //     "DocNumber": "1069",
    //     "SyncToken": "0",
    //     "domain": "QBO",
    //     "Balance": 100.0,
    //     "BillAddr": {
    //       "City": "Bayshore",
    //       "Line1": "4581 Finch St.",
    //       "PostalCode": "94326",
    //       "Lat": "INVALID",
    //       "Long": "INVALID",
    //       "CountrySubDivisionCode": "CA",
    //       "Id": "2"
    //     },
    //     "TxnDate": "2015-07-24",
    //     "TotalAmt": 100.0,
    //     "CustomerRef": {
    //       "name": "Amy's Bird Sanctuary",
    //       "value": "1"
    //     },
    //     "ShipAddr": {
    //       "City": "Bayshore",
    //       "Line1": "4581 Finch St.",
    //       "PostalCode": "94326",
    //       "Lat": "INVALID",
    //       "Long": "INVALID",
    //       "CountrySubDivisionCode": "CA",
    //       "Id": "109"
    //     },
    //     "LinkedTxn": [
    //     ],
    //     "DueDate": "2015-08-23",
    //     "PrintStatus": "NeedToPrint",
    //     "Deposit": 0,
    //     "sparse": false,
    //     "EmailStatus": "NotSet",
    //     "Line": [
    //       {
    //         "LineNum": 1,
    //         "Amount": 100.0,
    //         "SalesItemLineDetail": {
    //           "TaxCodeRef": {
    //             "value": "NON"
    //           },
    //           "ItemRef": {
    //             "name": "Services",
    //             "value": "1"
    //           }
    //         },
    //         "Id": "1",
    //         "DetailType": "SalesItemLineDetail"
    //       },
    //       {
    //         "DetailType": "SubTotalLineDetail",
    //         "Amount": 100.0,
    //         "SubTotalLineDetail": {}
    //       }
    //     ],
    //     "ApplyTaxAfterDiscount": false,
    //     "CustomField": [
    //       {
    //         "DefinitionId": "1",
    //         "Type": "StringType",
    //         "Name": "Crew #"
    //       }
    //     ],
    //     "Id": "238",
    //     "TxnTaxDetail": {
    //       "TotalTax": 0
    //     },
    //     "MetaData": {
    //       "CreateTime": "2015-07-24T10:33:39-07:00",
    //       "LastUpdatedTime": "2015-07-24T10:33:39-07:00"
    //     }
    //   },
    //   "time": "2015-07-24T10:33:39.11-07:00"
    // }
    //

    var line_num: i32 = 0;
    var amount: [:0]const u8 = "";
    var sales_item_line_detail_tax_code_ref_value: [:0]const u8 = "";
    var sales_item_line_detail_item_ref_name: [:0]const u8 = "";
    var sales_item_line_detail_item_ref_value: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var detail_type: [:0]const u8 = "";
    var definition_id: [:0]const u8 = "";
    var inv_type: [:0]const u8 = "";
    var name: [:0]const u8 = "";

    _ = try json_response.stringOf(alloc, "Invoice.DocNumber");
    _ = try json_response.stringOf(alloc, "Invoice.SyncToken");
    _ = try json_response.stringOf(alloc, "Invoice.domain");
    _ = try json_response.stringOf(alloc, "Invoice.Balance");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.City");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Line1");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.PostalCode");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Lat");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Long");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.CountrySubDivisionCode");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Id");
    _ = try json_response.stringOf(alloc, "Invoice.TxnDate");
    _ = try json_response.stringOf(alloc, "Invoice.TotalAmt");
    _ = try json_response.stringOf(alloc, "Invoice.CustomerRef.name");
    _ = try json_response.stringOf(alloc, "Invoice.CustomerRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.City");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Line1");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.PostalCode");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Lat");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Long");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.CountrySubDivisionCode");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Id");
    _ = try json_response.stringOf(alloc, "Invoice.DueDate");
    _ = try json_response.stringOf(alloc, "Invoice.PrintStatus");
    _ = json_response.intOf("Invoice.Deposit");
    _ = json_response.boolOf("Invoice.sparse");
    _ = try json_response.stringOf(alloc, "Invoice.EmailStatus");
    _ = json_response.boolOf("Invoice.ApplyTaxAfterDiscount");
    _ = try json_response.stringOf(alloc, "Invoice.Id");
    _ = json_response.intOf("Invoice.TxnTaxDetail.TotalTax");
    _ = try json_response.stringOf(alloc, "Invoice.MetaData.CreateTime");
    _ = try json_response.stringOf(alloc, "Invoice.MetaData.LastUpdatedTime");
    _ = try json_response.stringOf(alloc, "time");
    var i: i32 = 0;
    var count_i: i32 = json_response.sizeOfArray("Invoice.LinkedTxn");
    while (i < count_i) {
        json_response.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("Invoice.Line");
    while (i < count_i) {
        json_response.setI(i);
        line_num = json_response.intOf("Invoice.Line[i].LineNum");
        amount = try json_response.stringOf(alloc, "Invoice.Line[i].Amount");
        sales_item_line_detail_tax_code_ref_value = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value");
        sales_item_line_detail_item_ref_name = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.ItemRef.name");
        sales_item_line_detail_item_ref_value = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.ItemRef.value");
        id = try json_response.stringOf(alloc, "Invoice.Line[i].Id");
        detail_type = try json_response.stringOf(alloc, "Invoice.Line[i].DetailType");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("Invoice.CustomField");
    while (i < count_i) {
        json_response.setI(i);
        definition_id = try json_response.stringOf(alloc, "Invoice.CustomField[i].DefinitionId");
        inv_type = try json_response.stringOf(alloc, "Invoice.CustomField[i].Type");
        name = try json_response.stringOf(alloc, "Invoice.CustomField[i].Name");
        i = i + 1;
    }
}