To send a text from Rust, POST JSON to your SMS provider's REST endpoint with reqwest, put the API key in a header, and match on the status code instead of unwrapping it. You don't need a vendor crate. A struct, serde, and about thirty lines do the job; the retry logic is where most of your care goes.
We make Ready, so the example uses the Ready REST API and only the request shape we document. The pattern carries to any provider: one shared Client, a timeout on every call, and a stable idempotency key so a retry never texts the same person twice. The code assumes Rust 1.75 or later with tokio, reqwest (with the json feature), serde and uuid.
What you need before the first request
- An API key. Ready authenticates with an
X-API-Keyheader. Read it from the environment withstd::env::varand never commit it. Our API key security post covers rotation. - A card and a number. A card on file and a number of your own are required to send. The number is $2 a month. To build and test first, create a test key (Settings, Developers, Environment: Test): it simulates every send, with no card, number or charge. Add the card and number before you go live.
- Consent. Each request carries
consent_attested: true, your statement that the recipient agreed to receive texts. Only set it when it's true. - A registration plan. An account can send up to 500 texts a month before it registers for 10DLC, at its normal per-text rate. After that it has to register, and most approvals take 2 to 7 days. The 10DLC explainer covers why carriers ask for it.
A first test run costs a few cents: every text is charged at the normal per-segment rate from the first one.
Send a text with reqwest
The endpoint is POST https://api.tryready.com/v1/messages. The body takes to in E.164 format, body, and consent_attested. Here's a complete program:
use serde::Serialize;
use std::time::Duration;
#[derive(Serialize)]
struct SendRequest<'a> {
to: &'a str,
body: &'a str,
consent_attested: bool,
}
async fn send(
client: &reqwest::Client,
key: &str,
idem: &str,
to: &str,
body: &str,
) -> Result<(reqwest::StatusCode, String), reqwest::Error> {
let resp = client
.post("https://api.tryready.com/v1/messages")
.header("X-API-Key", key)
.header("Idempotency-Key", idem)
.json(&SendRequest { to, body, consent_attested: true })
.send()
.await?;
let status = resp.status();
let text = resp.text().await?;
Ok((status, text))
}
#[tokio::main]
async fn main() {
let key = std::env::var("READY_API_KEY").expect("READY_API_KEY not set");
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(20))
.build()
.unwrap();
let idem = uuid::Uuid::new_v4().to_string();
match send(&client, &key, &idem, "+12125550199", "Your order #1042 has shipped.").await {
Ok((status, text)) => println!("{status} {text}"),
Err(e) => eprintln!("request failed: {e}"),
}
} Three choices matter here. serde builds the JSON, so a customer name with a quote or a newline can't break the payload. The Client is built once and reused because it pools connections. And the builder sets a timeout, so a stalled socket gives up instead of hanging a task forever.
For the exact response fields and error bodies, use the messages API reference rather than a blog post's summary.
Retries that don't double-send
If a request times out, you don't know whether the text went out. Retrying blindly can text a customer twice, and duplicate texts are a dependable way to collect opt-outs. The fix is the Idempotency-Key header: generate one UUID per logical message, store it with your message record, and reuse it on every attempt. Our post on stopping duplicate texts explains the contract in more depth.
This wrapper retries only the cases that are safe to retry:
async fn send_with_retry(
client: &reqwest::Client,
key: &str,
idem: &str,
to: &str,
body: &str,
) -> Result<(reqwest::StatusCode, String), reqwest::Error> {
let mut delay = Duration::from_millis(500);
for attempt in 1..=4 {
let result = send(client, key, idem, to, body).await;
let retry = match &result {
Err(_) => true,
Ok((status, text)) => {
*status == reqwest::StatusCode::TOO_MANY_REQUESTS
|| *status == reqwest::StatusCode::INTERNAL_SERVER_ERROR
|| (*status == reqwest::StatusCode::CONFLICT && text.contains("idempotency_in_progress"))
}
};
if !retry || attempt == 4 {
return result;
}
tokio::time::sleep(delay).await;
delay *= 2;
}
unreachable!()
} A 4xx other than 429 is your bug or a refused send, so it comes back immediately instead of being retried. The same idempotency key rides on every attempt, which is right for timeouts and a 429. A 503 was not sent, but the same key replays that stored error, so resend it under a new key; after a 504, or a code ending in OUTCOME_UNKNOWN, check GET /v1/messages first. More patterns are in the retries and failure handling guide.
Sending to many numbers
For one message to many recipients, POST /v1/messages/bulk takes a to array and the same body, up to 100 recipients per call. Split a bigger list into chunks with slice::chunks, give each chunk its own idempotency key, and run a few at a time with a tokio semaphore rather than firing everything at once. The bulk messages docs list the details.
What it costs
A segment on the Standard rate is $0.0075 plus a $0.0045 carrier fee, passed through at cost. That all-in figure doesn't include screening, the optional scrub and validation checks, which cost extra when you switch them on. A 160-character text is one segment, so 10,000 of them is 10,000 × ($0.0075 + $0.0045) before screening. Accounts opened before November 12, 2026 keep their signup prices, shown under Billing. Full details are on the pricing page.
Ready's all-in prices per segment include the carrier fee but not screening: Scrub lists and Number validation, the two switches in Settings under Compliance, each add $0.003 per segment when switched on, shown as screening on your bill, and both are off by default. Unless you turn the opt-out footer off, the first text to each contact, and one again from time to time, also gets "Reply STOP to opt out" and your business name if the message does not already have them, so a text close to the segment limit can bill one more segment.
FAQ
Is there an official Rust crate for the Ready API?
No. The API is plain HTTPS and JSON, so reqwest and serde cover it. A thin wrapper of your own, like the one above, keeps the surface small and easy to audit.
Should I use blocking or async reqwest?
Use async if you already run tokio, since a send is network-bound and you'll often want several in flight. Use reqwest::blocking for a small CLI or a cron script where one send at a time is fine.
How do I keep the API key out of logs?
Never derive Debug on a struct that holds it, and don't log request headers. Read the key from the environment at startup and pass it by reference.
The short version
A text from Rust is one authenticated POST: a typed request, a timeout, an idempotency key, and a retry loop that only retries what's safe. The Node.js and Python versions follow the same shape if your stack is mixed.