To send a text from Elixir, make an HTTPS POST to /v1/messages with Req, put your API key in an X-API-Key header and send the recipient and message as JSON. Req encodes the body with Jason for you, so the whole call is about ten lines. The care goes into retries, because a retried request that sends twice costs you money and opt-outs.

We make Ready, so the example uses the Ready REST API and only the request shape we document. The patterns carry over to any provider: config from runtime.exs, one UUID per message, and a background job that reuses the same key on every attempt. The code assumes Elixir 1.15 or later, Phoenix 1.7 or later and Oban 2.17 or later.

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-Key header. Load it from the environment and never commit it.
  • A card and a number. A card on file and a number of your own are required to send, and the number is $2 a month. To build 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, your promise that the recipient agreed to texts. Only set it when it's true.
  • A plan for registration. An account can send up to 500 texts a calendar month until its business is verified and registration is paid for. Most approvals take 1 to 14 days. Our 10DLC explainer for SaaS platforms covers the why.

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.

Config in runtime.exs

Read the key at boot, not at compile time. Anything in config/config.exs is baked into a release, while config/runtime.exs runs when the node starts.

# config/runtime.exs
import Config

if config_env() == :prod do
  config :my_app, MyApp.Ready,
    api_key: System.fetch_env!("READY_API_KEY"),
    base_url: "https://api.tryready.com"
end

System.fetch_env!/1 raises at boot if the variable is missing, which is what you want. A node that starts without a key and fails on the first customer text is worse than one that refuses to start.

Send one text with Req

Add {:req, "~> 0.5"} to mix.exs. Req brings Jason along, and json: encodes the map and decodes the reply.

defmodule MyApp.Ready do
  @moduledoc "Thin client for the Ready messages API."

  def send_text(to, body, idempotency_key) do
    cfg = Application.fetch_env!(:my_app, __MODULE__)

    Req.post(
      url: cfg[:base_url] <> "/v1/messages",
      headers: [
        {"x-api-key", cfg[:api_key]},
        {"idempotency-key", idempotency_key}
      ],
      json: %{to: to, body: body, consent_attested: true},
      receive_timeout: 30_000,
      retry: false
    )
  end
end

Two choices are worth explaining. retry: false turns off Req's built-in retry, because its default treats some responses as safe to repeat that you may not want repeated. You'll own the retry rules instead. And receive_timeout gives a stalled connection a deadline, so a slow network doesn't hold a process forever.

to is E.164 (a 10-digit US number is treated as +1). The call returns {:ok, %Req.Response{status: status, body: body}} or {:error, exception}. For exact response fields and error bodies, use the messages API reference rather than a blog post's summary.

If you'd rather use Finch directly, the shape is the same: build the request with Finch.build(:post, url, headers, Jason.encode!(payload)), run it with Finch.request/2 and decode with Jason.decode!/1. Req sits on top of Finch, so you give up nothing by using it.

Two optional fields are worth knowing. from picks one of your own numbers, and media_url sends a picture as MMS. Leave from out and Ready picks the number: the one this contact already texts with, otherwise your primary number.

Retry rules that don't double-send

If a request times out, you don't know whether the text went out. The Idempotency-Key header fixes that: send one UUID per message, and a retry with the same key doesn't send twice. Generate it with Ecto.UUID.generate() or UUID.uuid4().

Here's when to reuse the key and when not to:

What came backWhat to do
Timeout or dropped connectionRetry with the same key
409 idempotency_in_progressWait a few seconds, retry with the same key
2xxDone. Store the result
5xxDon't retry yet. Check GET /v1/messages, then send again with a new key if it never went out
Other 4xxDon't retry. The same request fails the same way

The 5xx row surprises people. A 5xx is stored under the key and replayed, so sending again with the same key returns the same error and sends nothing. If you checked and the text didn't go out, that resend is a new send and needs a new key. Our posts on idempotency and duplicate texts and retries and failure handling go deeper.

An Oban job with one key per message

A Phoenix request shouldn't wait on a carrier. Enqueue a job instead, and let Oban handle the attempts. The trick is to create the idempotency key when you enqueue, store it in the job's args, and let every attempt read it from there.

defmodule MyApp.Workers.SendText do
  use Oban.Worker, queue: :sms, max_attempts: 5

  alias MyApp.Ready

  def enqueue(to, body) do
    %{to: to, body: body, key: Ecto.UUID.generate()}
    |> new()
    |> Oban.insert()
  end

  @impl Oban.Worker
  def perform(%Oban.Job{args: %{"to" => to, "body" => body, "key" => key}}) do
    case Ready.send_text(to, body, key) do
      {:ok, %{status: status}} when status in 200..299 ->
        :ok

      {:ok, %{status: 409, body: resp}} ->
        if inspect(resp) =~ "idempotency_in_progress" do
          {:snooze, 5}
        else
          {:cancel, "refused: #{inspect(resp)}"}
        end

      {:ok, %{status: status, body: resp}} when status >= 500 ->
        {:cancel, "server error #{status}, check GET /v1/messages: #{inspect(resp)}"}

      {:ok, %{status: status, body: resp}} ->
        {:cancel, "rejected #{status}: #{inspect(resp)}"}

      {:error, _timeout_or_closed} ->
        {:error, :retry_with_same_key}
    end
  end
end

Here's what each branch does:

  • A transport error returns {:error, ...}, so Oban retries the job. The args haven't changed, so the key is the same, and the retry is safe.
  • A 409 idempotency_in_progress means the first attempt is still running. {:snooze, 5} waits five seconds and tries again with the same key.
  • A 5xx cancels the job on purpose. Don't loop on it. Check GET /v1/messages for that recipient, and if the text never went out, enqueue a fresh job, which gets a new key.
  • Other 4xx responses are cancelled, since attempt five fails exactly like attempt one.

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. A bounded max_attempts plus a telemetry handler on discarded jobs gives you a list to review rather than a silent drop.

Sending to many numbers

For the same message to many recipients, POST /v1/messages/bulk takes to as a list, with at most 100 recipients per call. Chunk a larger list, and give each chunk its own job and its own stored key.

def enqueue_bulk(numbers, body) do
  numbers
  |> Enum.chunk_every(100)
  |> Enum.map(fn chunk ->
    %{to: chunk, body: body, key: Ecto.UUID.generate()}
    |> MyApp.Workers.SendBulk.new()
  end)
  |> Oban.insert_all()
end

Set the chunk size to match 100. The bulk worker mirrors the single one, with a list in to and the bulk path. Keep the queue's concurrency small so a big list doesn't flood your own rate limits. Carriers also cap segments per second for each registered campaign, roughly 1 to 4 for a fresh standard campaign and about 10 to 75 for higher tiers, so a queue limit of a few jobs is plenty. See the bulk messages docs for the details.

A worked cost example

Assume an Elixir service sends 3,000 single-segment order updates in a month, with screening off. That's 3,000 segments, and the text bill is 3,000 × ($0.0075 + $0.0045), plus $2 for the number. Keep texts to plain characters, since one emoji drops a segment from 160 characters to 70.

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.

Where to go next

Sending is half the story. To learn whether a text was delivered, and to read replies, run a Phoenix controller that Ready calls, read the raw body, verify the signature and answer fast. The API quickstart walks through a key, a first send and a webhook.

Frequently asked questions

Do I need an Elixir SDK to send SMS?

No. Req, or Finch with Jason, is all you need. The API is one JSON POST to /v1/messages with an X-API-Key header, so a small module like the one above replaces a client library.

Where should the idempotency key come from in an Oban job?

Generate it when you enqueue and store it in the job args. Every attempt then reads the same key. Don't generate it inside perform/1, or each retry gets a fresh one and defeats the point.

Should I retry a 500 response from the API?

Not with the same key. A 5xx is stored and replayed, so the same key returns the same error. Check GET /v1/messages to see whether the text went out, then send again with a new key only if it didn't.

How many numbers can one bulk call take?

At most 100 recipients per call. Split a longer list into chunks and give each chunk its own idempotency key, so a crash and restart reuses the key instead of resending.