Stripe webhook signature verification (HMAC-SHA256 over ${t}.${raw_body} from the Stripe-Signature header, with a replay window) and webhook endpoint management. See https://docs.stripe.com/webhooks#verify-manually.

Values

DEFAULT_TOLERANCE_SECONDS

DEFAULT_TOLERANCE_SECONDS: Int 300

Functions

construct-event

fn (request: Map): Map
fn (request: Map, webhook-secret: Str): Map
fn (request: Map, webhook-secret: Str, tolerance-seconds: Int): Map

Verify a webhook request (see verify-request) and return the Event it carries, or an Err when the signature is invalid or stale. The Hot-side equivalent of stripe.webhooks.constructEvent. Events are rendered in the webhook endpoint's own API version, which may differ from ::stripe/API_VERSION.

Example

stripe-webhook
meta {webhook: {service: "stripe", path: "/stripe/webhook"}}
fn (request) {
  event ::stripe::webhooks/construct-event(request)
  cond {
    is-err(event) => { ::hot::http/HttpResponse({status: 400, body: "invalid signature"}) }
    eq(event.type, "checkout.session.completed") => {
      // fulfil event.data.object
      ::hot::http/HttpResponse({status: 200, body: {received: true}})
    }
    => { ::hot::http/HttpResponse({status: 200, body: {received: true}}) }
  }
}
Context Vars: stripe.webhook.secret

create-webhook-endpoint

fn (request: CreateWebhookEndpointRequest): Map
fn (request: CreateWebhookEndpointRequest, options: ::stripe::api/RequestOptions | Map): Map

POST /v1/webhook_endpoints - Register a webhook endpoint. The response includes the signing secret (whsec_...), masked in call records — store it; it is only returned on creation. Set api_version: ::stripe/API_VERSION so event payloads match the shapes this package's API calls return; otherwise events use the account's default version.

Example

::webhooks ::stripe::webhooks

endpoint ::webhooks/create-webhook-endpoint(::webhooks/CreateWebhookEndpointRequest({
  url: "https://myapp.hot.dev/stripe/webhook",
  enabled_events: ["payment_intent.succeeded", "checkout.session.completed"],
  api_version: ::stripe/API_VERSION
}))
endpoint.secret  // => "whsec_..." (save as stripe.webhook.secret)

delete-webhook-endpoint

fn (webhook-endpoint: Str): Map
fn (webhook-endpoint: Str, opts: Map): Map

DELETE /v1/webhook_endpoints/{webhook_endpoint} - Delete a webhook endpoint.

Example

::webhooks ::stripe::webhooks

result ::webhooks/delete-webhook-endpoint("we_123")
result.deleted

get-webhook-endpoint

fn (webhook-endpoint: Str): Map
fn (webhook-endpoint: Str, opts: Map): Map

GET /v1/webhook_endpoints/{webhook_endpoint} - Retrieve a webhook endpoint by ID.

Example

::webhooks ::stripe::webhooks

endpoint ::webhooks/get-webhook-endpoint("we_123")
endpoint.status
endpoint.enabled_events

list-webhook-endpoints

fn (): Map
fn (params: Map): Map
fn (params: Map, opts: Map): Map

GET /v1/webhook_endpoints - List your webhook endpoints. Optional params: limit, starting_after, ending_before.

Example

::webhooks ::stripe::webhooks

page ::webhooks/list-webhook-endpoints({limit: 20})
urls map(page.data, %.url)

parse-signature-header

fn (header: Str): Map

Parse a Stripe-Signature header into {t: Str, v1: Vec<Str>}. Multiple v1 entries can be present after a signing secret roll; unknown schemes (e.g. v0) are ignored.

Example

::stripe::webhooks/parse-signature-header("t=1492774577,v1=5257a869,v0=6ffbb59b")  // {t: "1492774577", v1: ["5257a869"]}

update-webhook-endpoint

fn (webhook-endpoint: Str, request: UpdateWebhookEndpointRequest): Map
fn (webhook-endpoint: Str, request: UpdateWebhookEndpointRequest, options: ::stripe::api/RequestOptions | Map): Map

POST /v1/webhook_endpoints/{webhook_endpoint} - Update a webhook endpoint. Set disabled: true to pause deliveries.

Example

::webhooks ::stripe::webhooks

// Pause deliveries
::webhooks/update-webhook-endpoint("we_123", ::webhooks/UpdateWebhookEndpointRequest({disabled: true}))

verify-request

fn (request: Map): Bool
fn (request: Map, webhook-secret: Str): Bool
fn (request: Map, webhook-secret: Str, tolerance-seconds: Int): Bool

Verify a Stripe webhook request signature using HMAC-SHA256 over the raw request body (::hot::http/raw-body). Returns true if any v1 signature matches (constant-time comparison) and the signed timestamp is within the tolerance window (default 300 seconds, matching Stripe's official libraries). Pass a tolerance of 0 to skip the timestamp check.

Uses the stripe.webhook.secret context variable (your endpoint's whsec_... signing secret) by default, failing when it is unset; or accepts an explicit secret, masked in call records — an empty explicit secret never verifies.

Always verify before trusting a webhook — unverified requests can be forged payment events.

Example

stripe-webhook
meta {webhook: {service: "stripe", path: "/stripe/webhook"}}
fn (request) {
  if(not(::stripe::webhooks/verify-request(request)),
    ::hot::http/HttpResponse({status: 400, body: "invalid signature"}),
    ::hot::http/HttpResponse({status: 200, body: {received: true}}))
}

// With an explicit signing secret and a custom tolerance in seconds
is-authentic fn (request: Map): Bool {
  ::stripe::webhooks/verify-request(request, "whsec_...", 600)
}
Context Vars: stripe.webhook.secret

Types

CreateWebhookEndpointRequest

CreateWebhookEndpointRequest type {
    url: Str,
    enabled_events: Vec<Str>,
    description: Str?,
    metadata: Map?,
    api_version: Str?,
    connect: Bool?,
    expand: Vec<Str>?
}

UpdateWebhookEndpointRequest

UpdateWebhookEndpointRequest type {
    url: Str?,
    enabled_events: Vec<Str>?,
    description: Str?,
    disabled: Bool?,
    metadata: Map?,
    expand: Vec<Str>?
}