Go
Use the official Lettermint Go SDK to send email from a Go application. The module provides typed requests and responses for the Sending API and Team API. See Integrations for the SDK capability overview.
This guide covers SDK v3, which uses the module path github.com/lettermint/lettermint-go/v3. If your project uses v2, follow the Go SDK v3 upgrade guide on GitHub.
Requirements
Before you start you need:
- Go 1.24 or newer. The SDK has no dependencies.
- A Lettermint account with a verified sending domain
- A Project API token from your project settings
1. Installation
Install the SDK using Go modules:
Code
Import it with the /v3 path:
Code
2. Send your first email
Create the client with your Project API token as the sending token:
Code
The client stores nothing about a message, so create it once and share it across goroutines. Every method takes a context.Context first. You can also pass the token as a string with lettermint.NewFromToken(token).
client.Emails.Send takes the message in the API's format, lettermint.SendMailRequest. Optional fields such as HTML and Text are lettermint.Nullable values, set with lettermint.Value(...):
Code
The Team API uses a separate Team API token. Pass it with lettermint.WithTeamToken, alone or next to lettermint.WithSendingToken. Each part of the client uses its own token and never falls back to the other. See the Team API quickstart.
3. Email features
The examples in this section use client.Emails.Compose(), a builder. The builder is a value: every setter returns a new builder and leaves the original unchanged, so one builder is safe to share and reuse. Keep the value that a setter returns when you build an email over several statements:
Code
Each builder example is equivalent to a lettermint.SendMailRequest passed to client.Emails.Send. Use whichever reads better.
Basic email
Send a simple text or HTML email:
Code
Multiple recipients
Send to multiple recipients using CC and BCC. Each of To, CC, BCC, and ReplyTo replaces its list:
Code
Custom headers and reply-to
Add custom headers and set reply-to addresses:
Code
Metadata
Add metadata for tracking and webhook payloads:
Code
Metadata is included in webhook payloads but not added to the actual email headers. Use it for tracking and analytics purposes.
Tags
Categorize emails for filtering and analytics:
Code
Tags accepts up to 20 name and value pairs. The SDK checks names and values before it sends the request and returns a *lettermint.ClientValidationError if one is invalid. Tag still sets the existing singular tag, which has no deprecation date. See Tags documentation.
A setter that gets invalid input records the error on the builder it returns. Send returns that error and sends nothing. Use Err() to check the builder earlier.
Route selection
Direct emails to specific routes within your project:
Code
File attachments
Attach files to your emails. Set Content to raw bytes and the SDK base64-encodes them, or set ContentBase64 to content that is already encoded:
Code
Inline images
Embed images directly in your HTML using Content-ID. Set ContentID and reference it as cid: in the markup. See Inline images.
Code
Idempotency
Pass an idempotency key so a retry never sends the same email twice. If a request with the same key is repeated within the idempotency window, Lettermint returns the original result instead of sending again. The key is an option of the call that sends the email:
Code
client.Emails.Send and client.Emails.SendBatch take the same option. The key applies only to that call, and the SDK never retries on its own.
Use one unique key for each logical email. For example, combine the order ID and the email type. A retry with the same key does not send a duplicate email. See the idempotency documentation for the full behaviour.
Batch sending
Send up to 500 emails in one request. Use Build to include a builder in the batch:
Code
4. Client configuration
Customize the client with functional options:
Code
The timeout covers the response headers and the body. lettermint.WithRequestTimeout(d) overrides it for one call, and cancelling the context.Context stops a request. lettermint.WithHTTPClient(c) takes your own http.Client, for example with a proxy. The SDK copies it and never changes yours. The SDK never follows redirects, so tokens never go to another host.
5. Response
Code
6. Error handling
Every error the SDK returns implements lettermint.Error. Check the types with errors.As:
Code
*lettermint.APIError carries Status, Code, Message, Details, and Body. The status-specific types embed it and unwrap to it, so errors.As(err, &apiErr) also matches a *lettermint.RateLimitError. The status types are *AuthenticationError (401), *PermissionError (403), *NotFoundError (404), *ConflictError (409), *ValidationError (422), *RateLimitError (429), and *ServerError (5xx).
Other errors include *ConnectionError, *UnexpectedResponseError, *RedirectError, *ConfigError for a missing token, and *ClientValidationError when the SDK rejects a request before it sends it. A cancelled context returns the context's error, not an SDK error.
The SDK never retries on its own. Reuse the same idempotency key when you retry so the customer gets one email.
7. Webhook verification
Verify incoming webhooks from Lettermint with the signing secret of the webhook endpoint. Pass the raw request body: the signature covers the exact bytes, so decoding and re-encoding the JSON breaks it.
Code
VerifyRequest requires the X-Lettermint-Signature and X-Lettermint-Delivery headers and accepts deliveries signed within 5 minutes by default. Change that with lettermint.NewWebhook(secret, lettermint.WithTolerance(time.Minute)). A failure is a *lettermint.WebhookVerificationError with a Reason. event.Event is a lettermint.WebhookEvent, and unknown events pass through as their string, so give switch statements a default branch where it matters. See Webhook signatures for the signing scheme.
Store your webhook secret in an environment variable. Never hardcode secrets in your application.
Track delivery, opens, and bounces
Sending is only half of a transactional setup. To see what happens after a message leaves your app, combine the SDK with Lettermint's platform features:
- Email tracking records opens and clicks, with bot filtering so your metrics stay accurate.
- Webhooks push delivery, bounce, and complaint events to your server in real time, so you can update order records or suppress bad addresses.
- Test addresses let you trigger a hard or soft bounce on demand while you build and verify your webhook handler.
Next steps
Tags
Organize and filter emails with tags.
Tracking
Track opens, clicks, and deliverability.
Webhooks
Receive delivery events through webhooks.
SMTP Alternative
Send via SMTP instead of the API.
GitHub Repository
Find the complete source code, report issues, or contribute on GitHub.