Skip to content

SendLayer/sendlayer-go

Repository files navigation

SendLayer Logo

SendLayer Go SDK

The official Go SDK for interacting with the SendLayer API, providing a simple and intuitive interface for sending emails, managing webhooks, and retrieving email events.

MIT licensed Publish Go SDK

Installation

go get github.com/sendlayer/sendlayer-go

Quick Start

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/sendlayer/sendlayer-go"
)

func main() {
	sl := sendlayer.New(os.Getenv("SENDLAYER_API_KEY"))

	resp, err := sl.Emails.Send(
		"paulie@example.com",
		"recipient@example.com",
		"Test Email",
		"This is a test email",
		"",
		nil, nil, nil,
		nil, nil, nil,
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Email sent! Message ID:", resp.MessageID)
}

Features

  • Email Module: Send emails with HTML/text content, attachments, CC/BCC, reply-to, custom headers, and tags
  • Webhooks Module: Create, retrieve, and delete webhooks for various email events
  • Events Module: Retrieve email events with filtering options
  • Error Handling: Clear, typed errors for API and validation issues

Email

Send emails using the SendLayer client:

sl := sendlayer.New("your-api-key")

resp, err := sl.Emails.Send(
	sendlayer.EmailAddress{Email: "paulie@example.com", Name: "Paulie Paloma"},
	[]sendlayer.EmailAddress{
		{Email: "recipient1@example.com", Name: "Recipient 1"},
		{Email: "recipient2@example.com", Name: "Recipient 2"},
	},
	"Complex Email",
	"Plain text fallback",
	"<p>This is a <strong>test email</strong>!</p>",
	[]sendlayer.EmailAddress{{Email: "cc@example.com", Name: "CC"}},
	[]sendlayer.EmailAddress{{Email: "bcc@example.com", Name: "BCC"}},
	[]sendlayer.EmailAddress{{Email: "reply@example.com", Name: "Reply"}},
	[]sendlayer.Attachment{{Path: "path/to/file.pdf", Type: "application/pdf"}},
	map[string]string{"X-Custom-Header": "value"},
	[]string{"tag1", "tag2"},
)
if err != nil {
	log.Fatal(err)
}

Events

sl := sendlayer.New("your-api-key")

// Get all events
all, err := sl.Events.Get(nil, nil, "", "", nil, nil)
if err != nil {
	log.Fatal(err)
}

// Get filtered events (last 24h, opened)
end := time.Now()
start := end.Add(-24 * time.Hour)
ev := "opened"
filtered, err := sl.Events.Get(&start, &end, ev, "", nil, nil)
if err != nil {
	log.Fatal(err)
}

fmt.Println("All events count:", all.TotalRecords)
fmt.Println("Filtered events count:", filtered.TotalRecords)

Webhooks

sl := sendlayer.New("your-api-key")

// Create a webhook
webhook, err := sl.Webhooks.Create("https://your-domain.com/webhook", "open")
if err != nil {
	log.Fatal(err)
}
fmt.Println("Webhook created:", webhook.WebhookID)

// Get all webhooks
webhooks, err := sl.Webhooks.Get()
if err != nil {
	log.Fatal(err)
}
fmt.Println("Webhooks:", webhooks)

// Delete a webhook
if err := sl.Webhooks.Delete(123); err != nil {
	log.Fatal(err)
}

Error Handling

The SDK returns typed errors to help you handle different scenarios:

resp, err := sl.Emails.Send(/* ... */)
if err != nil {
	var apiErr *sendlayer.SendLayerAPIError
	var valErr *sendlayer.SendLayerValidationError
	if errors.As(err, &apiErr) {
		fmt.Println("API error:", apiErr.Message, apiErr.StatusCode)
		return
	}
	if errors.As(err, &valErr) {
		fmt.Println("Validation error:", valErr.Error())
		return
	}
	fmt.Println("Unexpected error:", err)
	return
}

More Details

To learn more about using the SendLayer SDK, be sure to check our Developer Documentation.

License

MIT License - see LICENSE file for details

About

Official Golang SDK for SendLayer Email API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages