To send an SMS from an SQS consumer, call the REST API once per message, use the SQS messageId (or an id you put in the body) as the Idempotency-Key, and report only the failed messages back to SQS so successful ones aren't retried. Set the visibility timeout well above your function timeout, and give the queue a dead-letter queue (DLQ) for messages that keep failing.
We make Ready, so the example uses our REST API and only the request shape we document: POST /v1/messages with to, body and consent_attested, plus X-API-Key and Idempotency-Key. The SQS settings apply to any SMS API.
Why SQS plus SMS needs care
A standard SQS queue delivers at least once. The same message can arrive twice, and a message whose handler is slow or crashes comes back after the visibility timeout. For most work that's a nuisance. For texts it's a customer getting the same message two times.
The fix is a stable key. A redelivered message keeps its messageId, so using that value as the Idempotency-Key means a repeat request isn't sent twice. If one logical text can be enqueued twice by your producer, put your own id in the body and use that instead. Our deterministic key guide shows how.
Queue settings that matter
| Setting | Suggested value | Why |
|---|---|---|
| Visibility timeout | At least 6 times the function timeout (AWS's guidance for Lambda) | A message must not reappear while it's still being processed |
| Redrive policy | DLQ with maxReceiveCount of 5 | Poison messages stop cycling and wait for a person |
| Message retention on the DLQ | 14 days | Time to find and fix the cause |
| Batch size | 10 or fewer | A smaller batch keeps one slow message from delaying many |
ReportBatchItemFailures | Enabled on the event source mapping | Only failed messages return to the queue |
Without ReportBatchItemFailures, one failure in a batch of 10 makes SQS redeliver all 10. The idempotency key would protect you from duplicates, but you'd still spend ten API calls to send one text.
The handler
This is a Node 20 Lambda handler (ES module). The AWS SDK v3 ships in the Node runtime. The same processOne function works in a long-polling worker built on @aws-sdk/client-sqs.
// index.mjs
import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs';
const sqs = new SQSClient({});
const API = 'https://api.tryready.com/v1/messages';
async function processOne(record) {
const { to, body, key } = JSON.parse(record.body);
const res = await fetch(API, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.READY_API_KEY,
'Idempotency-Key': key ?? record.messageId,
},
body: JSON.stringify({ to, body, consent_attested: true }),
signal: AbortSignal.timeout(10_000),
});
if (res.ok) return 'done';
const text = await res.text();
if (res.status === 429 || res.status === 500 || res.status === 503) return 'retry';
if (res.status === 409 && text.includes('idempotency_in_progress')) return 'retry';
if (res.status === 409 && text.includes('QUIET_HOURS_BLOCK')) return 'later';
return 'park';
}
const forward = (url, record, delay = 0) =>
sqs.send(new SendMessageCommand({
QueueUrl: url,
MessageBody: record.body,
DelaySeconds: delay,
}));
export const handler = async (event) => {
const batchItemFailures = [];
await Promise.all(event.Records.map(async (record) => {
try {
const outcome = await processOne(record);
if (outcome === 'later') await forward(process.env.SOURCE_QUEUE_URL, record, 900);
if (outcome === 'park') await forward(process.env.PARKED_QUEUE_URL, record);
if (outcome === 'retry') batchItemFailures.push({ itemIdentifier: record.messageId });
} catch {
batchItemFailures.push({ itemIdentifier: record.messageId });
}
}));
return { batchItemFailures };
}; Four outcomes need four different answers:
- Success. Don't list the message. SQS deletes it.
- Retry (429, 500, 503, timeouts, network errors). List it in
batchItemFailures. SQS shows it again after the visibility timeout, and the same key goes out. A 503 means the text wasn't sent, so if you want to be strict, re-enqueue it with a new key instead of retrying under the old one. - Park (any other 4xx). A bad number or an opted-out contact fails every time, so retrying wastes calls. The handler copies the message to a separate parked queue and lets SQS delete the original. The retry guide explains which errors to stop on.
- Later (409
QUIET_HOURS_BLOCK). A time rule, not a failure. The handler re-enqueues the message with a 15-minute delay, which is the longest SQSDelaySecondsallows.
Set consent_attested to true only when the recipient agreed to texts. Read the key from an environment variable that comes from a secret store, as in our secrets manager guide.
Backoff, quiet hours and the DLQ
A plain retry comes back after one visibility timeout, which is a fixed delay. If the API answers 429, you want the message to wait longer. In a long-polling worker you can call ChangeMessageVisibility with a growing value based on the ApproximateReceiveCount attribute. In Lambda, the event record carries that attribute at record.attributes.ApproximateReceiveCount, and you can raise the queue's visibility timeout or add a delay at the producer.
Retrying a 409 QUIET_HOURS_BLOCK every 30 seconds into a closed window would burn receives until maxReceiveCount sends a good text to the DLQ, which is why it has its own outcome above. If the window is hours away, hold the message in your own store instead of looping every 15 minutes. See handling quiet hours. Alternatively, a request that sets hold_until_allowed: true asks Ready to hold the text until the window opens, the way it holds a campaign text.
For the DLQ itself, add an alarm on ApproximateNumberOfMessagesVisible greater than zero. When you fix the cause, use SQS redrive to send the messages back to the source queue. Every redriven message keeps its key, so a text that did succeed earlier isn't repeated. See observability and alerting.
FAQ
Should I use a FIFO queue to avoid duplicates?
A FIFO queue deduplicates at the queue within a five-minute window, which helps on the producer side. It doesn't cover a handler that sends and then crashes, so you still want the Idempotency-Key on the API call.
What visibility timeout should I pick?
Start at six times the function timeout. With a 10-second API timeout and a 30-second function timeout, that's 180 seconds. Raise it if you add retries inside the handler.
Can I process a whole batch in one API call?
For many numbers with the same text, yes: Ready has a bulk endpoint, covered in the bulk messages docs and in our guide to bulk chunking and partial failures. For different texts per message, send one request per record as above.
The short version
Stable key per message, partial batch responses, a visibility timeout that outlasts the function, and a DLQ with an alarm. If you'd rather not run a queue consumer, see the plain Lambda guide. The messages API docs list every field, and you can sign up for a key.