Prerequisites
Before getting started, you’ll need:- Ruby 3.0 or later
- A Rails 7 or Rails 8 application
- Authorize your sending domain
- Create and retrieve your SendLayer API key
Installation
Add the gem to yourGemfile.
Gemfile
Storing your API key
Rails encrypts credentials, so the key can live in the repository safely. Open the encrypted credentials file:config/credentials.yml.enc
Rails.application.credentials:
config/initializers/sendlayer.rb
On platforms that inject configuration through the environment, use
ENV.fetch('SENDLAYER_API_KEY') instead. Keep config/master.key out of version control either way.Sending an email from a controller
Call the client from a controller action. Theemails.send method accepts a hash of parameters.
app/controllers/contacts_controller.rb
config/routes.rb
reply_to field points at the person who filled in the form, so support agents can reply from their inbox.
Use a
from address on a domain you authorized in SendLayer. Requests from an unauthorized domain fail.Moving sends to a background job
An HTTP call inside a controller action holds the request open. Active Job moves that work to a worker, so the response returns right away.app/jobs/send_email_job.rb
app/controllers/signups_controller.rb
retry_on line handles transient API failures. Active Job re-runs the job with increasing delays instead of dropping the message.
Rendering email bodies with ERB templates
Rails templates give you layouts, partials, and helpers.ActionController::Base.render renders any template to a string outside the request cycle, which makes it a good fit for job code.
Add the template:
app/views/emails/welcome.html.erb
app/jobs/welcome_email_job.rb
tags array labels the message. Tags appear in your event history, which helps you separate welcome emails from other traffic.
Testing the endpoint
Start the development server:Next steps
Ruby SDK Reference
Review every method and parameter in the Ruby SDK.
Managing Webhooks
Create, list, and delete webhook subscriptions.
Retrieving Events
Query delivery events for any message.
Rate Limits
Understand recipient and request limits.