Send SMS with Rails

Send a text from Rails with a small service object over TextKit's REST API. No gem to install.

Prompt for your AI agent

Paste this into Claude Code, Cursor or any coding agent to have it do the integration:

text
Add SMS sending to this Rails project with TextKit.

- Docs for this stack: https://jointextkit.com/docs/send-with-rails.md
- API: POST https://api.jointextkit.com/v1/messages with `Authorization: Bearer
  <key>` and a JSON body `{"to": "+1...", "body": "..."}`.
- There is no Ruby gem: POST JSON to https://api.jointextkit.com/v1/messages
  with Net::HTTP from a service object, and call it from an Active Job.
- Read the key from the `TEXTKIT_API_KEY` environment variable. Never hardcode
  it or send it to the browser.
- Phone numbers are E.164 and US or Canada only (`+1` followed by 10 digits).
- A test key (`ssms_sk_test_`) simulates: `+15005550006` always ends `delivered`
  and no phone gets a text.
- Errors are JSON: `{"error": {"code", "message", "param"}}` with a 4xx status.
  On `429`, wait `Retry-After` seconds and retry.
- Send only from server code that runs after a real event (a signup, an order).
  Never expose an endpoint that texts any number a visitor types.

Before you start

  • An API key. Create a free account and copy the test key (ssms_sk_test_...) from Home in the console. It works at once, no card.
  • Rails 6 or later.
  • Put the key in an environment variable:
bash
export TEXTKIT_API_KEY=ssms_sk_test_...

1. Add a service object

Create app/services/textkit.rb:

ruby
require "net/http"

class Textkit
  ENDPOINT = URI("https://api.jointextkit.com/v1/messages")

  class Error < StandardError; end

  def self.send_sms(to:, body:)
    req = Net::HTTP::Post.new(ENDPOINT)
    req["Authorization"] = "Bearer #{ENV.fetch("TEXTKIT_API_KEY")}"
    req["Content-Type"] = "application/json"
    req.body = { to: to, body: body }.to_json

    res = Net::HTTP.start(ENDPOINT.hostname, ENDPOINT.port, use_ssl: true) { |http| http.request(req) }
    data = JSON.parse(res.body)
    raise Error, "#{data.dig("error", "code")}: #{data.dig("error", "message")}" unless res.is_a?(Net::HTTPSuccess)

    data
  end
end

2. Send a test

bash
bin/rails runner 'puts Textkit.send_sms(to: "+15005550006", body: "Hello from TextKit")["id"]'

It prints the message ID (msg_...).

3. Send from a job

ruby
class SendSmsJob < ApplicationJob
  queue_as :default

  def perform(to, body)
    Textkit.send_sms(to: to, body: body)
  end
end
ruby
SendSmsJob.perform_later(user.phone, "Your order shipped.")

What the test key did

+15005550006 is a sandbox number: the send comes back delivered and no phone gets a text (the response says "simulated": true). Put your own verified phone in to and the test key texts it for real. See Sandbox & test numbers.

Handle errors

Textkit.send_sms raises Textkit::Error with the API's code. A failed request returns a 4xx status and a JSON body {"error": {"code": "...", "message": "...", "param": "..."}}. On 429 rate_limited, wait the Retry-After seconds and try again. Send an Idempotency-Key header (any unique string per message) and a retried request returns the first result instead of sending twice.

StatusCodeWhat to do
400invalid_requestA field is missing or malformed; param names it.
401invalid_api_keyThe key is missing, revoked or mistyped.
402insufficient_creditsAdd credits; nothing was sent or charged.
403recipient_opted_outThey texted STOP. Do not retry.
429rate_limitedWait Retry-After seconds, then retry.

Every code is in Errors.

Go live

  1. Add a number in the console.
  2. Register your use case. We file your 10DLC registration; toll-free numbers use a guided verification form. Until carriers approve it, the number texts only your verified numbers. See Compliance & registration.
  3. Swap in a live key (ssms_sk_live_...). The code stays the same.

Next steps

  • Webhooks: delivery receipts and replies, pushed to your server.
  • Verify: one-time codes in two calls, billed only when verified.
  • Send an SMS: every field the endpoint takes.
  • All quickstarts: the same guide for other languages and frameworks.