Sample code for 30+ languages & platforms
Zig

SimpleTexting - Send SMS Message

See more SimpleTexting Examples

Send a text message to an individual contact.

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 --request POST \
    //   --url 'https://app2.simpletexting.com/v1/send?token=YOUR_API_TOKEN&phone=SOME_STRING_VALUE&message=SOME_STRING_VALUE' \
    //   --header 'accept: application/json' \
    //   --header 'content-type: application/x-www-form-urlencoded'

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

    http.setRequestHeader("accept", "application/json");
    http.setRequestHeader("content-type", "application/x-www-form-urlencoded");

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.addParam("token", "YOUR_API_TOKEN");
    req.addParam("phone", "SOME_10_DIGIT_PHONE_NUMBER");
    req.addParam("message", "Hello: This is a sample text message with some punctuation: &%$@,;.");

    req.addHeader("Accept", "application/json");

    req.setHttpVerb("POST");
    req.setContentType("application/x-www-form-urlencoded");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq("https://app2.simpletexting.com/v1/send", req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

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

    const resp_status_code = resp.getStatusCode();
    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 resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

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

    // {
    //   "code": 1,
    //   "message": "The request succeeded",
    //   "smsid": "5bec5b9d0a975a238808ffb2"
    // }

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

    _ = j_resp.intOf("code");
    _ = try j_resp.stringOf(alloc, "message");
    _ = try j_resp.stringOf(alloc, "smsid");
}