Custom code to support the x402 payment protocol in the SDK fetch/websockets#4
Open
rishabluthra wants to merge 5 commits intoagentmail-to:mainfrom
Open
Custom code to support the x402 payment protocol in the SDK fetch/websockets#4rishabluthra wants to merge 5 commits intoagentmail-to:mainfrom
rishabluthra wants to merge 5 commits intoagentmail-to:mainfrom
Conversation
Extends the Fern-generated SDK with wrapper classes that automatically handle x402 (402 Payment Required) flows when an x402Client is passed. HTTP requests use wrapped fetch; WebSocket connections probe for payment requirements and connect with signed payment headers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Users pass `wrapFetchWithPayment(fetch, x402)` via the existing `fetch` option for HTTP. The SDK wrapper now only handles WebSocket x402 probing, removing the lazy fetch init and X402Initialized abstraction. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The wrapper now calls wrapFetchWithPayment internally so users only need to pass x402 once. No separate fetch override needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Users no longer need to pass apiKey: "" when using x402. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds built-in support for the x402 payment protocol so HTTP API calls and WebSocket connections automatically handle
402 Payment Requiredresponses.x402ClienttoAgentMailClient— all API calls and WebSocket connections are payment-aware with zero extra code@x402/fetchis dynamically imported only whenx402option is providedsrc/wrapper/extend generated classes,protected via
.fernignoreUsage
HTTP
All REST API calls automatically handle 402 responses — the SDK intercepts the payment requirement, signs a USDC payment, and retries the request with a
PAYMENT-SIGNATUREheader.
WebSocket
WebSocket endpoints don't natively support 402 responses, so the SDK probes the endpoint over HTTP first, signs the payment, then opens the WebSocket with the payment headers.
Without x402
Existing usage is unchanged — if you don't pass x402, the client behaves exactly as before.
Peer dependencies
When using x402, install the relevant packages:
Summary by cubic
Adds built-in x402 payment support for both HTTP and WebSockets using a single x402 option on AgentMailClient. All 402 flows are handled automatically; existing usage is unchanged if you don’t pass x402.
New Features
Dependencies
Written for commit d957f2c. Summary will update on new commits.