Go
Use the standard net/http client unless your service already standardizes on another HTTP library.
Legacy docs also reference https://github.com/mailtarget/mailtarget-go-sdk for Go 1.17.5 and newer. Confirm the package status before publishing it as the preferred installation path. The raw HTTP example below uses only the Go standard library.
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
func main() {
body, err := json.Marshal(map[string]any{
"subject": "Hello from Mailtarget",
"from": map[string]string{
"email": "you@your-verified-domain.com",
"name": "Your Name",
},
"to": []map[string]string{
{"email": "recipient@example.com"},
},
"bodyText": "If you can read this, the integration works.",
})
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", "https://transmission.mailtarget.co/v1/layang/transmissions", bytes.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("MAILTARGET_API_KEY"))
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
var out struct {
TransmissionID string `json:"transmissionId"`
}
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
panic(err)
}
fmt.Println(out.TransmissionID)
}
Set a custom http.Client timeout in production.