Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Xero Get Invoices

See more Xero Examples

Get Xero company invoices.

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.

    const http = try chilkat.Http.init();
    defer http.deinit();

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/xero-access-token.json") catch {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    };

    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    // Replace the value here with an actual tenant ID obtained from this example:
    // Get Xero Tenant IDs
    http.setRequestHeader("Xero-tenant-id", "83299b9e-5747-4a14-a18a-a6c94f824eb7");

    http.setAccept("application/json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", "https://api.xero.com/api.xro/2.0/Invoices", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(try resp.getBodyStr(alloc)) catch {};
    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    if (resp.getStatusCode() != 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

    // {
    //   "Id": "bda1f62f-0d63-4178-8d54-e91fd226987a",
    //   "Status": "OK",
    //   "ProviderName": "Chilkat2222",
    //   "DateTimeUTC": "\/Date(1587210234493)\/",
    //   "Invoices": [
    //     {
    //       "Type": "ACCPAY",
    //       "InvoiceID": "0032f627-3156-4d30-9b1c-4d3b994dc921",
    //       "InvoiceNumber": "9871",
    //       "Reference": "",
    //       "Payments": [
    //         {
    //           "PaymentID": "22974891-3689-4694-9ee7-fd2ba917af55",
    //           "Date": "\/Date(1579737600000+0000)\/",
    //           "Amount": 148.50,
    //           "Reference": "Chq 409",
    //           "HasAccount": false,
    //           "HasValidationErrors": false
    //         }
    //       ],
    //       "CreditNotes": [
    //       ],
    //       "Prepayments": [
    //       ],
    //       "Overpayments": [
    //       ],
    //       "AmountDue": 0.00,
    //       "AmountPaid": 148.50,
    //       "AmountCredited": 0.00,
    //       "IsDiscounted": false,
    //       "HasAttachments": false,
    //       "HasErrors": false,
    //       "Contact": {
    //         "ContactID": "d6a384fb-f46f-41a3-8ac7-b7bc9e0b5efa",
    //         "Name": "Melrose Parking",
    //         "Addresses": [
    //         ],
    //         "Phones": [
    //         ],
    //         "ContactGroups": [
    //         ],
    //         "ContactPersons": [
    //         ],
    //         "HasValidationErrors": false
    //       },
    //       "DateString": "2020-01-15T00:00:00",
    //       "Date": "\/Date(1579046400000+0000)\/",
    //       "DueDateString": "2020-01-24T00:00:00",
    //       "DueDate": "\/Date(1579824000000+0000)\/",
    //       "Status": "PAID",
    //       "LineAmountTypes": "Exclusive",
    //       "LineItems": [
    //       ],
    //       "SubTotal": 135.00,
    //       "TotalTax": 13.50,
    //       "Total": 148.50,
    //       "UpdatedDateUTC": "\/Date(1221560931500+0000)\/",
    //       "CurrencyCode": "AUD",
    //       "FullyPaidOnDate": "\/Date(1579737600000+0000)\/"
    //     },
    //     {
    //       "Type": "ACCPAY",
    //       "InvoiceID": "673dd7cc-beb7-4697-83d4-0c47cb400cc2",
    //       "InvoiceNumber": "",
    //       "Reference": "",
    //       "Payments": [
    //         {
    //           "PaymentID": "4d06f609-5200-4364-9c8b-d4379a945252",
    //           "Date": "\/Date(1580688000000+0000)\/",
    //           "Amount": 974.60,
    //           "Reference": "DD # 96013",
    //           "HasAccount": false,
    //           "HasValidationErrors": false
    //         }
    //       ],
    //       "CreditNotes": [
    //         {
    //           "CreditNoteID": "7df8949c-b71f-40c0-bbcf-39f2f450f286",
    //           "CreditNoteNumber": "03391",
    //           "ID": "7df8949c-b71f-40c0-bbcf-39f2f450f286",
    //           "HasErrors": false,
    //           "AppliedAmount": 218.90,
    //           "DateString": "2020-01-29T00:00:00",
    //           "Date": "\/Date(1580256000000+0000)\/",
    //           "LineItems": [
    //           ],
    //           "Total": 218.90
    //         }
    //       ],
    //       "Prepayments": [
    //       ],
    //       "Overpayments": [
    //       ],
    //       "AmountDue": 0.00,
    //       "AmountPaid": 974.60,
    //       "AmountCredited": 218.90,
    //       "IsDiscounted": false,
    //       "HasAttachments": false,
    //       "HasErrors": false,
    //       "Contact": {
    //         "ContactID": "d0cd2c4f-18a0-4f7c-a32a-2db00f29d298",
    //         "Name": "PC Complete",
    //         "Addresses": [
    //         ],
    //         "Phones": [
    //         ],
    //         "ContactGroups": [
    //         ],
    //         "ContactPersons": [
    //         ],
    //         "HasValidationErrors": false
    //       },
    //       "DateString": "2020-01-28T00:00:00",
    //       "Date": "\/Date(1580169600000+0000)\/",
    //       "DueDateString": "2020-02-04T00:00:00",
    //       "DueDate": "\/Date(1580774400000+0000)\/",
    //       "Status": "PAID",
    //       "LineAmountTypes": "Exclusive",
    //       "LineItems": [
    //       ],
    //       "SubTotal": 1085.00,
    //       "TotalTax": 108.50,
    //       "Total": 1193.50,
    //       "UpdatedDateUTC": "\/Date(1221561913790+0000)\/",
    //       "CurrencyCode": "AUD",
    //       "FullyPaidOnDate": "\/Date(1580688000000+0000)\/"
    //     },
    //     {
    //       "Type": "ACCPAY",
    //       "InvoiceID": "c12aff7e-12bf-4185-8702-460929f19674",
    //       "InvoiceNumber": "",
    //       "Reference": "",
    //       "Payments": [
    //       ],
    //       "CreditNotes": [
    //       ],
    //       "Prepayments": [
    //       ],
    //       "Overpayments": [
    //       ],
    //       "AmountDue": 2166.99,
    //       "AmountPaid": 0.00,
    //       "AmountCredited": 0.00,
    //       "CurrencyRate": 1.000000,
    //       "IsDiscounted": false,
    //       "HasAttachments": false,
    //       "HasErrors": false,
    //       "Contact": {
    //         "ContactID": "d0cd2c4f-18a0-4f7c-a32a-2db00f29d298",
    //         "Name": "PC Complete",
    //         "Addresses": [
    //         ],
    //         "Phones": [
    //         ],
    //         "ContactGroups": [
    //         ],
    //         "ContactPersons": [
    //         ],
    //         "HasValidationErrors": false
    //       },
    //       "DateString": "2020-04-10T00:00:00",
    //       "Date": "\/Date(1586476800000+0000)\/",
    //       "DueDateString": "2020-05-05T00:00:00",
    //       "DueDate": "\/Date(1588636800000+0000)\/",
    //       "Status": "AUTHORISED",
    //       "LineAmountTypes": "Exclusive",
    //       "LineItems": [
    //       ],
    //       "SubTotal": 1969.99,
    //       "TotalTax": 197.00,
    //       "Total": 2166.99,
    //       "UpdatedDateUTC": "\/Date(1497965301980+0000)\/",
    //       "CurrencyCode": "AUD"
    //     }
    //   ]
    // }
    //

    var type_: [:0]const u8 = "";
    var invoice_id: [:0]const u8 = "";
    var invoice_number: [:0]const u8 = "";
    var reference: [:0]const u8 = "";
    var amount_due: [:0]const u8 = "";
    var amount_paid: [:0]const u8 = "";
    var amount_credited: [:0]const u8 = "";
    var is_discounted: bool = false;
    var has_attachments: bool = false;
    var has_errors: bool = false;
    var contact_contact_id: [:0]const u8 = "";
    var contact_name: [:0]const u8 = "";
    var contact_has_validation_errors: bool = false;
    var date_string: [:0]const u8 = "";
    var date: [:0]const u8 = "";
    var due_date_string: [:0]const u8 = "";
    var due_date: [:0]const u8 = "";
    var line_amount_types: [:0]const u8 = "";
    var sub_total: [:0]const u8 = "";
    var total_tax: [:0]const u8 = "";
    var total: [:0]const u8 = "";
    var updated_date_utc: [:0]const u8 = "";
    var currency_code: [:0]const u8 = "";
    var fully_paid_on_date: [:0]const u8 = "";
    var currency_rate: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var payment_id: [:0]const u8 = "";
    var amount: [:0]const u8 = "";
    var has_account: bool = false;
    var has_validation_errors: bool = false;
    var credit_note_id: [:0]const u8 = "";
    var credit_note_number: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var applied_amount: [:0]const u8 = "";
    var k: i32 = 0;
    var count_k: i32 = 0;

    id = try json_response.stringOf(alloc, "Id");
    var status: [:0]const u8 = try json_response.stringOf(alloc, "Status");
    _ = try json_response.stringOf(alloc, "ProviderName");
    _ = try json_response.stringOf(alloc, "DateTimeUTC");
    var i: i32 = 0;
    const count_i = json_response.sizeOfArray("Invoices");
    while (i < count_i) {
        json_response.setI(i);
        type_ = try json_response.stringOf(alloc, "Invoices[i].Type");
        invoice_id = try json_response.stringOf(alloc, "Invoices[i].InvoiceID");
        invoice_number = try json_response.stringOf(alloc, "Invoices[i].InvoiceNumber");
        reference = try json_response.stringOf(alloc, "Invoices[i].Reference");
        amount_due = try json_response.stringOf(alloc, "Invoices[i].AmountDue");
        amount_paid = try json_response.stringOf(alloc, "Invoices[i].AmountPaid");
        amount_credited = try json_response.stringOf(alloc, "Invoices[i].AmountCredited");
        is_discounted = json_response.boolOf("Invoices[i].IsDiscounted");
        has_attachments = json_response.boolOf("Invoices[i].HasAttachments");
        has_errors = json_response.boolOf("Invoices[i].HasErrors");
        contact_contact_id = try json_response.stringOf(alloc, "Invoices[i].Contact.ContactID");
        contact_name = try json_response.stringOf(alloc, "Invoices[i].Contact.Name");
        contact_has_validation_errors = json_response.boolOf("Invoices[i].Contact.HasValidationErrors");
        date_string = try json_response.stringOf(alloc, "Invoices[i].DateString");
        date = try json_response.stringOf(alloc, "Invoices[i].Date");
        due_date_string = try json_response.stringOf(alloc, "Invoices[i].DueDateString");
        due_date = try json_response.stringOf(alloc, "Invoices[i].DueDate");
        status = try json_response.stringOf(alloc, "Invoices[i].Status");
        line_amount_types = try json_response.stringOf(alloc, "Invoices[i].LineAmountTypes");
        sub_total = try json_response.stringOf(alloc, "Invoices[i].SubTotal");
        total_tax = try json_response.stringOf(alloc, "Invoices[i].TotalTax");
        total = try json_response.stringOf(alloc, "Invoices[i].Total");
        updated_date_utc = try json_response.stringOf(alloc, "Invoices[i].UpdatedDateUTC");
        currency_code = try json_response.stringOf(alloc, "Invoices[i].CurrencyCode");
        fully_paid_on_date = try json_response.stringOf(alloc, "Invoices[i].FullyPaidOnDate");
        currency_rate = try json_response.stringOf(alloc, "Invoices[i].CurrencyRate");
        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].Payments");
        while (j < count_j) {
            json_response.setJ(j);
            payment_id = try json_response.stringOf(alloc, "Invoices[i].Payments[j].PaymentID");
            date = try json_response.stringOf(alloc, "Invoices[i].Payments[j].Date");
            amount = try json_response.stringOf(alloc, "Invoices[i].Payments[j].Amount");
            reference = try json_response.stringOf(alloc, "Invoices[i].Payments[j].Reference");
            has_account = json_response.boolOf("Invoices[i].Payments[j].HasAccount");
            has_validation_errors = json_response.boolOf("Invoices[i].Payments[j].HasValidationErrors");
            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].CreditNotes");
        while (j < count_j) {
            json_response.setJ(j);
            credit_note_id = try json_response.stringOf(alloc, "Invoices[i].CreditNotes[j].CreditNoteID");
            credit_note_number = try json_response.stringOf(alloc, "Invoices[i].CreditNotes[j].CreditNoteNumber");
            id = try json_response.stringOf(alloc, "Invoices[i].CreditNotes[j].ID");
            has_errors = json_response.boolOf("Invoices[i].CreditNotes[j].HasErrors");
            applied_amount = try json_response.stringOf(alloc, "Invoices[i].CreditNotes[j].AppliedAmount");
            date_string = try json_response.stringOf(alloc, "Invoices[i].CreditNotes[j].DateString");
            date = try json_response.stringOf(alloc, "Invoices[i].CreditNotes[j].Date");
            total = try json_response.stringOf(alloc, "Invoices[i].CreditNotes[j].Total");
            k = 0;
            count_k = json_response.sizeOfArray("Invoices[i].CreditNotes[j].LineItems");
            while (k < count_k) {
                json_response.setK(k);
                k = k + 1;
            }

            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].Prepayments");
        while (j < count_j) {
            json_response.setJ(j);
            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].Overpayments");
        while (j < count_j) {
            json_response.setJ(j);
            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].Contact.Addresses");
        while (j < count_j) {
            json_response.setJ(j);
            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].Contact.Phones");
        while (j < count_j) {
            json_response.setJ(j);
            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].Contact.ContactGroups");
        while (j < count_j) {
            json_response.setJ(j);
            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].Contact.ContactPersons");
        while (j < count_j) {
            json_response.setJ(j);
            j = j + 1;
        }

        j = 0;
        count_j = json_response.sizeOfArray("Invoices[i].LineItems");
        while (j < count_j) {
            json_response.setJ(j);
            j = j + 1;
        }

        i = i + 1;
    }
}