Go SDK
Send email from Go with the github.com/sorvsys/exx-go module. Install, configure, send, handle errors and retries, SMTP and webhooks.
github.com/sorvsys/exx-go is the official Go client for the exx.sh API. It needs Go
1.24+ and uses the standard library's HTTP and SMTP clients. MIT licensed.
Install
go get github.com/sorvsys/exx-goSend an email
package main
import (
"context"
"errors"
"fmt"
"log"
exx "github.com/sorvsys/exx-go"
)
func main() {
client := exx.NewClient("") // reads EXX_API_KEY
sent, err := client.Emails.Send(context.Background(), &exx.SendEmailRequest{
From: "Acme <hello@acme.com>",
To: []string{"delivered@example.com"},
Subject: "Your sign-in link",
Html: "<p>Click the link to sign in.</p>",
Text: "Click the link to sign in.",
Tags: []exx.Tag{{Name: "category", Value: "sign_in"}},
})
var apiErr *exx.Error
if errors.As(err, &apiErr) {
// apiErr.Name is the API's error name, e.g. "validation_error".
log.Fatalf("%s (%d): %s", apiErr.Name, apiErr.StatusCode, apiErr.Message)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(sent.Id)
}Emails.SendWithOptions and Batch.SendWithOptions also take
exx.SendOptions{IdempotencyKey, Stream}. The
sending guide explains every field.
Configure the client
exx.NewClient(key, options...) reads the key from EXX_API_KEY when key is empty.
| Option | Default | Meaning |
|---|---|---|
exx.WithBaseURL | EXX_BASE_URL, then https://api.exx.sh | The API's address |
exx.WithHTTPClient | a client of the SDK's own | Your own *http.Client |
exx.WithRetries | 2 | Retries after the first attempt, below |
exx.WithTimeout | 30 s | The time limit of each attempt |
The context.Context you pass to each call bounds the whole call, retries included.
Handle errors
API errors are *exx.Error with Name (for a server error too), Message and
StatusCode; branch on Name as the errors guide describes.
StatusCode is 0 when no answer came back, and errors.Unwrap then gives the transport
error. An id that is not a UUID is invalid_parameter with status 422, before any request.
Retries
The client retries rate-limited requests, except a spent daily or monthly quota. It retries
server errors and lost answers only when sending again cannot act twice: any request but a
POST without an Idempotency-Key. A send the server is still processing
(409 concurrent_idempotent_requests) is asked again until it has the answer.
Services
| Service | Methods |
|---|---|
Emails | Send, SendWithOptions, Get, List, Update, Cancel, ListAttachments, GetAttachment; received mail: ListReceived, GetReceived, ListReceivedAttachments, GetReceivedAttachment; Attempts and Transcript (exx only) |
Emails.Verify | an address's syntax, DNS and list findings (exx only, verify addresses) |
Events | Send and SendWithOptions (Resend's SendEventRequest shape), Create and ListNames (exx only) (application events) |
Workflows | Create, Get, List, Update, Remove, Publish, Pause, Resume, Duplicate, RotateSigningSecret and Schema (exx only, workflows) |
Workflows.Versions | List and Get a workflow's published versions |
Workflows.Runs | Start (StartWorkflowRunRequest.IdempotencyKey), List, Get, Cancel and CancelAll |
Batch | Send, SendWithOptions |
Domains | create, get, list, update, delete and verify domains; UpdateDomainRequest.DmarcReports, DmarcSummary, DmarcReports (exx only) |
ApiKeys | create, list and revoke keys |
Webhooks | create, get, list, update, delete webhooks and rotate their secret; Domains on create and update (exx only) |
Webhooks.Deliveries | List, Get and Replay a webhook's deliveries (exx only) |
Templates | Create, Get, List, Update, Publish, Duplicate, Remove; Check (exx only) |
Suppressions | list, add and remove suppressed addresses |
Metrics | Get (the series), Providers, Reputation (exx only) |
SMTP
exx.NewSMTPClient sends the same email shape over SMTP with net/smtp.
package main
import (
"context"
"errors"
"fmt"
"log"
exx "github.com/sorvsys/exx-go"
)
func main() {
smtp := exx.NewSMTPClient("") // reads EXX_API_KEY; smtp.exx.sh, port 465 (TLS)
sent, err := smtp.Send(context.Background(), &exx.SendEmailRequest{
From: "Acme <hello@acme.com>",
To: []string{"delivered@example.com"},
Subject: "Your sign-in link",
Html: "<p>Click the link to sign in.</p>",
Text: "Click the link to sign in.",
}, &exx.SendOptions{IdempotencyKey: "sign-in/123"})
var e *exx.Error
if errors.As(err, &e) {
// "smtp_error" (e.SMTPCode) for a refusal, "validation_error" for what SMTP cannot
// carry (tags, scheduling, attachment paths).
log.Fatalf("%s: %s", e.Name, e.Message)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(sent.Id) // the same id the API and webhooks use
}The options are exx.WithSMTPHost and exx.WithSMTPPort (by default EXX_SMTP_HOST and
EXX_SMTP_PORT, then smtp.exx.sh and 465), exx.WithSMTPImplicitTLS,
exx.WithSMTPTLSConfig, exx.WithSMTPTimeout and exx.WithSMTPDialer. Port 465 is TLS
from the start and any other port uses STARTTLS; the key is never sent without TLS. A
refusal is *exx.Error with Name smtp_error and SMTPCode.
Webhooks
exx.VerifyWebhook checks a delivery against the raw body and returns the parsed event
(Type, CreatedAt and the raw Data); see the webhooks guide:
package main
import (
"io"
"log"
"net/http"
"os"
exx "github.com/sorvsys/exx-go"
)
func main() {
http.HandleFunc("/webhooks/exx", func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body) // the raw body, before any JSON parsing
if err != nil {
http.Error(w, "read error", http.StatusBadRequest)
return
}
event, err := exx.VerifyWebhook(&exx.VerifyWebhookOptions{
Payload: string(body),
Headers: exx.HeadersFrom(r.Header),
WebhookSecret: os.Getenv("EXX_WEBHOOK_SECRET"),
})
if err != nil {
http.Error(w, "bad signature", http.StatusBadRequest)
return
}
log.Println(event.Type, string(event.Data))
w.WriteHeader(http.StatusNoContent)
})
log.Fatal(http.ListenAndServe(":3000", nil))
}