Send SMS messages with Telnyx - a cheaper alternative to Twilio

Here's a quick and easy way to send SMS messages with Telnyx - a cheaper alternative to Twilio.

First Step

Register at https://telnyx.com/ to get your API key and phone number.

Telnyx.cfc

Check the Repo out at https://gitlab.com/blusol/telnyx-sms-coldfusion/

component {
    public function init(required string apiKey) {
        this.apiKey = arguments.apiKey;
        this.apiUrl = "https://api.telnyx.com/v2/messages"
        return this;
    }

    public struct function sendSMS(required string to, required string from, required string message) {
        // Prepare the payload
        var payload = {
            "to": arguments.to,
            "from": arguments.from,
            "text": arguments.message
        };

        // Make the HTTP request
        cfhttp(method="POST", charset="utf-8", url=this.apiUrl, result="httpResponse") {
            cfhttpparam(name="Authorization", type="header", value="Bearer " & this.apiKey);
            cfhttpparam(name="Content-Type", type="header", value="application/json");
            cfhttpparam(type="body", value=serializeJSON(payload));
        }

        // Parse the response
        var response = deserializeJSON(httpResponse.fileContent);

        return response;
    }
}

Sample Use

<cfset sms = new telnyx('YOUR_API_KEY')>
<cfset response = sms.sendSMS('toPhoneNumber', 'fromPhoneNumber', 'Your Message Here')>
<cfdump var="#response#">

It's that easy!

Share this post