In this tutorial, we will learn how to send SMS messages with TypeScript and Twilio. Twilio is a cloud communications platform that provides APIs for sending and receiving SMS messages, among other features. TypeScript is a typed superset of JavaScript that provides better type checking and code readability.

Before we begin, please make sure you have a Twilio account with an active phone number that can send SMS messages. Also, ensure that you have Node.js installed on your computer.


Step 1: Installing Dependencies

To get started, create a new directory for our project and navigate to it in your terminal. Then, run the following command to initialize a new Node.js project:

npm init -y

Next, we will install the following dependencies using npm:

npm install twilio @types/twilio

The twilio package is the official Twilio SDK for Node.js. The @types/twilio package provides TypeScript typings for the Twilio SDK, enabling us to write type-safe code.


Step 2: Setting up Twilio Credentials

To use Twilio, we need to authenticate ourselves using our account SID and auth token. You can find these credentials on the Twilio Console dashboard. Copy your account SID and auth token to a new file called .env in the root of your project directory:

TWILIO_ACCOUNT_SID=<your_account_sid>
TWILIO_AUTH_TOKEN=<your_auth_token>
TWILIO_PHONE_NUMBER=<your_phone_number>

We will use the dotenv package to load these credentials into our TypeScript code. Run the following command to install it:

npm install dotenv


Step 3: Writing TypeScript Code

Create a new file called send-sms.ts in the root of your project directory. Here's a complete example of how to send an SMS message using TypeScript and Twilio:

import dotenv from 'dotenv';
import twilio from 'twilio';

dotenv.config();

const client = twilio(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);

const from = process.env.TWILIO_PHONE_NUMBER;
const to = '<recipient_phone_number>';
const message = '<your_message>';

client.messages
  .create({
    from,
    to,
    body: message,
  })
  .then((message) => console.log(`Sent SMS message ${message.sid}`))
  .catch((error) => console.error(error));

In this code, we first load our Twilio credentials from the .env file using the dotenv package. We then create a new Twilio client instance using our account SID and auth token.

Next, we specify the sender phone number (from), recipient phone number (to), and message text (message). We use the client.messages.create() method to send the SMS message, passing in the necessary parameters as an object.

Finally, we handle the promise returned by client.messages.create(). If the SMS message is successfully sent, we log the message SID to the console. Otherwise, we log the error that occurred.


Step 4: Running the Code

To run our TypeScript code, we need to transpile it to JavaScript using the TypeScript compiler. Run the following command to install it:

npm install typescript

Next, create a new file called tsconfig.json in the root of your project directory with the following contents:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist"
  },
  "include": ["**/*.ts"],
  "exclude":["node_modules","dist"]
}

This configuration tells TypeScript to transpile our code to ES6 and output the JavaScript files to a `dist` directory. We exclude the `node_modules` and `dist` directories from transpilation.

To transpile our TypeScript code, run the following command:

npx tsc

This command will generate a new JavaScript file called `send-sms.js` in the `dist` directory.

To run the code, use the following command:

node dist/send-sms.js 

Make sure to replace `<recipient_phone_number>` and `<your_message>` with the phone number of the recipient and the message you want to send, respectively.

Congratulations! You have successfully sent an SMS message using TypeScript and Twilio.


Conclusion

In this tutorial, we learned how to send SMS messages with TypeScript and Twilio. We installed the necessary dependencies, set up Twilio credentials, wrote TypeScript code to send an SMS message, and transpiled our code to JavaScript. By following these steps, you can easily send SMS messages using Twilio and TypeScript in your Node.js applications.