Platform
Docs
Solutions
ContactLog In

2 Ways To Send Text Messages (SMS) with Rust [How-to Guide]

SMS marketing has the highest reach and response rate compared to typical email marketing. SMS allows organizations to interact with end users by sending relevant, brief, and personalized communications. Hence, SMS sending has become a common feature in modern applications like e-commerce sites.

As developers, it is essential to know the different ways of implementing such features to deliver the best applications to customers. So, in this article, I will discuss two ways to send SMS using Rust to give you a better understanding.

01. Using Twilio API

Twilio is a programmable SMS API that allows you to build powerful messaging features. With this API's help, you can send and receive SMS messages, monitor the delivery of sent messages, plan SMS deliveries for a later time, and access and edit message records.

Advantages of using Twilio API

  • Supports multiple programming languages.
  • You will only be charged for the services you use.
  • Good documentation and customer support.

Disadvantages of using Twilio API

  • Not very mobile-friendly.
  • More expensive than other APIs.
  • Not easy for non-developer users to get started.

Tutorial: How to Send SMS Using Twilio API

Prerequisites

The OpenAPI Generator should be installed on your machine to continue with Twilio implementation. You can use the below command to install OpenAPI Generator.

1 2 3 openapi-generator generate -g rust \  -i https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json \   -o Twilio-rust

Step 1 - Updating dependencies

As the first step, you need to update the cargo.toml file with the below dependencies.

1 2 3 4 5 6 7 8 9 10 11 [package] name = "twilio-rust-example" version = "0.1.0" edition = "2021" #See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] dotenv = "0.15.0" openapi = { path = "../twilio-rust" } tokio = { version = "1.21.1", features = ["rt", "rt-multi-thread", "macros"] }

Then, run the cargo build command to install the dependencies.

Step 2 - Setup environment variables

After installing dependencies, you must configure the Twilio API key, API secret, account SID and Twilio mobile number in your project. For example, you can include those details in the .env file like the below:

1 2 3 4 5 TWILIO_API_KEY = SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TWILIO_API_KEY_SECRET = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TWILIO_ACCOUNT_SID = ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TWILIO_PHONE_NUMBER = +1098765432 TO_NUMBER = +1234567890

You can find more information on creating an API key from this documentation.

Step 3 - Sending SMS

Once the environment variables are updated, you can modify the Rust code to send SMS. For that, update themain.rs file with the below code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 use dotenv; use openapi::apis::{configuration::Configuration, default_api as twilio_api}; use std::env; #[tokio::main] async fn main() { dotenv::dotenv().expect("Failed to read .env file"); let account_sid = env::var("TWILIO_ACCOUNT_SID").expect("Failed to parse Account SID"); let api_key = env::var("TWILIO_API_KEY").expect("Failed to parse API Key"); let api_key_secret = env::var("TWILIO_API_KEY_SECRET").expect("Failed to parse API Key Secret"); let from = env::var("TWILIO_PHONE_NUMBER").expect("Failed to parse 'from' number"); let to = env::var("TO_NUMBER").expect("Failed to parse 'to' number"); twilio_config.basic_auth = Some((api_key, Some(api_key_secret))); let message = twilio_api::create_message( &twilio_config, &account_sid, &to, None, None, None, None, None, None, None, None, None, None, None, None, None, None, Some(&from), None, Some("Hello !!!"), None, ) .await; let result = match message { Ok(result) => result, Err(error) => panic!("Something went wrong, {:?}", error), }; println!("{:?}", result.sid); }

Finally, execute cargo run command to execute the program, and you will receive the SMS on the defined mobile number within a few seconds.

02. Using Infobip API

Infobip is another popular API you can use to implement SMS features with Rust. Most organizations use Infopib to scale their communications at global levels since it is a cloud base service. In addition to SMS, Infobip supports multiple communication channels, including email, voice, WhatsApp Business, Messenger, and many more.

Advantages of using Infobip API

  • Simple user interface.
  • Good documentation.
  • Supports multiple languages, including Rust, JavaScript, Flutter, Python, iOS, etc.

Disadvantages of using Infobip API

  • Support teams take a significant time to respond.
  • High cost.
  • Limitations in data extraction.

Tutorial: How to Send SMS Using Infobip API

authentication

Step 1 - Updating dependencies

As the first step, you need to update the cargo.toml file with the below dependencies.

1 2 [dependencies] infobip_sdk = "0.3.0"

Then, run the cargo build command to install the dependencies.

Step 2 - Sending SMS

After installing dependencies, update the main.rs file with the below code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 use infobip_sdk::model::sms::{Destination, Message, SendRequestBody}; use infobip_sdk::api::sms::SmsClient; use infobip_sdk::configuration::Configuration; #[tokio::main] async fn main() { let sms_client = SmsClient::with_configuration( Configuration::from_env_api_key().unwrap() ); let mut message = Message::new( vec![Destination::new("123456789012".to_string())] ); message.text = Some("Your message text".to_string()); let request_body = SendRequestBody::new(vec![message]); let response = sms_client.send(request_body).await.unwrap(); assert_eq!(response.status, reqwest::StatusCode::OK); println!("Response body:\n{}", serde_json::to_string(&response.body).unwrap()); }

Finally, execute cargo run command to execute the program, and you will receive the SMS on the defined mobile number within a few seconds.

You can find more details on Infobip payload handling in their documentation.

Conclusion

This article discussed two approaches we can use to implement SMS sending with Rust, alongside their pros and cons. I hope these suggestions will help you to implement the best SMS features for your Rust applications.

View More Guides

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.