Skip to content

Add x402 payment protocol support#27

Merged
Benjpoirier merged 7 commits into
mainfrom
feat/x402-payment-protocol-support
Mar 19, 2026
Merged

Add x402 payment protocol support#27
Benjpoirier merged 7 commits into
mainfrom
feat/x402-payment-protocol-support

Conversation

@Benjpoirier
Copy link
Copy Markdown
Contributor

@Benjpoirier Benjpoirier commented Mar 17, 2026

Summary

  • Add support for the X402 payment protocol, allowing clients to
    automatically handle HTTP 402 responses by signing and retrying requests with on-chain
    payments
  • Introduce X402Signer abstraction and createX402Signer helper that wires up viem +
    @x402/evm + @x402/core for Base chain EVM payments
  • Add LinkupPaymentRequiredError for cases where a 402 is received but cannot be
    handled (e.g. missing header, already retried)
  • Add Axios response interceptor that transparently decodes payment-required headers,
    signs payment payloads, and retries the original request

Test plan

  • Unit tests for createX402Signer (account creation, public client setup, EVM
    signer wiring, chain registration, payload delegation, error propagation) — 100% coverage
  • Unit tests for the X402 interceptor flow (402 → sign → retry → success, missing
    header, already-retried guard, non-402 passthrough, network errors)
  • Unit tests for LinkupPaymentRequiredError
  • Manual end-to-end testing on testnet with a test wallet

Usage

const signer = createX402Signer({ privateKey: '0x...' });
const client = new LinkupClient({ signer });

const result = await client.search({
  query: 'What is x402?',
  depth: 'standard',
  outputType: 'sourcedAnswer',
});

@Benjpoirier Benjpoirier force-pushed the feat/x402-payment-protocol-support branch from 29db020 to 5c9a72f Compare March 17, 2026 16:38
@Benjpoirier Benjpoirier force-pushed the feat/x402-payment-protocol-support branch from 5c9a72f to 47fe838 Compare March 18, 2026 08:34
@Benjpoirier Benjpoirier force-pushed the feat/x402-payment-protocol-support branch from 0137feb to 9cfd75d Compare March 18, 2026 16:08
Comment thread src/x402/__tests__/create-x402-signer.test.ts Outdated
Comment thread src/x402/create-x402-signer.ts Outdated
Comment thread x402-core.d.ts
Comment thread src/x402/types.ts
@@ -0,0 +1,3 @@
export interface X402Signer {
createPaymentPayload(paymentRequired: unknown): Promise<unknown>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unknown -> Promise. Can't we do better here or not ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't type this better without changing the project's moduleResolution.
With "node", TypeScript can't resolve @x402/core subpath exports (@x402/core/types), so we can't import PaymentRequired / PaymentPayload directly. Keeping unknown for now — we'll type it properly when we migrate to moduleResolution: "node16" or "bundler

Comment thread src/types.ts Outdated
Comment thread src/types.ts Outdated
Comment thread package.json
"@commitlint/config-conventional": "^19.8.1",
"@types/jest": "^29.5.14",
"@types/node": "^24.0.4",
"@x402/evm": "^2.7.0",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you missed @x402/core also in devDependencies no ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Comment thread src/linkup-client.ts Outdated
Comment thread src/linkup-client.ts Outdated
Comment thread src/linkup-client.ts Outdated
Comment thread src/linkup-client.ts Outdated
@Benjpoirier Benjpoirier requested a review from Shedna March 19, 2026 13:38
@Benjpoirier Benjpoirier merged commit 2365190 into main Mar 19, 2026
3 checks passed
@Benjpoirier Benjpoirier deleted the feat/x402-payment-protocol-support branch March 19, 2026 15:53
@Shedna
Copy link
Copy Markdown
Contributor

Shedna commented May 20, 2026

🎉 This PR is included in version 3.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants