How to automatically shorten links in Twilio SMS for better engagement?

To integrate the IDM.in URL shortener with your Twilio SMS messaging, you’ll need to use a server-side language like Node.js, Python etc. Here is the Node.js example to learn how to shorten your link before sending a SMS.

When sending SMS messages via Twilio programmatically, it’s common to include links that may be too long or cumbersome. Long URLs can take up valuable character space in your message and make your SMS look unreadable.

Using the idm.in URL shorten API, you can write a function in your server side to shorten all your links for Twilio SMS:

Prerequisites

  1. Twilio account
  2. IDM URL shortener account
  3. Basic knowledge of http requests and API

First, install the Twilio package from sending SMS from NPM:

npm install twilio

Create a Function to Shorten URLs

Now, create a function that takes a long URL and returns a shortened version using the short links API.

  1. Login to your IDM.in account
  2. Go to settings > API keys to create your api key
const fetch = require('node-fetch');

// IDM.in API details
const IDM_API_KEY = 'your_idm_api_key';
const IDM_API_URL = 'https://api.idm.in/v1';

async function shortenUrl(longUrl) {
    const response = await fetch(IDM_API_URL + '/links', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${IDM_API_KEY}`
      },
      body: JSON.stringify({ url: longUrl })
    });
    const data = await response.json();
    if (response.ok) {
      return data.short_url;
    }
}

Send SMS via Twilio

The Twilio SMS sending function is a streamlined method to automate the process of sending text messages via Twilio’s platform. This function leverages Twilio’s API to send SMS messages to any phone number.

It can be easily customized to include dynamic content like personalized messages making it ideal for notifications, alerts, marketing campaigns, and OTPs.

const twilio = require('twilio');

// Twilio credentials
const accountSid = 'your_twilio_account_sid';
const authToken = 'your_twilio_auth_token';
const client = new twilio(accountSid, authToken);

async function sendSms(to, from, longUrl, messageBody) {
  const shortUrl = await shortenUrl(longUrl);

  const fullMessage = `${messageBody} ${shortUrl}`;
  client.messages
    .create({
      body: fullMessage,
      from: from,
      to: to
    })
    .then(message => console.log(`Message sent: ${message.sid}`))
    .catch(error => console.error('Failed to send SMS:', error));
}

Example usage

For example, if I want to send a appointment confirmation message to the customer while booking an appointment.

The sendSms function is called with parameters for the recipient’s number, the sender’s number, a long URL, and the text.

If you look on the code sendSms function code, it calls the shortenUrl function to shorten the provided long URL using the URL shortening service to convert the long URL into short concise link and then send the SMS to given phone number.

const to = '+1234567890';
const from = '+0987654321';
const longUrl = 'https://dayschedule.com/appointemnt/12312312/confirmed;
const messageBody = 'Your appointment has been confirmed, see details here - ';

await sendSms(to, from, longUrl, messageBody);

Full code here - https://gist.github.com/vickyRathee/79dce7284feea1d78d439d8b318fad31