To send an SMS from Swift, call your messaging provider's REST API from a server such as Vapor, with the API key in a header, and never from the iPhone app itself. Vapor's built-in client does the job in one async function, with no third-party SDK.
We make Ready, so the example uses the Ready REST API and only the request shape we document. The point of this post is the Swift side: types, async/await and where the call should live.
Why the call belongs on the server
A key shipped inside an iOS binary can be extracted in minutes with freely available tools. If that key can send texts, anyone who finds it can run up your bill and text your customers. So the app asks your backend ("text me a code", "send the receipt"), and the backend decides whether to allow it and then calls the SMS API. Our post on key security and rotation goes further on this.
The send function
import Vapor
struct SmsPayload: Content {
let to: String
let body: String
let consent_attested: Bool
}
func sendSms(_ req: Request, to: String, text: String, key: String) async throws -> ClientResponse {
var headers = HTTPHeaders()
headers.add(name: "X-API-Key", value: Environment.get("READY_API_KEY") ?? "")
headers.add(name: "Idempotency-Key", value: key)
return try await req.client.post("https://api.tryready.com/v1/messages", headers: headers) { out in
try out.content.encode(SmsPayload(to: to, body: text, consent_attested: true), as: .json)
}
} Content handles the JSON encoding, so a quote or emoji in the text is escaped for you. to is E.164 (+12125550199). Set consent_attested to true only when the recipient agreed to texts.
A route that uses it
app.post("notify") { req async throws -> HTTPStatus in
let key = UUID().uuidString
let res = try await sendSms(req, to: "+12125550199", text: "Your order #1042 has shipped.", key: key)
guard res.status.code < 300 else {
req.logger.error("sms send failed: \(res.status.code)")
throw Abort(.badGateway)
}
return .accepted
} Set a timeout on the client so a stalled carrier hop can't hang the route: app.http.client.configuration.timeout = .init(connect: .seconds(5), read: .seconds(15)). That works for a demo. In production, don't make the user wait on the carrier hop. Insert a row, return 202, and let a background job (Vapor Queues is the usual choice) do the send. The job stores the idempotency key at creation and reuses it on every retry, which is what keeps a timed-out request from texting someone twice. The retries guide shows which statuses to retry and which to park.
Handling the answer
| Status | Meaning for your code |
|---|---|
| 2xx | Accepted. Store the response body, don't assume delivery |
409 QUIET_HOURS_BLOCK | Held by the quiet-hours rule. Send again once the window opens |
| 429 | Back off and retry with the same key |
500 send_failed | Not sent. Retry with the same key in a minute |
| 503 | Not sent, but the same key replays that answer. Resend under a new key |
| 504 or timeout | May still send. Check GET /v1/messages first; the same key answers 409 idempotency_in_progress until it finishes |
| Other 4xx | Don't retry. Fix the request or park it |
Accepted isn't delivered. Delivery comes later by webhook, and our inbound webhook guide shows the receiving side.
FAQ
Should I use URLSession instead of Vapor's client?
Either works. Vapor's client is the simplest inside a Vapor app. In a plain Swift command-line tool, URLSession with async data(for:) does the same job.
Can I text from an iOS app directly?
You can open the Messages composer from an app, but that sends from the user's own number. To send from your business number, call your backend, which calls the API.
How do I avoid sending the same text twice?
Create the idempotency key with the job and send it in the Idempotency-Key header on every attempt. A repeated key means the request isn't sent twice.
The short version
Server-side only, Content for the JSON, a stored idempotency key and a queue between your route and the API. The messages API docs list the rest of the fields.
Cost and registration
A segment on the Standard rate is $0.0075 plus a $0.0045 carrier fee, passed through at cost, excluding optional screening. An account can send up to 500 texts a month before it registers for 10DLC, and most approvals take 2 to 7 days. See pricing for the full list.