To send an SMS from Sidekiq, put the HTTP call in a job, create the Idempotency-Key when you enqueue (not inside perform), and raise on 429 and 5xx so Sidekiq's retry schedule does the waiting. Because the key travels in the job arguments, every retry sends the same key, and a retry after a timeout can't text the customer twice.
We make Ready, so the code below uses the Ready REST API and only the request shape we document. If you're on Rails and want the controller and model side too, our Ruby on Rails SMS guide covers it. This post is about the job.
Why the key belongs in the job arguments
Sidekiq runs perform again with the same arguments after a failure. That's the property we want. If you generate a UUID inside perform, each retry gets a new key, and the API sees a brand new message. If you generate it at enqueue time and pass it as an argument, all attempts share one key, and the API treats a repeat as the same send.
Two rules fall out of that:
- One key per message, created once, stored in the job.
- Never reuse a key for a different message, because the second message would be treated as a repeat.
The idempotency guide explains what happens on the server side when a key repeats.
The worker
require "net/http"
require "json"
require "uri"
class SendSmsJob
include Sidekiq::Job
class Retryable < StandardError; end
class Parked < StandardError; end
sidekiq_options queue: "sms", retry: 8
sidekiq_retry_in do |count, exception|
case exception
when Parked then :kill
else (2**count) * 10 + rand(10)
end
end
URL = URI("https://api.tryready.com/v1/messages")
def perform(to, body, key)
res = post(to, body, key)
code = res.code.to_i
return if code.between?(200, 299)
raise Retryable, "status #{code}" if retryable?(code, res.body)
raise Parked, "status #{code}: #{res.body}"
end
private
def retryable?(code, text)
return true if code == 429
return true if code >= 500 && code != 503
code == 409 && text.to_s.include?("idempotency_in_progress")
end
def post(to, body, key)
http = Net::HTTP.new(URL.host, URL.port)
http.use_ssl = true
http.open_timeout = 5
http.read_timeout = 15
req = Net::HTTP::Post.new(URL.request_uri)
req["Content-Type"] = "application/json"
req["X-API-Key"] = ENV.fetch("READY_API_KEY")
req["Idempotency-Key"] = key
req.body = { to: to, body: body, consent_attested: true }.to_json
http.request(req)
end
end Enqueue it like this:
SendSmsJob.perform_async("+12125550199", "Your order #1042 has shipped.", SecureRandom.uuid) A few details worth knowing:
ENV.fetchraises ifREADY_API_KEYis missing, so a misconfigured deploy fails loudly instead of posting with an empty key.tois E.164. A 10-digit US number is treated as +1.- Set
consent_attestedtotrueonly when the recipient agreed to texts. It's your statement about that person, so pass the real answer from your own consent data. - Both timeouts are set.
Net::HTTPwaits a long time by default, and a hung socket ties up a Sidekiq thread.
What each status should do
sidekiq_retry_in returning :kill sends the job straight to the Dead set. Anything else gets an exponential delay with a little jitter, which Sidekiq counts against the retry: 8 budget.
| Response | Meaning | Job behaviour |
|---|---|---|
| 2xx | Accepted | Done |
| 429 | Slow down | Retry with the same key |
| 500, 502, 504 | Server trouble | Retry with the same key |
| 503 | Not sent | Park it, then resend with a new key |
409 idempotency_in_progress | The first attempt is still running | Retry with the same key |
409 QUIET_HOURS_BLOCK | A time rule, not an error | Park it, resend once the window opens |
| Other 4xx | Bad number, opted-out contact, validation | Park it |
Parking is the point. A bad number won't become a good number on attempt five, and an opted-out contact must not be retried at all. In the Dead set you can see, count and alert on those. Our retries and failure handling guide has the full table.
For the quiet hours case, an API text is held only if the window opens within a few hours. Otherwise it isn't sent, and you resend it once the window opens. Schedule a new job for that time with a fresh key. If you'd rather not run that logic yourself, scheduled messages are documented separately.
Keeping duplicates out at enqueue time
The job key protects against retries. It doesn't protect against your own code enqueuing twice, for example from a callback that fires on every save. Two defences help:
- Create the key when the business event happens and store it on your own row (for instance on the order), then enqueue with that stored value.
- Check for an existing key before enqueuing, so a second trigger finds the first.
Either way, the rule is the same: one message, one key, decided before the first attempt.
Testing the worker
Stub the HTTP layer with WebMock and run the job inline with Sidekiq::Testing.inline!. Return a 500 once and then a 200 and assert both requests carried the same Idempotency-Key header. Then return a 400 and assert the job raises Parked. Those two tests cover the behaviour that matters. Our guide to mocking an SMS API goes further.
FAQ
Should I use Active Job instead of Sidekiq::Job?
You can, and retry_on works, but sidekiq_retry_in with :kill is more direct, and the Dead set gives you a place to look. If you do use Active Job, still pass the key as an argument.
How many retries are reasonable?
Eight attempts on the schedule above span roughly 40 minutes. A text that is still failing after that usually needs a person, not another attempt.
What about webhooks for delivery status?
Receiving them is a separate endpoint. See the webhook docs for the payload, verify the signature, return 200 fast, dedupe and enqueue the real work. Our inbound webhook guide walks through it.
The short version
Create the key at enqueue time, send it on every attempt, retry 429 and 5xx, and park everything else where you can see it. The messages API docs list every field. When you're ready to send from your own account, you can sign up and make a first request in a few minutes.