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

Activix CRM Create a Phone

See more Activix CRM Examples

Create a phone. Returns the created phone.

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();

    http.setAuthToken("ACCESS_TOKEN");

    http.setAccept("application/json");

    // The following JSON is sent in the request body:

    // {
    //   "lead_id": 7135833,
    //   "number": "+15141234455",
    //   "type": "home"
    // }

    // Use this online tool to generate the code from sample JSON:
    // Generate Code to Create JSON

    const json_request_body = try chilkat.JsonObject.init();
    defer json_request_body.deinit();
    json_request_body.updateInt("lead_id", 7135833) catch {};
    json_request_body.updateString("number", "+15141234455") catch {};
    json_request_body.updateString("type", "home") catch {};

    const url = "https://crm.activix.ca/api/v2/lead-phones";

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", url, json_request_body, "application/json", 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() >= 300) {
        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

    // {
    //     "data": {
    //         "id": 34566,
    //         "created_at": "2018-04-09T18:05:00+00:00",
    //         "updated_at": "2018-04-09T18:05:00+00:00",
    //         "lead_id": 3466512,
    //         "number": "+15141234455",
    //         ...
    //     }
    // }

    _ = json_response.intOf("data.id");
    _ = try json_response.stringOf(alloc, "data.created_at");
    _ = try json_response.stringOf(alloc, "data.updated_at");
    _ = json_response.intOf("data.lead_id");
    _ = try json_response.stringOf(alloc, "data.extension");
    _ = try json_response.stringOf(alloc, "data.number");
    _ = try json_response.stringOf(alloc, "data.type");
    _ = json_response.boolOf("data.valid");
    _ = try json_response.stringOf(alloc, "data.validated");
    _ = json_response.boolOf("data.mobile");
}