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

Clickatell One API: Upload and send a WhatsApp PDF

See more Clickatell Examples

Send a local invoice.pdf during the customer’s open WhatsApp service window. Upload the binary PDF for one recipient, then reference the returned fileId in a separate message to the same recipient. The upload and message submission have separate acceptance results.

Clickatell API reference

Chilkat Node.js Downloads

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    var http = new chilkat.Http();
    http.RequireSslCertVerify = true;
    //  Load the key for this One API integration from your application configuration.
    http.SetRequestHeader("Authorization","ONE_API_KEY");
    http.SetRequestHeader("Accept","application/json");

    var recipient = "PHONE_NUMBER";
    var pdf = new chilkat.BinData();
    var loaded = pdf.LoadFile("invoice.pdf");
    if (loaded !== true) {
        console.log("Could not read invoice.pdf.");
        return;
    }

    //  Restrict this customer's invoice to that customer's number.
    http.SetUrlVar("recipient",recipient);
    var upload = new chilkat.HttpResponse();
    var uploaded = http.HttpBd("POST","https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}",pdf,"application/pdf",upload);
    if ((uploaded !== true) || (upload.StatusCode !== 201)) {
        console.log("PDF upload failed. HTTP status " + upload.StatusCode);
        return;
    }

    var media = new chilkat.JsonObject();
    var decoded = media.Load(upload.BodyStr);
    if ((decoded !== true) || (media.BoolOf("accepted") !== true)) {
        console.log("Upload was not accepted: " + media.StringOf("error.description"));
        return;
    }

    var fileId = media.StringOf("fileId");
    if ((media.JsonTypeOf("fileId") !== 1) || (fileId == "")) {
        console.log("Upload returned no file ID.");
        return;
    }

    var body = new chilkat.JsonObject();
    body.UpdateString("messages[0].channel","whatsapp");
    body.UpdateString("messages[0].to",recipient);
    body.UpdateString("messages[0].media.fileId",fileId);
    body.UpdateString("messages[0].media.contentType","application/pdf");
    body.UpdateString("messages[0].media.fileName","invoice.pdf");

    var resp = new chilkat.HttpResponse();
    var sent = http.HttpJson("POST","https://platform.clickatell.com/v1/message",body,"application/json",resp);
    if (resp.StatusCode == 0) {
        console.log("No HTTP response. Check the message report before resending.");
        return;
    }

    var reply = new chilkat.JsonObject();
    var parsed = reply.Load(resp.BodyStr);
    if ((sent !== true) || (resp.StatusCode !== 202)) {
        console.log("Send returned HTTP " + resp.StatusCode);
        if (reply.HasMember("error.description") == true) {
            console.log(reply.StringOf("error.description"));
        }

        return;
    }

    if (reply.HasMember("error.code") == true) {
        if (reply.IsNullOf("error.code") !== true) {
            console.log("Submission error " + reply.StringOf("error.code") + "  " + reply.StringOf("error.description"));
            return;
        }

    }

    //  HTTP acceptance and the recipient result are separate checks.
    if ((parsed !== true) || (reply.JsonTypeOf("messages[0].accepted") !== 5)) {
        console.log("Missing recipient result. Check the message report.");
        return;
    }

    if (reply.BoolOf("messages[0].accepted") !== true) {
        console.log("Message rejected: " + reply.StringOf("messages[0].error.code") + "  " + reply.StringOf("messages[0].error.description"));
        return;
    }

    if (reply.JsonTypeOf("messages[0].apiMessageId") !== 1) {
        console.log("Accepted without a message ID. Check the message report.");
        return;
    }

    console.log("Accepted. Message ID: " + reply.StringOf("messages[0].apiMessageId"));

}

chilkatExample();