How to send emails with Go

Send emails from Go using the standard library `net/http` package.

There is no official SDK — call the Publiq REST API with your language HTTP client, from the server, with the API key in the Authorization header.

package publiq

import (
	"bytes"
	"encoding/json"
	"net/http"
	"os"
)

func SendWelcome(to string) error {
	body, _ := json.Marshal(map[string]any{
		"from":        "you@yourdomain.com",
		"to":          to,
		"templateKey": "welcome-email",
		"variables":   map[string]string{"first_name": "Ana"},
	})
	req, _ := http.NewRequest("POST", "https://api.publiq.digital/v1/emails", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("PUBLIQ_API_KEY"))
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
	return nil
}

Best practices

  • Always call Publiq from the server — the API key goes in the Authorization header, never on the client.
  • On 429/5xx, retry with exponential backoff (respect Retry-After).
  • Prefer templateKey over inline HTML to keep content versioned.
  • Handle errors via the response error.code field — see Errors.
  • Send from a verified domain — see Domains.

See also

How to send emails with Go — Publiq Docs