Tutorial: Send an SMS Message

var TeleSignSDK = require('telesignsdk');

const customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"; // Todo: find in portal.telesign.com
const apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="; // Todo: find in portal.telesign.com
const rest_endpoint = "https://rest-api.telesign.com"; // Todo: Enterprise customer, change this!
const timeout = 10*1000; // 10 secs

const client = new TeleSignSDK( customerId,
    apiKey,
    rest_endpoint,
    timeout // optional
    // userAgent
);

const phoneNumber = "Your test phone number with no special characters or spaces.";
const message = "You're scheduled for a dentist appointment at 2:30PM.";
const messageType = "ARN";

console.log("## MessagingClient.message ##");

function messageCallback(error, responseBody) {
    if (error === null) {
        console.log(`Messaging response for messaging phone number: ${phoneNumber}` +
            ` => code: ${responseBody['status']['code']}` +
            `, description: ${responseBody['status']['description']}`);
    } else {
        console.error("Unable to send message. " + error);
    }
}

client.sms.message(messageCallback, phoneNumber, message, messageType);
package com.telesign.example.messaging;

import com.telesign.MessagingClient;
import com.telesign.RestClient;

public class SendMessage {

    public static void main(String[] args) {

        String customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
        String apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";

        String phoneNumber = "Your test phone number with no special characters or spaces.";
        String message = "You're scheduled for a dentist appointment at 2:30PM.";
        String messageType = "ARN";

        try {
            MessagingClient messagingClient = new MessagingClient(customerId, apiKey);
            RestClient.TelesignResponse telesignResponse = messagingClient.message(phoneNumber, message, messageType, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
from __future__ import print_function
from telesign.messaging import MessagingClient

customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"
api_key = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="

phone_number = "Your test phone number with no special characters or spaces."
message = "You're scheduled for a dentist appointment at 2:30PM."
message_type = "ARN"

messaging = MessagingClient(customer_id, api_key)
response = messaging.message(phone_number, message, message_type)
require 'telesign'

customer_id = 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = 'EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=='

phone_number = 'Your test phone number with no special characters or spaces.'
message = 'You\'re scheduled for a dentist appointment at 2:30PM.'
message_type = 'ARN'

messaging_client = Telesign::MessagingClient.new(customer_id, api_key)
response = messaging_client.message(phone_number, message, message_type)
<?php
require __DIR__ . "/../../vendor/autoload.php";
use telesign\sdk\messaging\MessagingClient;
$customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
$api_key = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
$phone_number = "Your test phone number with no special characters or spaces.";
$message = "You're scheduled for a dentist appointment at 2:30PM.";
$message_type = "ARN";
$messaging = new MessagingClient($customer_id, $api_key);
$response = $messaging->message($phone_number, $message, $message_type);
using System;

namespace Telesign.Example.Messaging
{
    class SendMessage
    {
        static void Main(string[] args)
        {
            string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
            string apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";

            string phoneNumber = "Your test phone number with no special characters or spaces.";

            string message = "You're scheduled for a dentist appointment at 2:30PM.";
            string messageType = "ARN";
            try
            {
                MessagingClient messagingClient = new MessagingClient(customerId, apiKey;
                RestClient.TelesignResponse telesignResponse = messagingClient.Message(phoneNumber, message, messageType);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.WriteLine("Press any key to quit.");
            Console.ReadKey();
        }
    }
}
curl -X POST https://rest-api.telesign.com/v1/messaging \
-d phone_number=XXXXXXXX \
-d message="You have a dentist appointment at 2:15pm" \
-d message_type="ARN" \
-u "CUSTOMER_ID":"API_KEY"

This tutorial describes how to use TeleSign SDKs to send an SMS message. You can use this to send alerts, reminders, and notifications for a variety of situations such as:

  • Weather updates
  • Status of a package being delivered
  • Reminder for appointments or events
  • A notification about a special offer or promotion
  • Notifications about a change in status to an account, for example to let someone know their password changed

Requirements

For this tutorial, the following is required:

  • customer ID - Obtain this for your account by logging in to portal.telesign.com
  • API key - Obtain this for your account by logging in to portal.telesign.com
  • SDK - Download the SDK from TeleSign's GitHub repository for your selected language:
  • Language Version
    • Node.js - 6+
    • Java - 7+
    • Python - 2.7+
    • Ruby - 2+
    • PHP - 5.6+
    • C# - 4.5+
  • Purchased Phone Number - You must purchase a phone number in TelePortal to use with this API.

📘

NOTE:

If you do not have a customer ID, sign up for TeleSign's Free Trial.

Install the SDK

  1. Obtain your customer ID and API key.
  2. Log in to GitHub and choose the SDK in your preferred language:
  3. Download or clone the repository. If you download it, extract the repository from the .ZIP file.
  4. To install the SDK for use, do the following:
npm install telesignsdk -save
compile 'com.telesign:telesign:[current_version]'
<dependency>
    <groupId>com.telesign</groupId>
    <artifactId>telesign</artifactId>
    <version>[current_version]</version>
</dependency>
# From a terminal run:
pip install telesign
# From a terminal run:
gem install telesign
# From a terminal run:
composer require telesign/telesign
/* Add this to your packages.config file. */
<package id="Telesign" version="(insert latest version)" targetFramework="(insert framework)" />

Send an SMS Message

This section explains how to send an SMS message using Messaging. The sample code provided earlier is broken out into pieces and explained below.

In this code sample, you provide your customer ID and API key to authenticate yourself with TeleSign. Then you create a MessagingClient object and use the message method to send an SMS containing the message you specify with the message type you specify. The steps below explain the code sample with more detail.

  1. Begin by adding statements for including the appropriate part of the TeleSign SDK and any additional language specific functions you may need.
var TeleSignSDK = require('telesignsdk');
package com.telesign.example.messaging;

import com.telesign.MessagingClient;
import com.telesign.RestClient;
from __future__ import print_function
from telesign.messaging import MessagingClient

#If you are working with a version of Python 3, you do not need the first import #statement (`from __future__ import print_function`).
require 'telesign'
<?php
require __DIR__ . "/../../vendor/autoload.php";
use telesign\sdk\messaging\MessagingClient;
using System;
  1. Insert values for each of the items from customer_id/customerId through to message_type/messageType.
    • customer_id/customerId - Your TeleSign assigned customer ID, available in your account information in the portal.
    • api_key/apiKey - Your TeleSign assigned API key, available in your account information in the portal.
    • phone_number/phoneNumber - The phone number purchased from TeleSign in TelePortal that you want to use. When you provide the number, it should be a string with no spaces or special characters. Include the complete number; for example, for a US phone number, you would include the country code and the area code with the number, like this:16505551212.
    • message - Provide your message as a string.
    • message_type/messageType - Label the type of message you are sending. You have three choices:
      OTP - One time passwords, ARN - Alerts, reminders, and notifications, and MKT - Marketing traffic.
const customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"; // Todo: find in portal.telesign.com
const apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="; // Todo: find in portal.telesign.com
const rest_endpoint = "https://rest-api.telesign.com"; // Todo: Enterprise customer, change this!
const timeout = 10*1000; // 10 secs

const client = new TeleSignSDK( customerId,
    apiKey,
    rest_endpoint,
    timeout // optional
    // userAgent
);

const phoneNumber = "Your test phone number with no special characters or spaces.";
const message = "You're scheduled for a dentist appointment at 2:30PM.";
const messageType = "ARN";
public class SendMessage {

    public static void main(String[] args) {

        String customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
        String apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";

        String phoneNumber = "Your test phone number with no special characters or spaces.";
        String message = "You're scheduled for a dentist appointment at 2:30PM.";
        String messageType = "ARN";
customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"
api_key = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="

phone_number = "Your test phone number with no special characters or spaces."
message = "You're scheduled for a dentist appointment at 2:30PM."
message_type = "ARN"
customer_id = 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = 'EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=='

phone_number = 'Your test phone number with no special characters or spaces.'
message = 'You\'re scheduled for a dentist appointment at 2:30PM.'
message_type = 'ARN'
$customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
$api_key = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
$phone_number = "Your test phone number with no special characters or spaces.";
$message = "You're scheduled for a dentist appointment at 2:30PM.";
$message_type = "ARN";
namespace Telesign.Example.Messaging
{
    class SendMessage
    {
        static void Main(string[] args)
        {
            string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
            string apiKey = "EXAMPLETE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";

            string phoneNumber = "Your test phone number with no special characters or spaces.";

            string message = "You're scheduled for a dentist appointment at 2:30PM.";
            string messageType = "ARN";
  1. Instantiate a MessagingClient object containing your customer_id/customerId and api_key/apiKey. Send an SMS to the phone number containing the message and message type you provided. Store the response in response/telesignResponse. For node.js, use this step to set up a console and create a function that will handle responses when you create a client message object. Then create the client message object and pass it the function you created.
console.log("## MessagingClient.message ##");

function messageCallback(error, responseBody) {
    if (error === null) {
        console.log(`Messaging response for messaging phone number: ${phoneNumber}` +
            ` => code: ${responseBody['status']['code']}` +
            `, description: ${responseBody['status']['description']}`);
    } else {
        console.error("Unable to send message. " + error);
    }
}

client.sms.message(messageCallback, phoneNumber, message, messageType);
try {
            MessagingClient messagingClient = new MessagingClient(customerId, apiKey);
           RestClient.TelesignResponse telesignResponse = messagingClient.message(phoneNumber, message, messageType, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
messaging = MessagingClient(customer_id, api_key)
response = messaging.message(phone_number, message, message_type)
messaging_client = Telesign::MessagingClient.new(customer_id, api_key)
response = messaging_client.message(phone_number, message, message_type)
$messaging = new MessagingClient($customer_id, $api_key);
$response = $messaging->message($phone_number, $message, $message_type);
try
            {
                MessagingClient messagingClient = new MessagingClient(customerId, apiKey);
                RestClient.TelesignResponse telesignResponse = messagingClient.Message(phoneNumber, message, messageType);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.WriteLine("Press any key to quit.");
            Console.ReadKey();
        }
    }
}
  1. You should receive an SMS message containing the message you specified at the phone number you specified. You can examine the result of your phone call by looking at the contents of response.

Code it Without the SDK

If you want to write all your own code, you must handle authentication yourself. You'll need the following documents:


Did this page help you?