Skip to Content
Docs
Frameworks

Go

Send transactional emails in Go using the standard net/http library or the official emitd Go SDK with high concurrency and low latency.

Go is renowned for its speed, concurrency, and minimal footprint. emitd pairs naturally with Go, processing outbound email requests at the edge in sub-second time.

1. Minimal example with standard net/http

You don't need any third-party dependencies to integrate emitd in Go — the standard library is all you need:

main.go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
)

type EmailPayload struct {
	From          string            `json:"from"`
	To            []string          `json:"to"`
	Subject       string            `json:"subject"`
	HTMLBody      string            `json:"html_body"`
	MessageStream string            `json:"message_stream,omitempty"`
	Tags          map[string]string `json:"tags,omitempty"`
}

type EmailResponse struct {
	MessageID string `json:"message_id"`
	Status    string `json:"status"`
}

func main() {
	apiKey := os.Getenv("EMITD_API_KEY")
	if apiKey == "" {
		panic("EMITD_API_KEY environment variable is required")
	}

	payload := EmailPayload{
		From:          "notifications@yourdomain.com",
		To:            []string{"user@example.com"},
		Subject:       "Account verified",
		HTMLBody:      "<h1>Your account is verified!</h1><p>Welcome to our platform.</p>",
		MessageStream: "outbound",
		Tags: map[string]string{
			"app": "backend-go",
		},
	}

	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}

	req, err := http.NewRequest("POST", "https://api.emitd.com/v1/email", bytes.NewBuffer(bodyBytes))
	if err != nil {
		panic(err)
	}

	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 10 * time.Second}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	respBody, _ := io.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusAccepted {
		fmt.Printf("Error: %s\n", respBody)
		return
	}

	var res EmailResponse
	json.Unmarshal(respBody, &res)
	fmt.Printf("Queued email with ID: %s (status: %s)\n", res.MessageID, res.Status)
}

2. Verifying webhooks in Go

Handle delivery and bounce notifications with HMAC SHA-256 verification:

webhook_handler.go
package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"io"
	"net/http"
	"os"
)

func handleEmitdWebhook(w http.ResponseWriter, r *http.Request) {
	signature := r.Header.Get("emitd-signature")
	secret := os.Getenv("EMITD_WEBHOOK_SECRET")

	body, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "Bad request", http.StatusBadRequest)
		return
	}

	mac := hmac.New(sha256.New, []byte(secret))
	mac.Write(body)
	expectedSignature := hex.EncodeToString(mac.Sum(nil))

	if !hmac.Equal([]byte(signature), []byte(expectedSignature)) {
		http.Error(w, "Invalid signature", http.StatusUnauthorized)
		return
	}

	// Signature is valid — parse the JSON body
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("OK"))
}

What's next?

  • Sending domains — set up SPF, DKIM, and custom Return-Path.
  • Batch email — send up to 100 emails in a single HTTP request.
  • Idempotency — prevent duplicate sends during network retries.

On this page