X402.Plug.PaymentGate (X402 v0.3.3)

Copy Markdown View Source

Plug middleware that gates configured routes behind x402 payment verification.

For matching routes, requests without an x-payment header receive a 402 response with x402 payment requirements. Requests with x-payment are decoded, verified, and settled with the configured facilitator before the request is allowed to continue through the plug pipeline.

Summary

Types

Configuration map produced by init/1.

Functions

Gates matching requests behind x402 payment verification.

Validates and compiles X402.Plug.PaymentGate options.

Types

options()

@type options() :: %{
  facilitator: X402.Facilitator.server(),
  hooks: module(),
  payment_identifier_cache:
    X402.Extensions.PaymentIdentifier.ETSCache.server() | nil,
  routes: [compiled_route()]
}

Configuration map produced by init/1.

Functions

call(conn, map)

(since 0.1.0)
@spec call(Plug.Conn.t(), options()) :: Plug.Conn.t()

Gates matching requests behind x402 payment verification.

init(opts)

(since 0.1.0)
@spec init(keyword()) :: options()

Validates and compiles X402.Plug.PaymentGate options.

Options

  • :facilitator (term/0) - Facilitator server pid/name used for verification and settlement. The default value is X402.Facilitator.

  • :hooks - Lifecycle hook module implementing X402.Hooks. The default value is X402.Hooks.Default.

  • :payment_identifier_cache - Optional ETSCache server pid/name used for idempotency. When set, the plug performs an atomic claim (via put_new) on the payment proof hash before settling, preventing concurrent requests from double-settling the same payment. The default value is nil.

  • :routes (list of map/0) - Required. Route gate definitions.