glassnode-api - v0.29.3
    Preparing search index...

    Function createX402Fetch

    • Create an x402-capable fetch for paid Glassnode calls (Node-first).

      Dynamically loads the optional peer deps @x402/fetch + @x402/evm; pass the result as the fetch option of GlassnodeAPI together with x402: true.

      Errors:

      • Invalid maxPaymentPerCall → rejects with GlassnodeInputError (argument: 'maxPaymentPerCall').
      • Optional peer deps not installed → rejects with GlassnodeConfigError (import error on .cause).

      The returned fetch rejects with a GlassnodePaymentError (surfaced as-is and never retried by GlassnodeAPI) when:

      • the payment layer fails before a paid request is sent — e.g. the server's price is above maxPaymentPerCall, the signer throws, or the 402 carries no usable payment requirements (paymentMayHaveSettled: false: nothing was paid);
      • the call fails after a request carrying a signed payment was sent (paymentMayHaveSettled: true). The server may already have settled that payment and a retry would sign a new one, so it is never retried. Either the paid request failed in transit (connection reset, timeout abort: the transport error on .cause, timedOut set for a timeout), or it was answered with a non-2xx status other than 402 — e.g. a proxy 502/504 after the origin settled, a 429, a 400 — (status set, the equivalent GlassnodeApiError on .cause).

      Redirects are never followed: the returned fetch passes redirect: 'manual' (a caller's redirect: 'error' is kept; 'follow' is overridden), because fetch's default 'follow' resends custom headers — the signed PAYMENT-SIGNATURE / X-PAYMENT — to whatever origin a 3xx names, and a redirected unpaid request could be priced (and paid) by the redirect target. A 3xx to the unpaid request is returned as-is (the client reports GlassnodeApiError with the 3xx status, nothing signed); a 3xx to the paid request is a GlassnodePaymentError with status and paymentMayHaveSettled: true, like any other non-2xx after payment.

      A rejection of, or a non-2xx answer to, the unpaid request (no payment signed yet) is passed through unchanged, so the client still reports and retries it (GlassnodeNetworkError, or GlassnodeApiError for 429/5xx). A 402 the server returns to the paid request (it refused the payment) is not an error here either; the client reports it as GlassnodeApiError (status 402), which it never retries.

      Parameters

      Returns Promise<
          {
              (input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
              (input: string | Request | URL, init?: RequestInit): Promise<Response>;
          },
      >