Sign inGet Started

Go SDK

Send email with Gin

Use Parcel Wing from Gin handlers with JSON binding and server-side API keys.

Install the SDK

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

Terminal

go get github.com/parcelwing/parcelwing-go

Gin handler

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

main.go

package main
 
import (
"net/http"
"os"
 
"github.com/gin-gonic/gin"
parcelwing "github.com/parcelwing/parcelwing-go"
)
 
func main() {
client, err := parcelwing.New(os.Getenv("PARCELWING_API_KEY"))
if err != nil {
panic(err)
}
 
router := gin.Default()
router.POST("/send-welcome", func(c *gin.Context) {
var payload map[string]string
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
 
emails, err := client.Emails.Send(c.Request.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 {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
 
c.JSON(http.StatusOK, gin.H{"id": emails[0].ID})
})
 
router.Run(":8080")
}

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.