Skip to content

Conversation

@CarsonRoscoe
Copy link
Contributor

@CarsonRoscoe CarsonRoscoe commented Dec 1, 2025

x402 v2 Development

This branch represents the in-progress implementation of the x402 v2 Specification, as well as a total rewrite of the Typescript and Go packages.
Community contributions are welcome. Please PR directly into this branch as we swarm on reference implementations.

(This PR is a continuation of #482 with a cleaned up commit history)


Overview

x402 v2 introduces a modular architecture separating the specification, facilitator, and SDK layers.
This branch is dedicated to building the reference SDKs as we build out v2.

  • All legacy code has been moved down a folder into a /legacy subfolder. The legacy typescript packages can be found in /typescript/packages/legacy, the legacy go package in /go/legacy, examples in /examples/typescript/legacy, e2e tests in /e2e/legacy, etc.
  • The top-level typescript, go, examples and e2e folders are current.
  • Python remains unchanged, and will be updated in a fast-follow.
  • Java is being deprecated until the community chooses to update it to v2.

TypeScript Packages

Package Description
@x402/core Core primitives — mechanism and transport agnostic
@x402/evm Ethereum implementation of the exact scheme
@x402/svm Solana implementation of the exact scheme
@x402/extensions Optional extensions (bazaar discovery, sign-in-with-x) — tree-shakable
@x402/express Express server middleware
@x402/hono Hono server middleware
@x402/next Next.js server middleware
@x402/fetch Fetch API client interceptor
@x402/axios Axios client interceptor
@x402/paywall Paywall UI components (opt-in install)

Go Packages

Package Description
x402 Core primitives — X402Client, X402ResourceServer, X402Facilitator
x402/http HTTP transport layer (client wrapper, server integration)
x402/http/gin Gin server middleware
x402/mechanisms/evm Ethereum exact scheme (client, server, facilitator)
x402/mechanisms/svm Solana exact scheme (client, server, facilitator)
x402/signers/evm EVM signer helpers
x402/signers/svm SVM signer helpers
x402/extensions Protocol extensions (bazaar discovery)

Python Packages

Will be added shortly


Testing

The v2 implementation includes unit tests, integration tests, and e2e tests.

Running Tests

TypeScript:

cd typescript
pnpm test              # Unit tests
pnpm test:integration  # Integration tests

Go:

cd go
make test              # Unit tests
make test-integration  # Integration tests

E2E (all languages):

cd e2e
pnpm test              # Run e2e tests
pnpm test -v           # Verbose logging
pnpm test --min        # Minimize redundant tests, maximize coverage

Usage Patterns

TypeScript and Go follow nearly identical usage patterns. The examples below use TypeScript for demonstration purposes.

Client (TypeScript)

The x402Client uses a builder pattern to register payment schemes:

import { x402Client } from "@x402/fetch";  // or @x402/axios
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { ExactSvmScheme } from "@x402/svm/exact/client";

// Builder pattern with chaining
const client = new x402Client()
  .register("eip155:*", new ExactEvmScheme(evmSigner))      // All EVM chains
  .register("eip155:1", new ExactEvmScheme(ethereumSigner)) // Override for Ethereum
  .register("solana:*", new ExactSvmScheme(svmSigner))      // All Solana networks
  .registerPolicy((version, requirements) => requirements)  // Filter payment options
  .onBeforePaymentCreation(async (context) => {            // Validation hooks
    // Custom logic before payment creation
  })
  .onAfterPaymentCreation(async (context) => {             // Success hooks
    // Logging, metrics, analytics
  });

// Wrap native fetch with payment handling
const fetchWithPayment = wrapFetchWithPayment(fetch, client);

// Use like normal fetch - payments handled automatically
const response = await fetchWithPayment("https://api.example.com/data");
const data = await response.json();

Server (TypeScript)

The x402ResourceServer uses a builder pattern to register verification schemes:

import { x402ResourceServer, paymentMiddleware } from "@x402/express"; // or @x402/hono, @x402/next
import { HTTPFacilitatorClient } from "@x402/core/server";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { ExactSvmScheme } from "@x402/svm/exact/server";

// Builder pattern with chaining
const facilitatorClient = new HTTPFacilitatorClient({ url: facilitatorUrl }); // or array of facilitator clients
const server = new x402ResourceServer(facilitatorClient)
  .register("eip155:*", new ExactEvmScheme())      // All EVM chains
  .register("solana:*", new ExactSvmScheme())      // All Solana networks
  .onBeforeVerify(async (context) => {            // Verification hooks
    // Custom validation before verifying payment
  })
  .onAfterSettle(async (context) => {             // Settlement hooks
    // Logging, database updates after settlement
  });

// Use with Express middleware
app.use(paymentMiddleware({
  "GET /weather": {
    accepts: { scheme: "exact", price: "$0.001", network: "eip155:84532", payTo: evmAddress },
    description: "Weather data",
  },
}, server));

Facilitator (TypeScript)

The x402Facilitator verifies and settles payments on-chain:

import { x402Facilitator } from "@x402/core/facilitator";
import { ExactEvmScheme } from "@x402/evm/exact/facilitator";
import { ExactSvmScheme } from "@x402/svm/exact/facilitator";

// Builder pattern with chaining
const facilitator = new x402Facilitator()
  .register("eip155:84532", new ExactEvmScheme(evmSigner))
  .register("solana:*", new ExactSvmScheme(svmSigner));

// Add lifecycle hooks
facilitator
  .onBeforeVerify(async (context) => {
    // Pre-verification checks (rate limiting, fraud detection)
  })
  .onAfterSettle(async (context) => {
    // Post-settlement actions (cleanup, notifications)
  });

// Expose via HTTP endpoints
app.post("/verify", async (req, res) => {
  const result = await facilitator.verify(req.body.paymentPayload, req.body.paymentRequirements);
  res.json(result);
});

app.post("/settle", async (req, res) => {
  const result = await facilitator.settle(req.body.paymentPayload, req.body.paymentRequirements);
  res.json(result);
});

Backwards Compatibility

The EVM and SVM mechanism packages export v1 schemes from a /v1/ subpath for backwards compatibility with legacy x402 servers and clients. These must be registered using .registerV1().

Client:

import { ExactEvmSchemeV1 } from "@x402/evm/exact/v1/client";
import { ExactSvmSchemeV1 } from "@x402/svm/exact/v1/client";

const client = new x402Client()
  .registerV1("base-sepolia", new ExactEvmSchemeV1(evmSigner))
  .registerV1("solana-devnet", new ExactSvmSchemeV1(svmSigner));

Facilitator:

import { ExactEvmSchemeV1 } from "@x402/evm/exact/v1/facilitator";
import { ExactSvmSchemeV1 } from "@x402/svm/exact/v1/facilitator";

const facilitator = new x402Facilitator()
  .registerV1("base-sepolia", new ExactEvmSchemeV1(evmSigner))
  .registerV1("solana-devnet", new ExactSvmSchemeV1(svmSigner));

@vercel
Copy link

vercel bot commented Dec 1, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
x402 Ready Ready Preview Comment Dec 11, 2025 3:35pm

@cb-heimdall
Copy link

cb-heimdall commented Dec 1, 2025

✅ Heimdall Review Status

Requirement Status More Info
Reviews 1/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 0
Sum 1

@CarsonRoscoe CarsonRoscoe changed the title WIP: Development v2 Development v2 Dec 1, 2025
Copy link
Contributor

@notorious-d-e-v notorious-d-e-v left a comment

Choose a reason for hiding this comment

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

Nice work @CarsonRoscoe -- huge lift!

If it would be helpful, I'm happy to implement the changes that come out of this review.

Would love to get some more eyes on it too @amilz @alexanderguy @GuiBibeau

* go settlement failure and initOnStart fixes

* update go unit tests

* update readmes
* add multiple payment options for go

* fix SyncFacilitatorOnStart
* add hooks for advanced go example

* fix legacy ts deps
* add miniapp example

* fix format

* add mcp example

* fix format
* add manifest to miniapp

* fix format
* port new website to public repo

* chore: update pnpm-lock.yaml with motion dependency

* feat: add x402 V2 launch blog post and update hero code snippet

- Add /writing/x402-v2-launch blog post page
- Add blog images (blog_intro.png, blog_x402_layers.png, homepage_build5.png)
- Enable Writing nav link in NavBar and Footer
- Update hero code snippet to show V2 SDK usage
- Fix CodeSnippet to support multi-line code

* feat: edit nav for mobile

* Revert "feat: edit nav for mobile"

This reverts commit 475999a.

* feat: add ecosystem page and minor home page tweaks

* feat: fix typography in ecosystem

* homepage qa fixes

* feat: add ecosystem icons and fix halftone

* cleanup assets

* fix: minor bug on faq

* feat: add hamburger menu to mobile
Copy link

@avidreder avidreder left a comment

Choose a reason for hiding this comment

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

LGTM

@CarsonRoscoe CarsonRoscoe merged commit 1a79fea into main Dec 11, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

9 participants