unionewrapper

package module
v1.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

UniOne Go net/http

Go net/http

Go net/http integration of the UniOne Go SDK (github.com/unione-repo/unione-go-sdk): the API client in the request context (net/http middleware), a webhook endpoint with signature check.

API reference · Docs for LLMs: llms.txt

Install

go get github.com/unione-repo/unione-nethttp

Requirements: Go 1.21+.

Configuration

Get an API key in the control panel and keep it in the environment, never in code:

export UNIONE_API_KEY="your-api-key"

Settings are read from the environment by default:

UNIONE_API_KEY=YOUR_API_KEY
UNIONE_HOST=us1.unione.io

us1.unione.io in the example is for an account on another datacenter; without the host setting the package uses api.unione.io.

Option Environment variable Default Description
APIKey UNIONE_API_KEY User or project API key (required).
Host UNIONE_HOST api.unione.io API host (datacenter: api.unione.io, eu1.unione.io, us1.unione.io) or a full base URL.
Timeout UNIONE_TIMEOUT 30 Request timeout, seconds.
WebhookKey UNIONE_WEBHOOK_KEY API key the webhooks are signed with (the key of the user/project of the webhook), if it differs from the API key.
WebhookPath UNIONE_WEBHOOK_PATH /unione/webhook Path of the webhook endpoint.

ConfigFromEnv() reads the variables; a Config can also be filled in code. UNIONE_TIMEOUT is in seconds, Config.Timeout is a time.Duration.

Webhooks

The endpoint answers GET with 200 OK (the URL check of webhook/set) and accepts the POST notifications: it checks the auth field (MD5 of the request body in which the auth value is replaced by the API key), answers 401 to a wrong signature, and passes the notification and each of its events to the application.

import (
	"context"
	"log"

	"github.com/unione-repo/unione-go-sdk/api/webhook"
	unionewrapper "github.com/unione-repo/unione-nethttp"
)

cfg := unionewrapper.ConfigFromEnv()
handler := unionewrapper.WebhookHandler(cfg.SigningKey(), unionewrapper.Handlers{
	OnEvent: func(ctx context.Context, e *webhook.WebhookCallbackEventsByUserItemEventsItem, user *webhook.WebhookCallbackEventsByUserItem) error {
		if e.EventData != nil {
			log.Printf("UniOne event %s: %s %s", e.EventName, e.EventData.Email, e.EventData.Status)
		}
		return nil
	},
})

mux.Handle(cfg.Path(), handler)           // net/http, chi: r.Handle(cfg.Path(), handler)
router.Any(cfg.Path(), gin.WrapH(handler)) // gin
e.Any(cfg.Path(), echo.WrapHandler(handler)) // echo

Register the URL of the endpoint with webhook/set, e.g. https://example.com/unione/webhook.

Answer 200 within 3 seconds and process the events in the background (a queue): otherwise UniOne repeats the request every 10 minutes for 24 hours (retry_count grows), and a handler that failed every request for at least 10 events of a day is turned inactive. The handler must take at least 5 parallel requests of up to 100 events each; deduplicate, a repeat delivers the same events again.

API client

Every section of the API is available on the client, see the SDK docs.

import (
	"fmt"
	"net/http"

	unionewrapper "github.com/unione-repo/unione-nethttp"
)

cfg := unionewrapper.ConfigFromEnv()
client := cfg.Client()

mux := http.NewServeMux()
mux.HandleFunc("/usage", func(w http.ResponseWriter, r *http.Request) {
	info, err := unionewrapper.ClientFromContext(r.Context()).System.Info(r.Context())
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadGateway)
		return
	}
	fmt.Fprintln(w, info.Email)
})
http.ListenAndServe(":8080", unionewrapper.ClientMiddleware(client)(mux))
Sending through the API

The client calls email/send with all of its options: templates, substitutions, metadata, tracking. Fields of the request and other examples: Go SDK docs.

import (
	"net/http"

	unione "github.com/unione-repo/unione-go-sdk"
	"github.com/unione-repo/unione-go-sdk/api/email"
	"github.com/unione-repo/unione-go-sdk/common"
	unionewrapper "github.com/unione-repo/unione-nethttp"
)

mux.HandleFunc("/send", func(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	client := unionewrapper.ClientFromContext(ctx)

	if _, err := client.Email.Send(ctx, &email.EmailSendRequest{
		Message: &email.MessageObject{
			Recipients: []email.MessageObjectRecipientsItem{email.RecipientFromAddress("Alex <alex@example.com>")},
			FromEmail:  unione.String("shop@example.com"),
			FromName:   unione.String("Shop"),
			Subject:    "Your order",
			Body:       common.HTMLBody("<p>Hello, {{to_name}}! Your order has shipped.</p>"),
		},
	}); err != nil {
		http.Error(w, err.Error(), http.StatusBadGateway)
		return
	}
})

No domain yet: the test sandbox domain

Until your domain is verified, send from the sandbox domain: a test domain every account has, already verified, with DKIM and SPF set. It delivers only to addresses you confirm (the confirmation link comes by email, valid 48 hours), up to 100 emails a day. The sandbox domain is deleted 31 days after it was created, but you can get it again: the next domain/get-sandbox-domain call creates a new one (with a new name, so read it from the call, do not hardcode it).

The examples use the API client of the package (see the configuration above).

import (
	"net/http"

	unione "github.com/unione-repo/unione-go-sdk"
	"github.com/unione-repo/unione-go-sdk/api/confirmedemail"
	"github.com/unione-repo/unione-go-sdk/api/email"
	"github.com/unione-repo/unione-go-sdk/common"
	unionewrapper "github.com/unione-repo/unione-nethttp"
)

mux.HandleFunc("/sandbox", func(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	client := unionewrapper.ClientFromContext(ctx)

	// 1. Your sandbox domain: created on the first call, the same one afterwards
	sandbox, err := client.Domain.GetSandboxDomain(ctx)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadGateway)
		return
	}

	// 2. Confirm your inbox: UniOne sends a link, click it within 48 hours
	confirm := &confirmedemail.ConfirmedEmailRequest{Email: "you@example.com"}
	if _, err := client.ConfirmedEmail.Create(ctx, confirm); err != nil {
		http.Error(w, err.Error(), http.StatusBadGateway)
		return
	}

	// 3. Send from any address on the sandbox domain to the confirmed one
	if _, err := client.Email.Send(ctx, &email.EmailSendRequest{
		Message: &email.MessageObject{
			Recipients: []email.MessageObjectRecipientsItem{email.RecipientFromAddress("you@example.com")},
			FromEmail:  unione.String("noreply@" + sandbox.Domain),
			Subject:    "Hello from UniOne",
			Body:       common.HTMLBody("<p>It works!</p>"),
		},
	}); err != nil {
		http.Error(w, err.Error(), http.StatusBadGateway)
		return
	}
})
Sandbox limits
  • Sends only to confirmed addresses: up to 10 per account.
  • Up to 100 emails a day.
  • The sandbox domain expires after 31 days; call domain/get-sandbox-domain again to get a new one.
  • Works only with the account API key, not a project key.
  • Not listed in domain/list; the other domain/* methods do not apply to it.

The sandbox is for development and testing. Do not use a sandbox domain as the sender in production.

The same with curl
BASE=https://api.unione.io/en/transactional/api/v1

curl -s -X POST $BASE/domain/get-sandbox-domain.json \
  -H "Content-Type: application/json" -H "X-API-KEY: $UNIONE_API_KEY" -d '{}'

curl -s -X POST $BASE/confirmed-email/create.json \
  -H "Content-Type: application/json" -H "X-API-KEY: $UNIONE_API_KEY" \
  -d '{"email": "you@example.com"}'

curl -s -X POST $BASE/email/send.json \
  -H "Content-Type: application/json" -H "X-API-KEY: $UNIONE_API_KEY" \
  -d '{
        "message": {
          "recipients": [
            {
              "email": "you@example.com"
            }
          ],
          "from_email": "noreply@<sandbox-domain>",
          "subject": "Hello from UniOne",
          "body": {
            "html": "<p>It works!</p>"
          }
        }
      }'

Going to production

1. Verify your sending domain. domain/get-dns-records returns the verification record, the DKIM key and the DMARC target; add the records at your DNS provider, then domain/validate-verification-record and domain/validate-dkim.

Type Host Value Where it comes from
TXT @ unione-validate-hash=… verification-record of the response, as is
TXT <selector>._domainkey (the selector is on the domain page of the control panel, e.g. us) v=DKIM1; k=rsa; p=<dkim> dkim of the response after v=DKIM1; k=rsa; p=
TXT @ v=spf1 include:spf.unione.io ~all not in the response; add the include to your SPF record if you have one
CNAME _dmarc <domain>.dmarc.… dmarc of the response (UniOne publishes the DMARC policy); keep your own DMARC TXT record instead if the domain has one

2. Make sending reliable.

Retries 429 and 5xx with backoff and the same idempotence_key; fix other errors instead of retrying
Limits 500 recipients and 10 MB per request, attachments included: base64 makes a file about a third larger
Body always html or plaintext (a template can supply it)
Delivery status a webhook, not polling
Queue send from a background job, not inside the web request
Config from_email from config, so the sandbox → production switch needs no code change
Defaults keep the unsubscribe footer and tracking; changing them needs UniOne support

Help

Documentation

Overview

Package unionewrapper plugs the UniOne Go SDK into net/http applications: the client configured from the environment, a middleware that puts it into the request context and the webhook endpoint.

Index

Constants

View Source
const (
	EnvAPIKey      = "UNIONE_API_KEY"
	EnvHost        = "UNIONE_HOST"
	EnvTimeout     = "UNIONE_TIMEOUT"
	EnvWebhookKey  = "UNIONE_WEBHOOK_KEY"
	EnvWebhookPath = "UNIONE_WEBHOOK_PATH"
)

Environment variables read by ConfigFromEnv.

View Source
const DefaultWebhookPath = "/unione/webhook"

DefaultWebhookPath is the path of the webhook endpoint when WebhookPath is empty.

View Source
const MaxWebhookBody = 16 << 20

MaxWebhookBody is the largest webhook request body accepted, bytes.

View Source
const Platform = "nethttp.1.2.4"

Platform is the package and its version in the "platform" query parameter of the requests, before the SDK one.

View Source
const SignaturePlaceholder = "00000000000000000000000000000000"

SignaturePlaceholder is the "auth" value Sign replaces.

Variables

View Source
var ErrNoAPIKey = errors.New("unionewrapper: " + EnvAPIKey + " is not set")

ErrNoAPIKey is returned by NewClientFromEnv when UNIONE_API_KEY is not set.

Functions

func ClientFromContext

func ClientFromContext(ctx context.Context) *unione.Client

ClientFromContext returns the API client of ClientMiddleware, nil when there is none.

func ClientMiddleware

func ClientMiddleware(c *unione.Client) func(http.Handler) http.Handler

ClientMiddleware puts the API client into the context of every request.

func NewClientFromEnv

func NewClientFromEnv() (*unione.Client, error)

NewClientFromEnv returns the API client configured from the environment.

func Sign

func Sign(body []byte, apiKey string) []byte

Sign signs a body whose "auth" is SignaturePlaceholder, e.g. for tests.

func VerifySignature

func VerifySignature(body []byte, apiKey string) bool

VerifySignature checks the "auth" field of a webhook notification: the MD5 of the request body in which the "auth" value is replaced by the API key of the user/project of the webhook.

func WebhookHandler

func WebhookHandler(key string, h Handlers) http.Handler

WebhookHandler is the webhook endpoint: GET/HEAD answer 200 (the URL check of webhook/set), POST checks the signature with key (401 when wrong), parses the notification and calls the handlers.

func WithClient

func WithClient(ctx context.Context, c *unione.Client) context.Context

WithClient returns a copy of ctx with the API client.

Types

type Config

type Config struct {
	// APIKey is the user or project API key.
	APIKey string
	// Host is an API host (datacenter) or a full base URL; empty: the default host.
	Host string
	// Timeout of the requests; 0: 30 seconds.
	Timeout time.Duration
	// WebhookKey is the API key the webhooks are signed with; empty: APIKey.
	WebhookKey string
	// WebhookPath is the path of the webhook endpoint; empty: DefaultWebhookPath.
	WebhookPath string
}

Config holds the settings of the client and of the webhook endpoint.

func ConfigFromEnv

func ConfigFromEnv() Config

ConfigFromEnv reads the configuration from the UNIONE_* environment variables (UNIONE_TIMEOUT is in seconds).

func (Config) Client

func (c Config) Client() *unione.Client

Client returns the API client for the configuration.

func (Config) Path

func (c Config) Path() string

Path is the path of the webhook endpoint: WebhookPath or DefaultWebhookPath.

func (Config) SigningKey

func (c Config) SigningKey() string

SigningKey is the key the webhooks are checked with: WebhookKey or APIKey.

type Handlers

type Handlers struct {
	// OnNotification gets the whole notification.
	OnNotification func(ctx context.Context, notification *webhook.WebhookCallback) error
	// OnEvent gets each event of the notification (email status, spam block...) with its user/project.
	OnEvent func(ctx context.Context, event *webhook.WebhookCallbackEventsByUserItemEventsItem, user *webhook.WebhookCallbackEventsByUserItem) error
}

Handlers receive the webhook notifications with a valid signature. Both are optional; an error answers 500, so the API repeats the notification later. They must be fast: the API waits for 200 OK for 3 seconds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL