Prerequisites
Before getting started, you’ll need to:- Authorize your sending domain
- Create and retrieve your SendLayer API key
Installation
Install the SendLayer Ruby SDK using RubyGems:Usage
After the installation completes, you’re ready to start integrating the SDK into your code. The SDK includes modules to send emails, manage webhooks, and retrieve events.Sending an Email in Ruby
Create a new.rb file or edit an existing one. Then require the sendlayer gem and initialize the client with your API key. Once done, you
send_email.rb
emails.send() method to send emails using Ruby. The method requires 4 parameters:
from: Sender email addressto: Recipient email address(es)subject: The subject of your emailtextorhtml: This is the email content. You can send plain text or HTML messages to users
send_email.rb
Sending HTML Emails in Ruby
To send HTML emails, include thehtml parameter in the send call.
send_email.rb
You can include the
text parameter to have both plain text and HTML versions of your email.Sending to Multiple Recipients
You can send emails to multiple recipients and includecc, and bcc email addresses.
send_email.rb
email and name keys.
There are limits to the number of recipients you can add to a single request. See our rate limiting guide.
Including Attachments
When sending emails in Ruby with the SendLayer SDK, you can add attachments by including theattachments parameter in your email payload.
send_email.rb
path and type parameters. Make sure to specify the correct MIME type.
You can attach multiple files to your email payload, including local and remote files.
Email Parameters
Below are the supported parameters for sending emails with the Ruby SDK.Managing Webhooks
With the SendLayer Ruby SDK, you can create webhooks, list existing webhooks, and delete a webhook.Creating a New Webhook
After initializing the client, call thewebhooks.create method and pass the required parameters. You’ll need to pass the url and event parameters.
webhooks.rb
event parameter supports the following values:
- bounce
- click
- open
- unsubscribe
- complaint
- delivery
See our guide to learn more about managing webhooks and understanding webhook payload format.
Getting All Webhooks
Usewebhooks.get to retrieve all webhooks created on your SendLayer account.
webhooks.rb
Deleting a Webhook
Usewebhooks.delete(webhook_id) to delete a webhook by its numeric ID.
Deleting a webhook cannot be undone.
webhooks.rb
Webhooks Parameters
Retrieving Email Events
You can retrieve events associated with your API key.Getting All Events
Callevents.get with no parameters to retrieve recent events.
If no filter is provided, the SDK returns the top 5 events.
events.rb
Filtering Events
You can use optional parameters to filter results. Here’s an example:events.rb
Events Parameters
Error Handling
The SDK provides custom exception types for better error handling.Exception Types
SendLayer::SendLayerError: Base exception for all SendLayer errorsSendLayer::SendLayerAPIError: API-specific errors with status code and response dataSendLayer::SendLayerAuthenticationError: Invalid API key or authentication issuesSendLayer::SendLayerValidationError: Invalid parameters or validation errorsSendLayer::SendLayerNotFoundError: Resource not found (404 errors)SendLayer::SendLayerRateLimitError: Rate limit exceeded (429 errors)SendLayer::SendLayerInternalServerError: Server errors (5xx errors)
More Examples
SendLayer Ruby SDK
View more details and examples on GitHub.