Skip to main content
Add email, SMS, and in-app notifications with per-user preferences to a Rails app by sending from one Active Job through Courier. Courier delivers all three channels and skips any user who opted out on its hosted preference page.
Save it as first_send.rb, then run it with your API key and email address:
It prints a request id that you’ll find in Courier Logs. On a Test key, use your Courier login address. Test email reaches it with no provider connected.

courier-rails-quickstart

The finished Rails app from this guide, ready to clone and run.

Build this with a coding agent

To have a coding agent add Courier to your Rails app, paste this prompt into Claude Code, Cursor, or Codex from the project root:
Install the Courier skill with npx skills add trycourier/courier-skills, and connect the Courier MCP server so the agent can check Courier Logs and templates itself.

Rules for AI agents

  • Use the trycourier gem (require "courier"). The send method is send_, because send is reserved in Ruby.
  • Read COURIER_API_KEY and COURIER_TOPIC_ID from ENV on the server. The browser gets only a scoped JWT.
  • Pass one idempotency_key per event, built from the order id and updated_at: order-#{id}-shipped-#{updated_at.strftime("%s%6N")}.
  • Send from NotifyJob, enqueued in after_update_commit, and keep preferences.subscription_topic_id on the send.
  • Mint the inbox token for the user in the session. Never take a user id from params.
  • Never call Twilio or your email provider directly. Courier routes every channel.

What you’ll build

This Rails app notifies a user through Courier when their order ships:
  • An email, a text, and an in-app message from one send, when an Order ships
  • A <courier-inbox> on a Rails view
  • A hosted preference page link in every message

Prerequisites

For the first send

For the full build

Clone the finished Rails app

Clone the Courier Rails quickstart to run the finished app with your own key, after you create the preference topic.
The steps below add or change these files:

Install the trycourier gem in your Rails app

The Courier Ruby SDK is the trycourier gem. Add it to your Gemfile, run bundle install, and put your keys in .env. You get the topic id in the next step.

Create a preference topic for Rails notifications

A Courier subscription topic is the category a Rails user opts out of. In the preferences editor, add an “Order updates” topic with the default On, click Publish, and copy its id into COURIER_TOPIC_ID. Courier hosts the preference page that {{var "urls.preferences"}} links to.

Send email, SMS, and in-app notifications from an Active Job

The Rails NotifyJob makes one Courier call to POST /send.
app/jobs/notify_job.rb
An opted-out user is skipped as UNSUBSCRIBED, and a repeated idempotency key returns the first response.

Enqueue the notification job from an after_commit callback

The Rails after_update_commit callback enqueues the Courier job only after the transaction commits.
app/models/order.rb
User#courier_id in app/models/user.rb is the id Courier knows the user by, in the send and the inbox token:
If your app doesn’t have these models yet, generate them as the quickstart’s migration does, then add has_many :orders to User:

Mint an inbox token in a Rails controller

The Courier inbox in a Rails view signs in with a JWT from POST /auth/issue-token, scoped to one user.
app/controllers/courier_tokens_controller.rb
Take the user from your session, never from the request. A route that accepts a user id lets any caller read any user’s inbox. If your app has no current_user yet, the quickstart’s ApplicationController defines a demo one:
Add the routes in config/routes.rb, with an empty InboxController#show for the page:

Show in-app notifications in a Rails view

Courier’s inbox is the <courier-inbox> web component, so a plain ERB view in Rails renders it. Run bin/importmap pin @trycourier/courier-ui-inbox (with jsbundling, npm install @trycourier/courier-ui-inbox), then in app/views/inbox/show.html.erb:
The layout needs <%= javascript_importmap_tags %> in its <head>, or the component never loads.

Verify

Run the Rails app, ship a demo order, then find the message in Courier Logs:
  1. Run bin/rails server and open localhost:3000.
  2. In the cloned repo, run bin/rails courier:send. lib/tasks/courier.rake ships an order inline and logs the request id. In your own app, set an order’s status to shipped, and the job logs it.
  3. The message lands in the inbox and at DEMO_EMAIL. Without DEMO_PHONE, Courier skips SMS.
  4. Turn “Order updates” off on the linked preference page and send again. Logs shows UNSUBSCRIBED.

Go to production with Courier

Courier is notification infrastructure for humans and AI agents. In a Rails app it sends through Twilio and your email provider. Go live covers moving from Test to Production:
  • Run a real queue backend, such as Solid Queue, because the async adapter loses jobs on restart.
  • Use a Production key and topic.
  • Match the token’s expires_in to your session.

FAQ

The Noticed gem delivers notifications from your Rails app with a delivery method per channel. Courier sends through your providers and adds hosted preferences, failover, an inbox, and one log.
Rails users change their Courier notification preferences on a hosted page that {{var "urls.preferences"}} links from every message. To embed it, see Build a preference center.
Sidekiq is optional for Courier in Rails. The send is one HTTP call from an Active Job, so any Active Job backend works.
Rails renders in-app notifications with Courier’s <courier-inbox> web component. Pin @trycourier/courier-ui-inbox, add the tag to an ERB view, and sign in with a JWT. See Add an inbox.