Sample code for 30+ languages & platforms
Zig

Quickbooks Send an Invoice

See more QuickBooks Examples

Demonstrates how to send an invoice using 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.
    // --------------------------------------------------------------------------

    // Technically, the POST has an empty request body, but the Quickbooks documentation indicates that
    // the Content-Type header should be set to "application/octet-stream", which really makes no sense
    // because there is not content. (How can no content have a type???)
    rest.addHeader("Content-Type", "application/octet-stream") catch {};
    rest.setAllowHeaderFolding(false);

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestNoBodySb("POST", "/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>", 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": {
    //     "TxnDate": "2013-03-14",
    //     "domain": "QBO",
    //     "CurrencyRef": {
    //       "name": "United States Dollar",
    //       "value": "USD"
    //     },
    //     "ShipDate": "2013-03-01",
    //     "TrackingNum": "123456789",
    //     "ClassRef": {
    //       "name": "Class 1",
    //       "value": "200900000000000003901"
    //     },
    //     "PrintStatus": "NeedToPrint",
    //     "SalesTermRef": {
    //       "value": "4"
    //     },
    //     "DeliveryInfo": {
    //       "DeliveryType": "Email",
    //       "DeliveryTime": "2014-12-17T11:50:52-08:00"
    //     },
    //     "TotalAmt": 52.0,
    //     "Line": [
    //       {
    //         "Description": "Sample invoice create request",
    //         "DetailType": "SalesItemLineDetail",
    //         "SalesItemLineDetail": {
    //           "TaxCodeRef": {
    //             "value": "TAX"
    //           },
    //           "Qty": 1,
    //           "UnitPrice": 50,
    //           "ServiceDate": "2013-03-04",
    //           "ItemRef": {
    //             "name": "Hours",
    //             "value": "2"
    //           }
    //         },
    //         "LineNum": 1,
    //         "Amount": 50.0,
    //         "Id": "1"
    //       },
    //       {
    //         "DetailType": "SubTotalLineDetail",
    //         "Amount": 50.0,
    //         "SubTotalLineDetail": {}
    //       },
    //       {
    //         "DetailType": "DiscountLineDetail",
    //         "Amount": 5.0,
    //         "DiscountLineDetail": {
    //           "DiscountAccountRef": {
    //             "name": "Discounts given",
    //             "value": "30"
    //           },
    //           "PercentBased": true,
    //           "DiscountPercent": 10
    //         }
    //       },
    //       {
    //         "DetailType": "SalesItemLineDetail",
    //         "Amount": 2.0,
    //         "SalesItemLineDetail": {
    //           "ItemRef": {
    //             "value": "SHIPPING_ITEM_ID"
    //           }
    //         }
    //       }
    //     ],
    //     "DueDate": "2013-05-13",
    //     "MetaData": {
    //       "CreateTime": "2013-03-14T01:42:16-07:00",
    //       "LastUpdatedTime": "2014-12-17T11:50:58-08:00"
    //     },
    //     "DocNumber": "Sample_Inv#2",
    //     "PrivateNote": "Summary for sample invoice",
    //     "sparse": false,
    //     "DepositToAccountRef": {
    //       "name": "Undeposited Funds",
    //       "value": "4"
    //     },
    //     "CustomerMemo": {
    //       "value": "This is the customer message"
    //     },
    //     "EmailStatus": "EmailSent",
    //     "Deposit": 12.0,
    //     "Balance": 40.0,
    //     "CustomerRef": {
    //       "name": "Mr V3 Service Customer Jr2",
    //       "value": "15"
    //     },
    //     "TxnTaxDetail": {
    //       "TxnTaxCodeRef": {
    //         "value": "5"
    //       },
    //       "TotalTax": 5.0,
    //       "TaxLine": [
    //         {
    //           "DetailType": "TaxLineDetail",
    //           "Amount": 5.0,
    //           "TaxLineDetail": {
    //             "NetAmountTaxable": 50.0,
    //             "TaxPercent": 10,
    //             "TaxRateRef": {
    //               "value": "2"
    //             },
    //             "PercentBased": true
    //           }
    //         }
    //       ]
    //     },
    //     "SyncToken": "0",
    //     "BillEmail": {
    //       "Address": "test@intuit.com"
    //     },
    //     "ShipAddr": {
    //       "City": "San Jose",
    //       "Country": "USA",
    //       "Line5": "Cube 999",
    //       "Line4": "Dept 12",
    //       "Line3": "123 street",
    //       "Line2": "Building 1",
    //       "Line1": "Intuit",
    //       "PostalCode": "95123",
    //       "Lat": "37.2374847",
    //       "Long": "-121.8277925",
    //       "CountrySubDivisionCode": "CA",
    //       "Id": "36"
    //     },
    //     "DepartmentRef": {
    //       "name": "Mountain View",
    //       "value": "1"
    //     },
    //     "ShipMethodRef": {
    //       "name": "UPS",
    //       "value": "UPS"
    //     },
    //     "BillAddr": {
    //       "City": "Mountain View",
    //       "Country": "USA",
    //       "Line5": "Cube 999",
    //       "Line4": "Dept 12",
    //       "Line3": "123 street",
    //       "Line2": "Building 1",
    //       "Line1": "Google",
    //       "PostalCode": "95123",
    //       "Lat": "37.2374847",
    //       "Long": "-121.8277925",
    //       "CountrySubDivisionCode": "CA",
    //       "Id": "35"
    //     },
    //     "ApplyTaxAfterDiscount": false,
    //     "CustomField": [
    //       {
    //         "StringValue": "Custom1",
    //         "Type": "StringType",
    //         "Name": "Custom 1"
    //       },
    //       {
    //         "StringValue": "Custom2",
    //         "Type": "StringType",
    //         "Name": "Custom 2"
    //       },
    //       {
    //         "StringValue": "Custom3",
    //         "Type": "StringType",
    //         "Name": "Custom 3"
    //       }
    //     ],
    //     "Id": "96"
    //   },
    //   "time": "2013-03-14T13:32:04.895-07:00"
    // }
    //

    var description: [:0]const u8 = "";
    var detail_type: [:0]const u8 = "";
    var sales_item_line_detail_tax_code_ref_value: [:0]const u8 = "";
    var sales_item_line_detail_qty: i32 = 0;
    var sales_item_line_detail_unit_price: i32 = 0;
    var sales_item_line_detail_service_date: [: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 line_num: i32 = 0;
    var amount: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var discount_line_detail_discount_account_ref_name: [:0]const u8 = "";
    var discount_line_detail_discount_account_ref_value: [:0]const u8 = "";
    var discount_line_detail_percent_based: bool = false;
    var discount_line_detail_discount_percent: i32 = 0;
    var tax_line_detail_net_amount_taxable: [:0]const u8 = "";
    var tax_line_detail_tax_percent: i32 = 0;
    var tax_line_detail_tax_rate_ref_value: [:0]const u8 = "";
    var tax_line_detail_percent_based: bool = false;
    var string_value: [:0]const u8 = "";
    var type_: [:0]const u8 = "";
    var name: [:0]const u8 = "";

    _ = try json_response.stringOf(alloc, "Invoice.TxnDate");
    _ = try json_response.stringOf(alloc, "Invoice.domain");
    _ = try json_response.stringOf(alloc, "Invoice.CurrencyRef.name");
    _ = try json_response.stringOf(alloc, "Invoice.CurrencyRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.ShipDate");
    _ = try json_response.stringOf(alloc, "Invoice.TrackingNum");
    _ = try json_response.stringOf(alloc, "Invoice.ClassRef.name");
    _ = try json_response.stringOf(alloc, "Invoice.ClassRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.PrintStatus");
    _ = try json_response.stringOf(alloc, "Invoice.SalesTermRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.DeliveryInfo.DeliveryType");
    _ = try json_response.stringOf(alloc, "Invoice.DeliveryInfo.DeliveryTime");
    _ = try json_response.stringOf(alloc, "Invoice.TotalAmt");
    _ = try json_response.stringOf(alloc, "Invoice.DueDate");
    _ = try json_response.stringOf(alloc, "Invoice.MetaData.CreateTime");
    _ = try json_response.stringOf(alloc, "Invoice.MetaData.LastUpdatedTime");
    _ = try json_response.stringOf(alloc, "Invoice.DocNumber");
    _ = try json_response.stringOf(alloc, "Invoice.PrivateNote");
    _ = json_response.boolOf("Invoice.sparse");
    _ = try json_response.stringOf(alloc, "Invoice.DepositToAccountRef.name");
    _ = try json_response.stringOf(alloc, "Invoice.DepositToAccountRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.CustomerMemo.value");
    _ = try json_response.stringOf(alloc, "Invoice.EmailStatus");
    _ = try json_response.stringOf(alloc, "Invoice.Deposit");
    _ = try json_response.stringOf(alloc, "Invoice.Balance");
    _ = try json_response.stringOf(alloc, "Invoice.CustomerRef.name");
    _ = try json_response.stringOf(alloc, "Invoice.CustomerRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.TxnTaxDetail.TxnTaxCodeRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.TxnTaxDetail.TotalTax");
    _ = try json_response.stringOf(alloc, "Invoice.SyncToken");
    _ = try json_response.stringOf(alloc, "Invoice.BillEmail.Address");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.City");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Country");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Line5");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Line4");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Line3");
    _ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Line2");
    _ = 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.DepartmentRef.name");
    _ = try json_response.stringOf(alloc, "Invoice.DepartmentRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.ShipMethodRef.name");
    _ = try json_response.stringOf(alloc, "Invoice.ShipMethodRef.value");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.City");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Country");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Line5");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Line4");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Line3");
    _ = try json_response.stringOf(alloc, "Invoice.BillAddr.Line2");
    _ = 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");
    _ = json_response.boolOf("Invoice.ApplyTaxAfterDiscount");
    _ = try json_response.stringOf(alloc, "Invoice.Id");
    _ = try json_response.stringOf(alloc, "time");
    var i: i32 = 0;
    var count_i: i32 = json_response.sizeOfArray("Invoice.Line");
    while (i < count_i) {
        json_response.setI(i);
        description = try json_response.stringOf(alloc, "Invoice.Line[i].Description");
        detail_type = try json_response.stringOf(alloc, "Invoice.Line[i].DetailType");
        sales_item_line_detail_tax_code_ref_value = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value");
        sales_item_line_detail_qty = json_response.intOf("Invoice.Line[i].SalesItemLineDetail.Qty");
        sales_item_line_detail_unit_price = json_response.intOf("Invoice.Line[i].SalesItemLineDetail.UnitPrice");
        sales_item_line_detail_service_date = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.ServiceDate");
        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");
        line_num = json_response.intOf("Invoice.Line[i].LineNum");
        amount = try json_response.stringOf(alloc, "Invoice.Line[i].Amount");
        id = try json_response.stringOf(alloc, "Invoice.Line[i].Id");
        discount_line_detail_discount_account_ref_name = try json_response.stringOf(alloc, "Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name");
        discount_line_detail_discount_account_ref_value = try json_response.stringOf(alloc, "Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value");
        discount_line_detail_percent_based = json_response.boolOf("Invoice.Line[i].DiscountLineDetail.PercentBased");
        discount_line_detail_discount_percent = json_response.intOf("Invoice.Line[i].DiscountLineDetail.DiscountPercent");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("Invoice.TxnTaxDetail.TaxLine");
    while (i < count_i) {
        json_response.setI(i);
        detail_type = try json_response.stringOf(alloc, "Invoice.TxnTaxDetail.TaxLine[i].DetailType");
        amount = try json_response.stringOf(alloc, "Invoice.TxnTaxDetail.TaxLine[i].Amount");
        tax_line_detail_net_amount_taxable = try json_response.stringOf(alloc, "Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable");
        tax_line_detail_tax_percent = json_response.intOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent");
        tax_line_detail_tax_rate_ref_value = try json_response.stringOf(alloc, "Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value");
        tax_line_detail_percent_based = json_response.boolOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased");
        i = i + 1;
    }

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