Sample code for 30+ languages & platforms
Zig

Quickbooks Delete an Invoice

See more QuickBooks Examples

Demonstrates how to delete 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.
    // --------------------------------------------------------------------------

    // Create the following JSON:

    // {
    //   "SyncToken": "3",
    //   "Id": "33"
    // }
    //
    // 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("SyncToken", "3") catch {};
    json_req.updateString("Id", "33") 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?operation=delete", 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": {
    //     "status": "Deleted",
    //     "domain": "QBO",
    //     "Id": "33"
    //   },
    //   "time": "2013-03-15T00:18:15.322-07:00"
    // }
    //

    _ = try json_response.stringOf(alloc, "Invoice.status");
    _ = try json_response.stringOf(alloc, "Invoice.domain");
    _ = try json_response.stringOf(alloc, "Invoice.Id");
    _ = try json_response.stringOf(alloc, "time");
}