Skip to main content

RouterClient: TypeScript API Reference

RouterClient is the SELAT Router SDK entry point for TypeScript applications that need fetch-like access to paid endpoints.
RouterClient sends a request through SELAT Router, returns an unpaid upstream response unchanged when no payment is required, and completes one signed payment replay only when the router returns a valid 402 Payment Required challenge. It supports a private key or any SDK-compatible PaymentSigner. 1

Constructor

The chain option is required. You must supply a signer or a privateKey; when a signer is supplied, it is used as the signing implementation. 1

Send a paid request with fetch

client.fetch(input, init?) accepts a full target URL and a fetch-compatible request initializer. It returns a standard Promise<Response>, so applications can use familiar status, header, and body handling. 1

Request lifecycle

The payment flow consists of a maximum of two router requests for one fetch call. The SDK does not sign or replay ordinary non-402 responses. 1 Each of the two router request legs receives its own requestTimeoutMs window. Set a value that accommodates your expected upstream latency, but use an AbortSignal as well when the caller needs independent cancellation. 1

Configure request options

RouterFetchOptions extends the standard RequestInit options and adds preferProtocol. Use normal fetch options for HTTP method, headers, request body, and cancellation. 1

POST example

Protocol preference is a hint. Choose x402 or mpp only when your integration needs to express a rail preference; the default is mpp. 1

Bind a base URL with createFetch

Use createFetch({ baseUrl }) to create a fetch-like function for a known upstream. It resolves each relative path against the base URL, then routes the resulting absolute URL through the same payment flow. 1
This pattern keeps endpoint call sites concise while retaining the same signer, timeout, headers, and protocol configuration.

Claim and track refunds

Use the quote ID from a completed paid request to create a refund claim. refundClaim(quoteId) returns the new RefundRequest. Use refundQuery(quoteId) to retrieve its latest status. Both methods authenticate the request with the RouterClient signer. 1
RefundRequest includes the claim id, quoteId, requesting wallet, current status, and createdAt timestamp. RefundStatusResult includes the quoteId and status; it can also include chainId and refundTxId when available. A refund status is one of new, pending_review, approved, processing, succeeded, or failed. If the Router rejects the request or cannot return a valid result, either method throws an Error whose message includes the HTTP status and the returned detail. Handle this separately from payment-challenge parsing errors.

Handle SDK errors

The SDK exports a base error type and two specialized errors. Catch the most specific type that helps your application provide a useful recovery path. 1

Select a signer and chain

Use the Signers guide to choose a private key, Circle Agent Wallet, HTTP remote signer, custom remote signer, or Circle Developer-Controlled Wallet. The chain must match a payment option offered by the returned challenge and the wallet’s available Gateway balance. See Chains for the fund chains and the outbound settlement rails; for the exact set a specific runtime accepts, query its payment tooling. 1 For the first complete request, continue to the SDK Quickstart. For source examples, see SDK Examples.

References