Prerequisites
Before getting started, you’ll need to:- Authorize your sending domain
- Create and retrieve your SendLayer API key
Installation
Install the SendLayer Go SDK using thego get
command:
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
Create a new.go
file or edit an existing one. Import the sendlayer
package and initialize the client with your SendLayer API key.
sendEmail.go
Sending HTML Emails
To send HTML emails, include the HTML content as the fifth parameter in theSend()
method.
sendEmail.go
You can include both
text
and html
parameters to have both plain text and HTML versions of your email messageSending to Multiple Recipients
You can send emails to multiple recipients including"CC"
and "BCC"
email addresses using the EmailAddress
struct.
sendEmail.go
EmailAddress
structs containing the recipient’s Name
and Email
.
There are limits to the number of email recipients you can add to a single request. See our rate limiting guide to learn more.
Including Attachments
You can include attachments to your email message by adding theAttachment
struct to the email payload.
sendEmail.go
The maximum email size SendLayer allows is 10MB. This includes both the message as well as attachment files.
Path
field with the path to the attachment file you wish to attach and specify the correct Type
.
SendLayer SDK supports both local and remote file attachments and allows you to add multiple attachment files. See our tutorial to learn more about attaching files to emails.
Email Parameters
Below is a table containing the supported parameters for sending emails in Go using SendLayer SDK.Parameter | Type | Required | Description |
---|---|---|---|
from | string or EmailAddress | Yes | Email address of the sender. Can be a single email string or an EmailAddress struct with Email and Name fields |
to | string or []EmailAddress | Yes | Email address(es) of the recipient(s). Can be a single email string or a slice of EmailAddress structs |
subject | string | Yes | Subject line of the email |
text | string | Yes | Plain text version of the email content |
html | string | No | HTML version of the email content |
cc | []EmailAddress | No | CC email addresses as a slice of EmailAddress structs |
bcc | []EmailAddress | No | BCC email addresses as a slice of EmailAddress structs |
replyTo | []EmailAddress | No | Reply-to email addresses as a slice of EmailAddress structs |
attachments | []Attachment | No | Slice of Attachment structs containing file paths and MIME types |
headers | map[string]string | No | Map containing custom headers as key-value pairs |
tags | []string | No | Slice of strings for tagging emails |
Managing Webhooks
With the SendLayer Go SDK, you can create new webhooks, view all webhooks you’ve created, and also delete a specific webhook.Creating a New Webhook
To create a new webhook, you’ll need to first import the SendLayer package and initialize it with your API key. Then call theWebhooks.Create()
method and specify the required parameters. This method requires the url
and event
parameters.
webhooks.go
event
parameter is constrained to the following options:
- bounce
- click
- open
- unsubscribe
- complaint
- delivery
Getting All Webhooks
To view all the webhooks you’ve created, use theWebhooks.Get()
method.
webhooks.go
Deleting a Webhook
To delete a specific webhook, use theWebhooks.Delete()
method. This method accepts one required parameter webhookId
that needs to be an integer.
Deleting a webhook cannot be undone. You won’t be able to recover or access your webhook after deleting it.
webhooks.go
Webhooks Parameters
The table below contains details about the supported parameters in theWebhooks
module.
Parameter | Type | Required | Description |
---|---|---|---|
url | string | Yes | The webhook endpoint URL where events will be sent |
event | string | Yes | The type of event to listen for. Options: bounce, click, open, unsubscribe, complaint, delivery |
webhookId | int | Yes | Unique identifier for the webhook (used in delete operation) |
Retrieving Email Events
You can view all events connected to your API key.Getting All Events
To get started, import the SendLayer package and initialize it with your API key. Then call theEvents.Get()
method to retrieve all events.
The
Events.Get()
method retrieves the top 5 events in your account if no filter parameter is specified.events.go
Filtering Events
TheEvents.Get()
method in the SendLayer
client accepts some optional parameters. These parameters can be used to filter the API response. Here is an example:
events.go
Events Parameters
The table below contains details about the available parameters in theEvents
module.
Parameter | Type | Required | Description |
---|---|---|---|
startDate | *time.Time | No | Start date for filtering events |
endDate | *time.Time | No | End date for filtering events |
event | string | No | Filter the API request by the type of event. Supported events include: accepted , rejected , delivered , opened , clicked , unsubscribed , complained , failed |
messageId | string | No | Filter by the email MessageId |
startFrom | *int | No | Specify a starting number for the email filter |
retrieveCount | *int | No | This parameter controls the number of event records that’ll be displayed. It defaults to 5 if none is specified |
Error Handling
The SDK provides custom error types (SendLayerError
, SendLayerAPIError
, SendLayerValidationError
) for better error handling. You can use type assertions to handle different error types:
More Examples
SendLayer Go SDK
View more details and examples on GitHub.