Sign inGet Started

Go SDK

Send email with Chi

Use Parcel Wing from Chi routes while keeping the API key server-side.

Install the SDK

The official Go SDK is published as github.com/parcelwing/parcelwing-go.

Terminal

go get github.com/parcelwing/parcelwing-go

Chi route

Create the Parcel Wing client during server startup, then reuse it from trusted server-side handlers.

main.go

package main
 
import (
"encoding/json"
"net/http"
"os"
 
"github.com/go-chi/chi/v5"
parcelwing "github.com/parcelwing/parcelwing-go"
)
 
func main() {
client, err := parcelwing.New(os.Getenv("PARCELWING_API_KEY"))
if err != nil {
panic(err)
}
 
r := chi.NewRouter()
r.Post("/send-welcome", func(w http.ResponseWriter, r *http.Request) {
var payload map[string]string
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "invalid json", http.StatusBadRequest)
return
}
 
emails, err := client.Emails.Send(r.Context(), parcelwing.EmailSendRequest{
From: os.Getenv("PARCELWING_FROM_EMAIL"),
To: payload["email"],
TemplateAlias: "welcome",
TemplateParams: map[string]any{"first_name": payload["first_name"]},
})
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
 
w.Header().Set("content-type", "application/json")
json.NewEncoder(w).Encode(map[string]any{"id": emails[0].ID})
})
 
http.ListenAndServe(":8080", r)
}

Production notes

  • Keep PARCELWING_API_KEY in server-side environment variables.
  • Use the incoming request context where available so sends can respect request cancellation.
  • Log structured *parcelwing.Error fields for support and debugging.