Sign in to fill these examples with your own keys. Sign in Start free trial

Send emails with Go

Install the Go module from GitHub and call EmailsSend.

What you'll build

A Go program that sends a receipt email with the postshiba module and prints the queued message.

Before you start

  • An API key with Send only access. Full access also works. See Create an API key. Support keys cannot send.
  • A verified sending domain. See Add and verify a domain. from must be an address on it.
  • A cluster that is ready to send (sending_ready is true). See Clusters.
  • Go 1.22 or later.

Install

Run this inside a Go module. Start one with go mod init if you need to.

install.sh
1go get github.com/postshiba/postshiba-go

Send your first email

Pass your API key to NewClient, then call EmailsSend with a map. WithTeamID is only needed for team-scoped calls such as listing clusters, and those calls need a Full access key that stays out of your deployed app. The team id is on the API keys page.

send.go
1package main 2 3import ( 4 "context" 5 "fmt" 6 "log" 7 8 "github.com/postshiba/postshiba-go" 9) 10 11func main() { 12 client := postshiba.NewClient("YOUR_API_KEY", postshiba.WithTeamID("KjkAJW")) 13 14 sent, err := client.EmailsSend(context.Background(), map[string]any{ 15 "from": "hello@mail.example.com", 16 "to": []string{"you@example.com"}, 17 "reply_to": "support@example.com", 18 "subject": "Your receipt", 19 "text": "Thanks for your order.", 20 "html": "<p>Thanks for your order.</p>", 21 "unique_args": map[string]any{"order_id": "ord_123"}, 22 }) 23 if err != nil { 24 log.Fatal(err) 25 } 26 fmt.Println(sent) 27}

Run it with go run send.go. EmailsSend returns (any, error), and the value is the decoded JSON body of the 201 response. The message then shows under Events in the dashboard.

response.json
1{ 2 "queued": true, 3 "message_id": "abc@capsule.test", 4 "from": "hello@mail.example.com", 5 "to": ["you@example.com"], 6 "subject": "Your receipt", 7 "unique_args": {"order_id": "ord_123"} 8}

Pin a cluster

Without a pin, PostShiba uses your first cluster that is ready to send. Pass SendOptions with ClusterID to choose one.

pin.go
1sent, err := client.EmailsSend(ctx, message, &postshiba.SendOptions{ClusterID: "NmQpXr"})

Attachments and templates

Attach a file

Add attachments with filename, content_type, and base64 content. Up to 10 files, 18 MB in total. See Attachments for allowed types.

attachment.go
1pdf, err := os.ReadFile("invoice.pdf") 2if err != nil { 3 log.Fatal(err) 4} 5 6_, err = client.EmailsSend(ctx, map[string]any{ 7 "from": "hello@mail.example.com", 8 "to": []string{"you@example.com"}, 9 "subject": "Your invoice", 10 "text": "Your invoice is attached.", 11 "attachments": []map[string]any{{ 12 "filename": "invoice.pdf", 13 "content_type": "application/pdf", 14 "content": base64.StdEncoding.EncodeToString(pdf), 15 }}, 16})

Send a published template

Pass template with the alias or public id and its variables. Leave out html and text.

template.go
1_, err := client.EmailsSend(ctx, map[string]any{ 2 "from": "hello@mail.example.com", 3 "to": []string{"you@example.com"}, 4 "template": map[string]any{ 5 "id": "welcome", 6 "variables": map[string]any{"name": "Ada"}, 7 }, 8})

Keep the key safe

Use a Send only key in your app. It can send email and call GET /api/v1/users/me. Every other request returns 403 forbidden.

If this key leaks Send only Full access
Send mail from your verified domains Yes Yes
Read events, recipients, templates, and sent messages No Yes
See SMTP passwords and webhook secrets No Yes
Change clusters, domains, credentials, webhooks, and suppressions No Yes
  • Keep Full access keys for setup scripts, the CLI, and MCP. Don't deploy one with your app.
  • Read the key with os.Getenv("POSTSHIBA_API_KEY") instead of a string in your code. Don't commit it, log it, or put it in a URL.
  • Send from your server. Never put a key, even a Send only key, in browser or mobile code.
  • Create one key per app and environment. If a key leaks, delete it on API keys and create a new one. Deleting stops it straight away.

Troubleshooting

A response outside 2xx returns a *postshiba.Error. Err holds the JSON error string, next to Field and Message.

errors.go
1var apiErr *postshiba.Error 2if errors.As(err, &apiErr) { 3 log.Println(apiErr.Err, apiErr.Field, apiErr.Message) 4}
Status error What to do
401 Invalid token or no user signed in The API key is missing, mistyped, or deleted. Copy it again from API keys.
403 forbidden Support keys cannot send. A Send only key gets this on any call other than sending.
403 domain_unverified from is not on a verified domain.
403 kyc_required Your team is pending production approval. Until then, send only to team members, live inbox addresses, and inbox forward-to addresses.
422 invalid field names the problem, such as a missing to, an unpublished template, or a rejected attachment.
429 throttled The cluster hit its hourly throttle or 24-hour cap. The client does not retry, and retrying right away hits the same cap. See What happens at your cap.
429 rate_limited More than 300 send requests in one minute on one API key. Slow down.

Every other status is on Errors.

Next steps