Menu

Expand
Rate this page:

Generating a Rust client for Twilio's API

The Twilio OpenAPI Spec is currently in BETA. The spec is expected to be accurate, but is currently in active development and is unsupported. If you've identified a mismatch between Twilio's API behavior and the specification, please open an issue.

With the rollout of Twilio’s OpenAPI specification, it is now possible to auto-generate your own Twilio client in your preferred programming language.

One language that Twilio currently does not have an official SDK for is Rust. Using the power of the Twilio OpenAPI specification and open source tools such as OpenAPI Generator, you can now generate a strongly-typed Rust client for Twilio’s API methods.

Setup

To begin, install OpenAPI Generator. If you are on a Mac, we recommend installing using Homebrew for ease of use as below:

brew install openapi-generator

Once OpenAPI Generator is installed, the next step is to generate your very first Twilio client in Rust. Using the below code snippet in your terminal will pull in the Twilio OpenAPI spec from GitHub, and save the resulting client into the twilio-rust directory.

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

Using the Twilio Rust client

Creating a Rust Project

Now that the client has been generated, it’s time to create some code that will enable you to interact with Twilio’s API in Rust. If you don’t already have Rust installed, you can install Rust by following the instructions as described in their documentation.

With Rust installed, create a new Rust project. We suggest that you use cargo to simplify this process and run the following command from the same directory where you executed the codegen process:

cargo new twilio-rust-example

This will scaffold out a Rust project for you, with the necessary main.rs file and cargo.toml file for managing your dependencies.

Adding dependencies

In order to make use of the generated Twilio client, add it to Cargo.toml along with a few other helpful crates:

[package]
name = "twilio-rust-example"
version = "0.1.0"
authors = ["Your Name <your@email>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dotenv = "0.15.0"
# Ensure that you define the correct relative path to the generated Rust crate
openapi = { path = "../twilio-rust" }
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1.4.0", features = ["full"] }

Here we are importing the Twilio crate (by default it is generated with the name openapi) from the location where the generated Twilio Rust code was saved. Additionally we’re depending on a few other crates:

  • dotenv to allow easy use of environment variables from a local .env file
  • reqwest to patch some buggy behavior in the old version of reqwest that the rust codegen uses
  • tokio to provide an async runtime environment so that our client code can more easily use async/await

Once you’ve saved those changes to cargo.toml, you can run the following command to install the new dependencies.

cargo build

Sending an SMS Message using Twilio and Rust

Now that our dependencies are in order, it’s time to create a program in Rust.

We will leverage environment variables in order to securely access your Twilio credentials, so the next step is to create a .env file at the root of your Rust project and provide the necessary values. Be sure to replace the placeholder values here with your own credentials and a non-Twilio phone number such as your own as TO_NUMBER. Ensure that your phone numbers are declared in the E.164 format (+1234567890).

TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_API_KEY_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_PHONE_NUMBER=+1098765432
TO_NUMBER=+1234567890

Note that we’re using best practices here, and creating the Twilio client using an API Key and Secret instead of directly using and potentially exposing your account’s auth token. If you’d like to learn more about API Keys and how to create one, please refer to the API Keys documentation!

With the environment variables in place, it's time to open src/main.rs and replace the boilerplate code there with the following sample:

        
        
        

        Execute cargo run in your terminal, and you should see a log output of the message’s SID as well as an SMS appear on your intended phone shortly after.

        $ cargo run
            Finished dev [unoptimized + debuginfo] target(s) in 0.15s
             Running `target/debug/twilio-rust-example`
        Some("SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
        
        Rate this page:

        Need some help?

        We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting our Twilio Community forums or browsing the Twilio tag on Stack Overflow.

              
              
              

              Thank you for your feedback!

              We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

              Sending your feedback...
              🎉 Thank you for your feedback!
              Something went wrong. Please try again.

              Thanks for your feedback!

              Refer us and get $10 in 3 simple steps!

              Step 1

              Get link

              Get a free personal referral link here

              Step 2

              Give $10

              Your user signs up and upgrade using link

              Step 3

              Get $10

              1,250 free SMSes
              OR 1,000 free voice mins
              OR 12,000 chats
              OR more