Sending SMS in TypeScript: API Libraries and Integration

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending SMS in TypeScript: Overview of APIs and Libraries

Modern web applications and services often require integration with SMS gateways to send notifications, confirmations, or marketing messages. TypeScript, as a superset of JavaScript, provides strict typing, which is especially useful when working with external APIs. In this article, we will look at how to organize sending SMS from a TypeScript application using popular libraries and services.



Why TypeScript for working with SMS API?

TypeScript adds static typing, which reduces the number of runtime errors. When working with SMS providers, this is critical because API requests often contain complex data structures (phone numbers, text, sending parameters). Types help avoid typos in field names and ensure correct data transfer.



Main libraries for sending SMS

There are several popular services on the market that provide HTTP APIs for sending SMS. Let's look at two of the most common options for TypeScript.



1. Twilio (official SDK)

Twilio is one of the market leaders in cloud communications. The official SDK for Node.js is fully compatible with TypeScript and comes with ready-made types.

Installation:

npm install twilionpm install @types/twilio --save-dev  // types are already included, but for older versions

Example of sending SMS:

import { Twilio } from 'twilio';

const accountSid = 'YOUR_ACCOUNT_SID';const authToken = 'YOUR_AUTH_TOKEN';const client = new Twilio(accountSid, authToken);

interface SmsParams { to: string; // recipient number in format +71234567890 from: string; // sender number (purchased in Twilio) body: string; // message text}

async function sendSms(params: SmsParams): Promise<void> { try { const message = await client.messages.create({ body: params.body, from: params.from, to: params.to, }); console.log(`SMS sent. SID: ${message.sid}`); } catch (error) { console.error('Sending error:', error); }}

// UsagesendSms({ to: '+79001234567', from: '+15005550006', body: 'Hello! This is a test message from TypeScript.',});


2. SMS Aero (REST API via fetch/axios)

SMS Aero is a Russian service popular for local projects. There is no official SDK for TypeScript, but you can use typed wrappers over fetch or axios.

Installation:

npm install axios

Example with a typed request:

import axios, { AxiosResponse } from 'axios';

interface SmsAeroResponse { status: 'success' | 'error'; id?: number; message?: string;}

interface SmsAeroParams { to: string; from?: string; text: string; sign?: string; // sender signature}

const API_URL = 'https://gate.smsaero.ru/v2/sms/send';const EMAIL = 'your_email@example.com';const API_KEY = 'your_key';

async function sendSmsAero(params: SmsAeroParams): Promise<SmsAeroResponse> { const auth = Buffer.from(`${EMAIL}:${API_KEY}`).toString('base64'); const response: AxiosResponse<SmsAeroResponse> = await axios.post( API_URL, { number: params.to, text: params.text, sign: params.sign || 'SMS Aero', }, { headers: { 'Authorization': `Basic ${auth}`, 'Content-Type': 'application/json', }, } ); return response.data;}

// UsagesendSmsAero({ to: '79001234567', text: 'Your confirmation code: 1234', sign: 'MyApp',}).then(res => console.log(res));


Creating a universal TypeScript wrapper

For projects where a provider change may be required, it is convenient to create an abstract interface

Recommendations