Help us shape the future of Go! Take the 2026 Go Developer Survey

webhooks

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Generated by codegen. Do not edit by hand. Package webhooks verifies the signature ZelfAI puts on every webhook delivery.

Index

Constants

View Source
const (
	SignatureHeader = "Webhook-Signature"
	TimestampHeader = "Webhook-Timestamp"
	IDHeader        = "Webhook-Id"

	// DefaultTolerance bounds how far a delivery's timestamp may sit from now
	// before it is treated as a replay.
	DefaultTolerance = 5 * time.Minute
)

Variables

View Source
var ErrVerification = errors.New("webhook verification failed")

ErrVerification is returned for every failed verification. Callers should answer a failed verification with 400 and never process the body.

Functions

func Verify

func Verify(payload []byte, signature string, secrets []string, opts Options) (map[string]any, error)

Verify checks a delivery against one or more signing secrets and returns the decoded payload.

payload must be the exact bytes received: re-encoding the JSON changes the bytes and the signature will not match.

A rotation switches at once: the API signs the next delivery with the new secret only and rejects the old one immediately. A list of secrets is accepted, so both can be held during a rollout.

Types

type DownloadOptions

type DownloadOptions struct {
	HTTPClient *http.Client
	// SkipChecksum turns off the SHA-256 check over the downloaded body.
	SkipChecksum bool
}

DownloadOptions tunes Rows. The zero value uses http.DefaultClient and verifies the published checksum.

type Event

type Event struct {
	// Raw is the payload exactly as it arrived, including any field this SDK
	// predates.
	Raw map[string]any

	ID             string
	Type           EventType
	SubscriptionID string
	DeliveredAt    time.Time
	Livemode       bool
	Resource       string
	DeliveryMode   string
	ItemCount      int
	Files          []File
	Items          []map[string]any
	Statistics     map[string]any
}

Event is a delivery whose signature has already been checked.

func Parse

func Parse(payload []byte, signature string, secrets []string, opts Options) (*Event, error)

Parse verifies a delivery and returns it as a typed Event.

func (*Event) Rows

func (e *Event) Rows(ctx context.Context, visit func(row map[string]any) error, opts DownloadOptions) error

Rows downloads the manifest files and calls visit for each decoded row.

Each DownloadURL is signed and expires, so fetch soon after delivery. Unless SkipChecksum is set, the published SHA-256 is recomputed over the body, and a mismatch or a missing digest stops the walk before any row is visited.

type EventType

type EventType string

EventType is an event the API can deliver, one per `webhooks` declaration in the spec.

const (
	// EventAIAnalysisCompleted Fires when an AI analysis run reaches a terminal state (COMPLETED or FAILED)
	EventAIAnalysisCompleted EventType = "ai_analysis.completed"
	// EventContentSnapshot Scheduled snapshot of the content set that a subscription matches
	EventContentSnapshot EventType = "content.snapshot"
	// EventCreatorSnapshot Scheduled snapshot of the creator set that a subscription matches
	EventCreatorSnapshot EventType = "creator.snapshot"
	// EventCreatorVettingCompleted Fires when a creator vetting analysis run reaches a terminal state...
	EventCreatorVettingCompleted EventType = "creator_vetting.completed"
)

func (EventType) String

func (e EventType) String() string

String returns the wire value.

func (EventType) Valid

func (e EventType) Valid() bool

Valid reports whether the API declares this event type.

type File

type File struct {
	Format      string `json:"format"`
	ExpiresAt   int64  `json:"expires_at"`
	SHA256      string `json:"sha256"`
	SizeBytes   int64  `json:"size_bytes"`
	ItemCount   int    `json:"item_count"`
	DownloadURL string `json:"download_url"`
}

File is one downloadable snapshot file in a manifest delivery.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler verifies a delivery once, then routes it to the listener for its type. It is framework agnostic: hand it the raw body and the signature header from whatever server is in front of it.

func NewHandler

func NewHandler(secrets []string, opts Options) *Handler

NewHandler builds a Handler for the given signing secrets. A rotation switches at once, so hold both only for as long as the rollout takes.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, payload []byte, signature string) (*Event, error)

Handle verifies, then calls every listener registered for the event type. It returns before any listener runs if the signature, timestamp, or body does not check out.

func (*Handler) On

func (h *Handler) On(listener Listener, eventTypes ...EventType) *Handler

On registers a listener for one or more event types. It panics on an unknown type, since that is a wiring mistake rather than a runtime condition.

func (*Handler) OnAny

func (h *Handler) OnAny(listener Listener) *Handler

OnAny registers the listener used when no type specific one matched.

type Listener

type Listener func(ctx context.Context, event *Event) error

Listener handles one verified delivery.

type Options

type Options struct {
	Tolerance time.Duration
	Now       time.Time
}

Options tunes verification. The zero value uses DefaultTolerance and the current wall clock.

Jump to

Keyboard shortcuts

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