sendtern

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 11 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Sendtern for Go

Send transactional email, check delivery status, and capture test messages in Email Playground. The SDK requires Go 1.21 or later and uses the Go standard library.

go get github.com/Sendtern/sendtern-sdks/go@v0.3.1

Set SENDTERN_API_KEY in your server environment. A sending-only key can send emails and create captures. Retrieving delivery status requires a full-access key.

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/Sendtern/sendtern-sdks/go"
)

func main() {
    client, err := sendtern.New(os.Getenv("SENDTERN_API_KEY"), "")
    if err != nil {
        log.Fatal(err)
    }
    message := map[string]string{
        "from":    "sandbox@sendtern.test",
        "to":      "recipient@example.test",
        "subject": "Your receipt",
        "text":    "We received your payment.",
    }
    capture, err := client.Capture(context.Background(), message, "receipt-1042-preview")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Email Playground capture %s\n", capture.ID)
}

Use Send(ctx, message, key) to submit a message for delivery. Set from to an address on a Sendtern-verified domain. Call GetEmail(ctx, id) for typed delivery status and events, or Get(ctx, id) for the JSON object.

Use SendBatch(ctx, messages, key) to submit up to 100 messages as an array. A validation error rejects the strict batch. Use SendBatchPermissive to accept valid messages and report validation errors by their zero-based input index. Keep the same mode, payload, and idempotency key when retrying a batch. An empty key omits the header.

Use errors.As(err, &apiError) with var apiError *sendtern.APIError to inspect HTTP status, API error code, and the original Retry-After header when available. Each HTTP request has a 10-second timeout. Pass a context with a shorter deadline to cancel earlier. Pass a custom base URL as the second argument to New for a test environment.

Sendtern SDK guide · API reference

Documentation

Overview

Package sendtern calls the Sendtern transactional email API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Status     int
	Code       string
	Message    string
	RetryAfter string
}

APIError describes an HTTP response outside the 2xx status range. RetryAfter preserves the original header value when one was supplied.

func (*APIError) Error

func (e *APIError) Error() string

type BatchResult

type BatchResult struct {
	Data []EmailID `json:"data"`
}

BatchResult contains the IDs accepted by SendBatch.

type BatchValidationError

type BatchValidationError struct {
	Index   int    `json:"index"`
	Message string `json:"message"`
}

BatchValidationError identifies a rejected message by its zero-based input index.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is safe for concurrent use. Create one with New and reuse it.

func New

func New(apiKey, baseURL string) (*Client, error)

New creates a client for baseURL. An empty baseURL uses https://sendtern.com. Requests have a 10-second timeout and the client does not follow redirects.

func (*Client) Capture

func (c *Client) Capture(ctx context.Context, message any, idempotencyKey string) (EmailID, error)

Capture stores a JSON-encodable message in Email Playground without sending it.

func (*Client) Download added in v0.3.0

func (c *Client) Download(ctx context.Context, path string) ([]byte, error)

Download returns the original bytes from a documented native binary endpoint.

func (*Client) Get

func (c *Client) Get(ctx context.Context, id string) (map[string]any, error)

Get retrieves an email and its delivery timeline as a JSON object.

func (*Client) GetEmail

func (c *Client) GetEmail(ctx context.Context, id string) (Email, error)

GetEmail retrieves an email and its delivery timeline as typed fields.

func (Client) GoString

func (Client) GoString() string

GoString returns a description without client configuration or credentials.

func (*Client) List added in v0.3.0

func (c *Client) List(ctx context.Context, options ListOptions) (EmailList, error)

List returns email summaries using a full-access key.

func (*Client) Request added in v0.3.0

func (c *Client) Request(ctx context.Context, method, path string, body any, idempotencyKey string, result any) error

Request calls a documented native JSON endpoint and decodes into result. Pass nil for result to discard the response, including for HTTP 204.

func (*Client) Send

func (c *Client) Send(ctx context.Context, message any, idempotencyKey string) (EmailID, error)

Send submits a JSON-encodable message for asynchronous delivery. Pass the same idempotency key and payload for each retry of a logical email.

func (*Client) SendBatch

func (c *Client) SendBatch(ctx context.Context, messages any, idempotencyKey string) (BatchResult, error)

SendBatch submits an array of 1 to 100 JSON-encodable messages in strict mode.

func (*Client) SendBatchPermissive

func (c *Client) SendBatchPermissive(ctx context.Context, messages any, idempotencyKey string) (PermissiveBatchResult, error)

SendBatchPermissive submits 1 to 100 messages and reports individual validation failures. If all messages are invalid or the request fails, it returns an API error.

func (Client) String

func (Client) String() string

String returns a description without client configuration or credentials.

type Email

type Email struct {
	Object    string       `json:"object"`
	ID        string       `json:"id"`
	From      string       `json:"from"`
	To        []string     `json:"to"`
	CC        []string     `json:"cc"`
	BCC       []string     `json:"bcc"`
	ReplyTo   []string     `json:"reply_to"`
	Subject   string       `json:"subject"`
	CreatedAt time.Time    `json:"created_at"`
	UpdatedAt time.Time    `json:"updated_at"`
	LastEvent string       `json:"last_event"`
	Mode      string       `json:"mode"`
	Tags      []EmailTag   `json:"tags"`
	Text      *string      `json:"text"`
	HTML      *string      `json:"html"`
	Error     *string      `json:"error"`
	Events    []EmailEvent `json:"events"`
}

Email is the email and delivery timeline returned by GetEmail.

type EmailEvent

type EmailEvent struct {
	Kind      string    `json:"kind"`
	CreatedAt time.Time `json:"created_at"`
	Detail    *string   `json:"detail"`
}

EmailEvent represents a delivery event in the email timeline.

type EmailID

type EmailID struct {
	ID string `json:"id"`
}

EmailID identifies an accepted email or Email Playground capture.

type EmailList added in v0.3.0

type EmailList struct {
	Object  string  `json:"object"`
	Data    []Email `json:"data"`
	HasMore bool    `json:"has_more"`
}

EmailList is a page of email summaries. Detail fields are available from GetEmail.

type EmailTag

type EmailTag struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

EmailTag is a name and value attached to a message.

type ListOptions added in v0.3.0

type ListOptions struct {
	Limit  int
	After  string
	Query  string
	Status string
}

ListOptions specifies filters for List. Zero values use the server defaults.

type PermissiveBatchResult

type PermissiveBatchResult struct {
	Data   []EmailID              `json:"data"`
	Errors []BatchValidationError `json:"errors,omitempty"`
}

PermissiveBatchResult includes the accepted IDs and per-message validation errors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL