Sending SMS in Rust: RustSMS — API library for working with SMS gateways

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 Rust: Overview of the RustSMS Library



In the modern world of web development and automation, sending SMS messages remains a popular task: from two-factor authentication to order status notifications. If you are writing in Rust and looking for a reliable way to integrate with SMS providers, the RustSMS library is what you need. It provides a unified, asynchronous, and type-safe API for working with dozens of SMS gateways (Twilio, Vonage, Clickatell, SMS.ru, and others).



In this article, we will break down the key features of RustSMS, show code examples, and explain how to start sending messages in minutes.



Key Features of RustSMS

  • Unified interface — regardless of the chosen provider, you use the same set of methods.
  • Asynchronous — the library is built on tokio and reqwest, allowing you to avoid blocking the main thread.
  • Support for popular gateways — Twilio, Vonage (Nexmo), Clickatell, SMS.ru, MessageBird, and others.
  • Type-safe configurations — all parameters (number, text, provider) are checked at compile time.
  • Easy extensibility — you can write your own adapter for any custom gateway.


Installation

Add the dependency to your Cargo.toml:

[dependencies]rustsms = "0.5"tokio = { version = "1", features = ["full"] }


Basic SMS Sending Example

Before sending, you need to create a configuration for the selected provider. Let's look at an example with Twilio:

use rustsms::{SmsClient, providers::twilio::TwilioConfig};use std::error::Error;

#[tokio::main]async fn main() -> Result<(), Box> { // Create configuration for Twilio let config = TwilioConfig::new( "YOUR_ACCOUNT_SID", "YOUR_AUTH_TOKEN", "+1234567890", // your sender number );

// Initialize the client let client = SmsClient::new(config);

// Send the message let response = client .send("+0987654321", "Hello from Rust!") .await?;

println!("SMS sent! ID: {}", response.message_id); Ok(())}


Working with Multiple Providers

RustSMS allows you to easily switch between providers. To do this, you only need to change the configuration:

use rustsms::{SmsClient, providers::vonage::VonageConfig};

let config = VonageConfig::new( "YOUR_API_KEY", "YOUR_API_SECRET", "RustSMS",);let client = SmsClient::new(config);


Error Handling and Statuses

The library returns detailed errors if something goes wrong. You can handle them using pattern matching:

match client.send("+1111111111", "Test").await {    Ok(response) => println!("Success: {:?}", response),    Err(e) => match e {        rustsms::Error::Api { code, message } => {            eprintln!("API error {}: {}", code, message);        }        rustsms::Error::Network(e) => {            eprintln!("Network error: {}", e);        }        _ => eprintln!("Unknown error"),    },}


Advanced Features: Templates and Custom Headers

RustSMS supports sending messages using templates (for example, for OTP codes) and custom HTTP headers. Example with a template:

let otp_code = 123456;let message = format!("Your confirmation code: {}", otp_code);client    .send("+1111111111", &message)    .await?;

For more complex scenarios (for example, sending via an SMS gateway with additional parameters), you can use the send_with_opts method:

use rustsms::options::SendOptions;

let opts = SendOptions::new() .with_callba

Recommendations