Skip to main content

Prerequisites

Before getting started, you’ll need:
  1. Ruby 3.0 or later
  2. A Rails 7 or Rails 8 application
  3. Authorize your sending domain
  4. Create and retrieve your SendLayer API key

Installation

Add the gem to your Gemfile.
Gemfile
Then install it:

Storing your API key

Rails encrypts credentials, so the key can live in the repository safely. Open the encrypted credentials file:
Add the key to the file:
config/credentials.yml.enc
Read the value through Rails.application.credentials:
config/initializers/sendlayer.rb
The initializer runs at boot, so the client is ready before the first request.
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. The emails.send method accepts a hash of parameters.
app/controllers/contacts_controller.rb
Add the route:
config/routes.rb
The 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
Enqueue the job from the controller:
app/controllers/signups_controller.rb
The retry_on line handles transient API failures. Active Job re-runs the job with increasing delays instead of dropping the message.
Active Job needs a backing queue in production. The default async adapter loses jobs on restart, so configure Solid Queue, Sidekiq, or another adapter.

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
Render the template and pass the output to the SDK:
app/jobs/welcome_email_job.rb
The tags array labels the message. Tags appear in your event history, which helps you separate welcome emails from other traffic.
Keep email templates in a dedicated layout. Email clients support a narrow subset of CSS, so those views rarely share styles with your web pages.

Testing the endpoint

Start the development server:
Then post a request from another terminal:
A successful request returns the message ID:
Open the Email logs page in your SendLayer dashboard to confirm delivery.

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.