Sending SMS in Rust: Overview of API and Libraries
Rust is a modern systems programming language that combines high performance with memory safety. Although the Rust ecosystem is younger than that of Python or JavaScript, it already has all the necessary tools for integrating with SMS services. In this article, we will look at how to send SMS messages from Rust applications using popular APIs and libraries.
Main Approaches to Sending SMS
There are two main ways to send SMS from Rust:
- Using an HTTP client — directly calling the REST API of an SMS provider (Twilio, Vonage, SMS-Center, etc.)
- Using specialized crates — ready-made libraries that wrap the provider's API into a convenient Rust interface
Option 1: Sending SMS via Twilio API
Twilio is one of the most popular services for sending SMS. To work with it, you will need reqwest (HTTP client) and serde (JSON serialization/deserialization).
// Cargo.toml[dependencies]reqwest = { version = "0.12", features = ["json"] }serde = { version = "1.0", features = ["derive"] }serde_json = "1.0"tokio = { version = "1", features = ["full"] }
// main.rsuse reqwest::Client;use serde::{Deserialize, Serialize};
#[derive(Serialize)]struct TwilioSmsRequest { To: String, From: String, Body: String,}
#[derive(Deserialize, Debug)]struct TwilioSmsResponse { sid: String, status: String,}
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> { let account_sid = "YOUR_TWILIO_ACCOUNT_SID"; let auth_token = "YOUR_TWILIO_AUTH_TOKEN"; let from_number = "+1234567890"; // Number purchased from Twilio let to_number = "+0987654321";
let client = Client::new(); let url = format!("https://api.twilio.com/2010-04-01/Accounts/{}/Messages.json", account_sid);
let request_body = TwilioSmsRequest { To: to_number.to_string(), From: from_number.to_string(), Body: "Hello from Rust!".to_string(), };
let response = client .post(&url) .basic_auth(account_sid, Some(auth_token)) .form(&request_body) .send() .await?;
let sms_response: TwilioSmsResponse = response.json().await?; println!("SMS sent! SID: {}, Status: {}", sms_response.sid, sms_response.status);
Ok(())}Option 2: Using the twilio Crate
For more convenient work with Twilio, there is a specialized crate twilio. It provides a typed API and automatically handles authentication.
// Cargo.toml[dependencies]twilio = "0.5"tokio = { version = "1", features = ["full"] }
// main.rsuse twilio::Client;
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> { let client = Client::new( "YOUR_TWILIO_ACCOUNT_SID", "YOUR_TWILIO_AUTH_TOKEN" );
let message = client .send_message("+1234567890", "+0987654321", "Hello from Rust!") .await?;
println!("Message sent! SID: {}", message.sid); Ok(())}Option 3: Sending SMS via Vonage (Nexmo)
Vonage (formerly Nexmo) is another popular provider. To work with it, you can use the vonage crate.
// Cargo.toml[dependencies]vonage = "0.2"tokio = { version = "1", features = ["full"] }
// main.rsuse vonage::VonageClient;
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> { let client = VonageClient::new( "YOUR_VONAGE_API_KEY", "YOUR_VONAGE_API_SECRET" );
let response = client .send_sms("VonageAPI", "79001234567", "Hello from Rust!") Also in library
Machine Learning in Go: Overview of Libraries and Frameworks (Golang)
Processing Markdown in TypeScript: Libraries, Parsers, and Practical Applications
Working with S3 and Cloud Storage in Swift (iOS/macOS): AWSS3 and Amplify
Working with Excel in TypeScript: Libraries, Examples, and Tools