first_send.rb, then run it with your API key and email address:
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: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
trycouriergem (require "courier"). The send method issend_, becausesendis reserved in Ruby. - Read
COURIER_API_KEYandCOURIER_TOPIC_IDfromENVon the server. The browser gets only a scoped JWT. - Pass one
idempotency_keyper event, built from the order id andupdated_at:order-#{id}-shipped-#{updated_at.strftime("%s%6N")}. - Send from
NotifyJob, enqueued inafter_update_commit, and keeppreferences.subscription_topic_idon 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
Orderships - A
<courier-inbox>on a Rails view - A hosted preference page link in every message
Prerequisites
For the first send
- A Courier API key
- Ruby 3.2 or newer
For the full build
- A Rails 7.2 or newer app
- A preference topic id
- A connected email provider
- A connected SMS provider, such as Twilio
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.Install the trycourier gem in your Rails app
The Courier Ruby SDK is thetrycourier 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 intoCOURIER_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 RailsNotifyJob makes one Courier call to POST /send.
app/jobs/notify_job.rb
UNSUBSCRIBED, and a repeated idempotency key returns the first response.
Enqueue the notification job from an after_commit callback
The Railsafter_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:
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 fromPOST /auth/issue-token, scoped to one user.
app/controllers/courier_tokens_controller.rb
current_user yet, the quickstart’s ApplicationController defines a demo one:
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:
<%= 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:- Run
bin/rails serverand openlocalhost:3000. - In the cloned repo, run
bin/rails courier:send.lib/tasks/courier.rakeships an order inline and logs the request id. In your own app, set an order’s status toshipped, and the job logs it. - The message lands in the inbox and at
DEMO_EMAIL. WithoutDEMO_PHONE, Courier skips SMS. - 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_into your session.
FAQ
Should I use the Noticed gem or Courier?
Should I use the Noticed gem or Courier?
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.
How do users change their notification preferences in Rails?
How do users change their notification preferences in Rails?
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.Do I need Sidekiq to send notifications from Rails?
Do I need Sidekiq to send notifications from Rails?
Sidekiq is optional for Courier in Rails. The send is one HTTP call from an Active Job, so any Active Job backend works.
How do I show in-app notifications in a Rails view?
How do I show in-app notifications in a Rails view?
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.