Nexmo is a cloud communications platform that allows developers to add contextual communications to their business critical applications.

If you wish to send an SMS when a specific event in your mobile app occurs, you can write a KScript method to do this, that you can call from your mobile app or use as an action in an Automation.

Ingredients

This recipe assumes that you have KScripts enabled for your app and that you have a Nexmo account.

Recipe

Expand "Backend" in the left hand menu, select "API" and then click the "KScript" button. Create a new method called something like _sms_ and include the following JavaScript function:

function sendSMS(from, to, text) {
    var client = K.http.createClient('https://rest.nexmo.com');
    var API_KEY = 'YOUR_API_KEY';
    var API_SECRET = 'YOUR_SECRET_KEY';

    var data =  {
        "api_key" : API_KEY,
        "api_secret" : API_SECRET,
        "from" : from,
        "to" : to,
        "text" : text
    };

    var result = client.post('/sms/json', data);

    if (failed(result)) {
        K.log(result.error);
    }
    else if (result.isError()) {
        K.log(result.getBody());
    }

    return result;
}

You can then include your _sms_ helper method and call the sendSMS() function from any scripts that need to send a sms. To try it out, create another Kscript method called something like sendSMS, add input parameters(strings) for the to number and text message and then paste in the following code:

include("_sms_");
var FROM_NUMBER = 'Nexmo';

K.log(sendSMS(FROM_NUMBER, K.params.to, K.params.text));

You can now test your new method and use as an action in an Automation.

If your Nexmo account is in trial mode, then you can only send messages to verified phone numbers such as the number you provided during the signup process.

If you are using our Backend feature, you can also call this method from your mobile app or, if you wish to send a recurring SMS (for example a digest of recent activity), this can be scheduled to run once per hour or day.

Further reading

A full reference guide to the Nexmo SMS API can be found on the Nexmo website.