Skip to main content

Prerequisites

Before getting started, you’ll need to:
  1. Authorize your sending domain
  2. Create and retrieve your SendLayer API key

Installation

Install the SendLayer Go SDK using the go get command:

Usage

After installation, you can use the SDK modules for sending emails, managing webhooks, and retrieving email events.

Send Email in Golang

Initialize the client and call sl.Emails.Send() with a SendEmailRequest.
sendEmail.go

Send HTML Emails in Go

To send HTML emails, set the Html field in the request payload.
sendEmail.go
You can include both Text and Html to send plain text and HTML versions of the same email.

Send Email to Multiple Recipients

Use EmailAddress objects to send to multiple recipients, including Cc, Bcc, and ReplyTo.
sendEmail.go
From and To accept either a string email or EmailAddress values. Cc, Bcc, and ReplyTo support the same flexible format.
There are limits to the number of email recipients you can add to a single request. See our rate limiting guide to learn more.

Include Attachments in Email

Include attachments by setting the Attachments field in SendEmailRequest.
sendEmail.go
The maximum email size SendLayer allows is 10MB. This includes both the message as well as attachment files.
Set Path to your local file path or hosted file URL, then set the file Type (MIME 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 Request Parameters

The table below contains the supported fields in SendEmailRequest.
FieldTypeRequiredDescription
Fromstring or EmailAddressYesSender email, as a plain email string or EmailAddress (Email + optional Name)
Tostring, EmailAddress, or []EmailAddressYesRecipient email(s), as one address or multiple addresses
SubjectstringYesEmail subject line
TextstringYesPlain text body
HtmlstringNoHTML body
CcEmailAddress or []EmailAddressNoCarbon-copy recipients
BccEmailAddress or []EmailAddressNoBlind carbon-copy recipients
ReplyToEmailAddress or []EmailAddressNoReply-to recipient(s)
Attachments[]AttachmentNoFile attachments with Path and MIME Type
Headersmap[string]stringNoCustom email headers
Tags[]stringNoTags for categorizing emails

Manage Webhooks in Go

With the SendLayer Go SDK, you can create, list, and delete webhooks.

Create a New Webhook

Call Webhooks.Create() with a WebhookCreateRequest.
webhooks.go
The Event field accepts the following options:
  • bounce
  • click
  • open
  • unsubscribe
  • complaint
  • delivery
Example success response for new webhook:

Get All Webhooks

To view all the webhooks you’ve created, use the Webhooks.Get() method.
webhooks.go
Here is an example response:

Delete a Webhook

To delete a specific webhook, pass the webhook ID to Webhooks.Delete().
Deleting a webhook cannot be undone.
webhooks.go

Webhook Request Parameters

The table below contains the supported inputs in the Webhooks module.
ParameterTypeRequiredDescription
WebhookURLstringYes (create)Webhook endpoint URL where SendLayer sends events
EventstringYes (create)Event type for the webhook subscription
webhookIdintYes (delete)Unique webhook ID to delete

Retrieve Email Events

Use the Events module to retrieve email delivery and engagement events.

Get All Events

Initialize the client and call Events.Get(nil) to retrieve recent events.
events.go
Example response:

Filter Events

Pass a GetEventsRequest to filter by date range and event type.
events.go

Event Request Parameters

The table below contains supported fields in GetEventsRequest.
FieldTypeRequiredDescription
StartDate*time.TimeNoStart date for filtering events
EndDate*time.TimeNoEnd date for filtering events
EventstringNoFilter by event type (for example, opened)
MessageIDstringNoFilter by SendLayer message ID
StartFrom*intNoStarting offset for paginated event results
RetrieveCount*intNoNumber of records to return

Error Handling

The SDK returns typed errors you can inspect with errors.As.
Here is an example error response:

More Go SDK Examples

SendLayer Go SDK

View more details and examples on GitHub.