To send a text from C++, make an HTTPS POST to your SMS provider's REST endpoint with libcurl, put your API key in a header and send the recipient and message as JSON. You don't need a vendor SDK. One easy handle, three headers and a timeout is the whole job. The care goes into escaping the JSON, retrying only when it's safe and keeping the key out of your source.
We make Ready, so the example uses the Ready REST API and only the request shape we document. The pattern carries over to any provider that takes JSON over HTTPS. The code is C++17 and links against libcurl. It has no other dependency, because the payload is small enough to build by hand.
What you need before the first request
Four things stand between you and a delivered text.
- An API key. Ready authenticates with an
X-API-Keyheader. Read it from an environment variable or a secrets manager at startup. Never compile it into the binary. - 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.
- Consent. Each request carries
consent_attested: true. That's your promise the recipient agreed to receive texts, so set it from a stored opt-in record, not as a constant you never look at. - libcurl. Install the development package for your platform (
libcurl4-openssl-devon Debian and Ubuntu,brew install curlon a Mac, vcpkg on Windows).
Every text is charged from the first one: each segment costs $0.0075 plus a $0.0045 carrier fee. Screening, the optional scrub and validation checks, is extra when you switch it on.
Send one text with libcurl
The endpoint is POST https://api.tryready.com/v1/messages. The body takes to in E.164 format (a 10-digit US number is treated as +1), body and consent_attested. Here's a complete program:
#include <curl/curl.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
static size_t collect(char* data, size_t size, size_t n, void* out) {
static_cast<std::string*>(out)->append(data, size * n);
return size * n;
}
// Escape a string for use inside a JSON value.
static std::string json_escape(const std::string& s) {
std::string o;
for (unsigned char c : s) {
switch (c) {
case '"': o += "\\\""; break;
case '\\': o += "\\\\"; break;
case '\n': o += "\\n"; break;
case '\r': o += "\\r"; break;
case '\t': o += "\\t"; break;
default:
if (c < 0x20) {
char buf[7];
std::snprintf(buf, sizeof buf, "\\u%04x", c);
o += buf;
} else {
o += static_cast<char>(c);
}
}
}
return o;
}
// Returns the HTTP status, or 0 if the request never got an answer.
long send_text(const std::string& to, const std::string& text,
const std::string& idem_key, std::string& response) {
const char* key = std::getenv("READY_API_KEY");
if (!key) return -1;
const std::string payload = "{\"to\":\"" + json_escape(to) +
"\",\"body\":\"" + json_escape(text) +
"\",\"consent_attested\":true}";
CURL* curl = curl_easy_init();
if (!curl) return -1;
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, ("X-API-Key: " + std::string(key)).c_str());
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, ("Idempotency-Key: " + idem_key).c_str());
curl_easy_setopt(curl, CURLOPT_URL, "https://api.tryready.com/v1/messages");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 35L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, collect);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
long status = 0;
if (curl_easy_perform(curl) == CURLE_OK) {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return status;
}
int main() {
curl_global_init(CURL_GLOBAL_DEFAULT);
std::string response;
long status = send_text("+12125550199", "Your order #1042 has shipped.",
"0b6f6d1e-6d1a-4a53-9a2e-3f1d2c9e8a11", response);
std::cout << status << " " << response << "\n";
curl_global_cleanup();
return status >= 200 && status < 300 ? 0 : 1;
} Build it with g++ -std=c++17 send.cpp -lcurl -o send, set READY_API_KEY and run it.
A few choices in there are worth explaining. json_escape exists because a customer name with a quote or a newline would otherwise produce broken JSON, and a text built by string concatenation is where that bites. Both timeouts are set, since libcurl waits forever by default. And CURLOPT_POSTFIELDS copies nothing, so the payload string must outlive the call, which it does here.
The program prints the status and the raw body. For the exact response fields and error bodies, use the messages API reference instead of a blog post's summary.
Optional fields
Two optional fields are worth knowing. from picks one of your own numbers, and media_url sends a picture as MMS. If you leave from out, Ready picks the number: the one this contact already texts with, otherwise your primary number (or your number closest to their area code if you turned Local presence on). That keeps a conversation on one thread on the contact's phone. Add them to the payload string only when you have a value.
Retries without double-sending
If your request times out, you don't know whether the text went out, and retrying blindly can text a customer twice. Duplicate texts are a quick way to collect opt-outs.
The fix is the Idempotency-Key header. Generate one UUID per logical message, store it with the message record and send the same key on every attempt of that message. In the example above the key is a literal so the program is easy to read. In real code, create it when you queue the message, not when you call send_text.
The rules that keep a retry safe:
- Reuse the key after a timeout or a dropped connection, where
send_textreturns 0, and after a 409idempotency_in_progress. - Don't reuse it after a 5xx. The answer is stored under the key and replayed, so a second try with that key sends nothing new. Check
GET /v1/messagesto see whether the text went out, then send again under a new key if it didn't. - Don't retry other 4xx responses. A malformed number or a bad key fails the same way on every attempt.
- Back off between tries. Wait a little longer each time, and stop after a few attempts. Keep the row pending in your own database so a later job can pick it up.
Ready also refuses some sends up front without charging you: opted-out numbers, numbers on your Ban list and known landlines. Treat those as final. Our posts on idempotency keys and duplicate texts and retries and failure handling go deeper. The error codes docs list each code and when to retry it.
Sending to many numbers
For the same message to many recipients, POST /v1/messages/bulk takes to as an array, with at most 100 recipients per call. Split a bigger list into batches and give each batch its own persisted idempotency key, so a crash and restart reuses the key instead of resending. The bulk messages docs have the details.
Keep the worker count small. A pool of a few threads, each with its own easy handle, is plenty. Handles aren't safe to share across threads, so don't. If you want connection reuse inside one thread, keep the handle alive between calls rather than cleaning it up each time.
What a send costs
Assume a C++ service sends 3,000 single-segment order updates in a month, with screening off. That's 3,000 segments. At $0.0075 each plus the $0.0045 carrier fee, you pay 3,000 × $0.012 = $36 for the texts, plus $2 for the number: $38 a month. The pricing page lists every rate.
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.
Accounts opened before 17:55 UTC on November 12, 2026 keep their signup prices, shown under Billing, and so do the sub-accounts they open.
Segments are what's metered, not messages. An emoji drops a segment to 70 characters, so a 140-character text with one emoji is 3 segments instead of 1. If your firmware or service builds message text at runtime, count segments before you budget.
Wrapping up
Sending an SMS from C++ is a small amount of code: build escaped JSON, set three headers, set both timeouts and check the status. The real work is a stable idempotency key per message, retrying only what's safe and keeping the API key out of the binary.
If you're comparing languages, the Go and Java guides use the same request. When you're ready to try it, sign up and follow the API quickstart.