Sample code for 30+ languages & platforms
Zig

Twilio List Log Messages

See more Twilio Examples

Fetch the log containing information about SMS messages previously sent (or failed to be sent).

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Implements the following CURL command:

    // curl -X GET 'https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json?PageSize=20' \
    // -u TWILIO_ACCOUNT_SID:TWILIO_AUTH_TOKEN

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    http.setLogin("TWILIO_ACCOUNT_SID");
    http.setPassword("TWILIO_AUTH_TOKEN");

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json?PageSize=20", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadSb(sb_response_body) catch {};
    json.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "end": 1,
    //   "first_page_uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0",
    //   "next_page_uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=1&PageToken=PAMMc26223853f8c46b4ab7dfaa6abba0a26",
    //   "page": 0,
    //   "page_size": 2,
    //   "previous_page_uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0",
    //   "messages": [
    //     {
    //       "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    //       "api_version": "2010-04-01",
    //       "body": "testing",
    //       "date_created": "Fri, 24 May 2019 17:44:46 +0000",
    //       "date_sent": "Fri, 24 May 2019 17:44:50 +0000",
    //       "date_updated": "Fri, 24 May 2019 17:44:50 +0000",
    //       "direction": "outbound-api",
    //       "error_code": null,
    //       "error_message": null,
    //       "from": "+12019235161",
    //       "messaging_service_sid": null,
    //       "num_media": "0",
    //       "num_segments": "1",
    //       "price": "-0.00750",
    //       "price_unit": "USD",
    //       "sid": "SMded05904ccb347238880ca9264e8fe1c",
    //       "status": "sent",
    //       "subresource_uris": {
    //         "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMded05904ccb347238880ca9264e8fe1c/Media.json",
    //         "feedback": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMded05904ccb347238880ca9264e8fe1c/Feedback.json"
    //       },
    //       "to": "+18182008801",
    //       "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMded05904ccb347238880ca9264e8fe1c.json"
    //     },
    //     {
    //       "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    //       "api_version": "2010-04-01",
    //       "body": "look mom I have media!",
    //       "date_created": "Fri, 24 May 2019 17:44:46 +0000",
    //       "date_sent": "Fri, 24 May 2019 17:44:49 +0000",
    //       "date_updated": "Fri, 24 May 2019 17:44:49 +0000",
    //       "direction": "inbound",
    //       "error_code": 30004,
    //       "error_message": "Message blocked",
    //       "from": "+12019235161",
    //       "messaging_service_sid": null,
    //       "num_media": "3",
    //       "num_segments": "1",
    //       "price": "-0.00750",
    //       "price_unit": "USD",
    //       "sid": "MMc26223853f8c46b4ab7dfaa6abba0a26",
    //       "status": "received",
    //       "subresource_uris": {
    //         "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Media.json",
    //         "feedback": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Feedback.json"
    //       },
    //       "to": "+18182008801",
    //       "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26.json"
    //     }
    //   ],
    //   "start": 0,
    //   "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var account_sid: [:0]const u8 = "";
    var api_version: [:0]const u8 = "";
    var body: [:0]const u8 = "";
    var date_created: [:0]const u8 = "";
    var date_sent: [:0]const u8 = "";
    var date_updated: [:0]const u8 = "";
    var direction: [:0]const u8 = "";
    var error_code: [:0]const u8 = "";
    var error_message: [:0]const u8 = "";
    var from: [:0]const u8 = "";
    var messaging_service_sid: [:0]const u8 = "";
    var num_media: [:0]const u8 = "";
    var num_segments: [:0]const u8 = "";
    var price: [:0]const u8 = "";
    var price_unit: [:0]const u8 = "";
    var sid: [:0]const u8 = "";
    var status: [:0]const u8 = "";
    var subresource_uris_media: [:0]const u8 = "";
    var subresource_uris_feedback: [:0]const u8 = "";
    var v_to: [:0]const u8 = "";

    _ = json.intOf("end");
    _ = try json.stringOf(alloc, "first_page_uri");
    _ = try json.stringOf(alloc, "next_page_uri");
    _ = json.intOf("page");
    _ = json.intOf("page_size");
    _ = try json.stringOf(alloc, "previous_page_uri");
    _ = json.intOf("start");
    var uri: [:0]const u8 = try json.stringOf(alloc, "uri");
    var i: i32 = 0;
    const count_i = json.sizeOfArray("messages");
    while (i < count_i) {
        json.setI(i);
        account_sid = try json.stringOf(alloc, "messages[i].account_sid");
        api_version = try json.stringOf(alloc, "messages[i].api_version");
        body = try json.stringOf(alloc, "messages[i].body");
        date_created = try json.stringOf(alloc, "messages[i].date_created");
        date_sent = try json.stringOf(alloc, "messages[i].date_sent");
        date_updated = try json.stringOf(alloc, "messages[i].date_updated");
        direction = try json.stringOf(alloc, "messages[i].direction");
        error_code = try json.stringOf(alloc, "messages[i].error_code");
        error_message = try json.stringOf(alloc, "messages[i].error_message");
        from = try json.stringOf(alloc, "messages[i].from");
        messaging_service_sid = try json.stringOf(alloc, "messages[i].messaging_service_sid");
        num_media = try json.stringOf(alloc, "messages[i].num_media");
        num_segments = try json.stringOf(alloc, "messages[i].num_segments");
        price = try json.stringOf(alloc, "messages[i].price");
        price_unit = try json.stringOf(alloc, "messages[i].price_unit");
        sid = try json.stringOf(alloc, "messages[i].sid");
        status = try json.stringOf(alloc, "messages[i].status");
        subresource_uris_media = try json.stringOf(alloc, "messages[i].subresource_uris.media");
        subresource_uris_feedback = try json.stringOf(alloc, "messages[i].subresource_uris.feedback");
        v_to = try json.stringOf(alloc, "messages[i].to");
        uri = try json.stringOf(alloc, "messages[i].uri");
        i = i + 1;
    }
}