--- name: send-sms title: "Send SMS" description: "Send an SMS message using the Telnyx Messaging API and the Telnyx Ruby SDK, exposed through a Rails controller endpoint." language: ruby framework: rails telnyx_products: [Messaging] channel: [sms] --- # Send SMS Send an SMS message using the Telnyx Messaging API and the Telnyx Ruby SDK, exposed through a Rails controller endpoint. ## Why Telnyx Telnyx is an **AI Communications Infrastructure** platform — voice, messaging, SIP, AI, and IoT on one private, global network. - **Deliverability built in** — number reputation, 10DLC registration, and deliverability monitoring included. ## Telnyx API Endpoints Used - **Send Message**: `POST /v2/messages` -- [API reference](https://developers.telnyx.com/api/messaging/send-message) ## Architecture ``` POST /sms/send │ ▼ ┌──────────────────┐ │ SmsController │ │ (Telnyx::Client) │ └────────┬─────────┘ │ client.messages.create ▼ ┌──────────────────┐ │ Telnyx Messaging │ └────────┬─────────┘ │ └──► SMS delivered ``` ## Environment Variables Copy `.env.example` to `.env` and fill in: | Variable | Type | Example | Required | Description | Where to get it | |----------|------|---------|----------|-------------|-----------------| | `TELNYX_API_KEY` | `string` | `KEY0123456789ABCDEF` | **yes** | Telnyx API v2 key | [Portal](https://portal.telnyx.com/api-keys) | | `TELNYX_PHONE_NUMBER` | `string` | `+15551234567` | **yes** | Telnyx sending number (E.164) | [Portal](https://portal.telnyx.com/numbers/my-numbers) | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/send-sms-ruby cp .env.example .env # ← fill in your credentials bundle install ruby app.rb # starts the Rails app ``` `app.rb` defines an `SmsController` whose `send_sms` action is mapped to `POST /sms/send`. ## API Reference ### `POST /sms/send` Sends a single SMS through the Telnyx Messaging API. ```bash curl -X POST http://localhost:3000/sms/send \ -H "Content-Type: application/json" \ -d '{ "to": "+12125551234", "message": "Hello from Telnyx!" }' ``` **Response:** ```json { "message_id": "40385f64-5717-4562-b3fc-2c963f66afa6", "status": "queued", "from": "+15551234567", "to": "+12125551234" } ``` ## Troubleshooting | Issue | Cause | Fix | |-------|-------|-----| | `{"error":"Invalid API key"}` (401) | `TELNYX_API_KEY` is missing, wrong, or has trailing whitespace | Copy a fresh key from [portal.telnyx.com/api-keys](https://portal.telnyx.com/api-keys) into `.env` and restart the app — env vars load at boot | | `{"error":"Phone number must be in E.164 format (e.g., +15551234567)"}` (400) | The `to` value does not start with `+` | Send the number in E.164 form, e.g. `+12125551234` | | `{"error":"Missing required fields: 'to' and 'message'"}` (400) | The request body omitted `to` or `message` | Include both fields in the JSON body | | `{"error":"TELNYX_PHONE_NUMBER environment variable not set"}` (500) | `TELNYX_PHONE_NUMBER` is not exported | Set it in `.env` and restart the app | | `{"error":"Rate limit exceeded. Please slow down."}` (429) | Too many requests in a short window | Back off and retry; batch sends with a queue | | `uninitialized constant Telnyx::Client` | The `telnyx` gem is not installed | Run `bundle install` and restart the app | ## Related Examples - [send-sms-python](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/send-sms-python/README.md) - Send SMS with Python / Flask - [send-sms-nodejs](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/send-sms-nodejs/README.md) - Send SMS with Node.js - [send-sms-go](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/send-sms-go/README.md) - Send SMS with Go - [send-bulk-sms-python](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/send-bulk-sms-python/README.md) - Send SMS to many recipients - [receive-sms-webhook-python](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/receive-sms-webhook-python/README.md) - Handle inbound SMS webhooks ## Resources - [Messaging Guide](https://developers.telnyx.com/docs/messaging) - [Send a Message — API Reference](https://developers.telnyx.com/api-reference/messages/send-a-message) - [Ruby SDK](https://developers.telnyx.com/development/sdk/ruby) - [Telnyx SMS API](https://telnyx.com/products/sms-api) - [Messaging Pricing](https://telnyx.com/pricing/messaging) - [Telnyx Portal](https://portal.telnyx.com)