### Quick Start Setup for x402 Python SDK Source: https://github.com/coinbase/x402/blob/main/examples/python/README.md Follow these steps to set up the project and run the main example. Ensure you copy the .env-local file to .env and update it with your private keys. ```bash cd clients/httpx cp .env-local .env # Edit .env with your EVM_PRIVATE_KEY and/or SVM_PRIVATE_KEY uv sync uv run python main.py ``` -------------------------------- ### Setup: Install Dependencies and Build Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/payment-identifier/README.md Commands to install project dependencies and build all packages from the root of the typescript examples. ```bash cd ../.. pnpm install && pnpm build cd clients/payment-identifier ``` -------------------------------- ### Start Next.js Example App Source: https://github.com/coinbase/x402/blob/main/e2e/servers/next/README.md Installs project dependencies and starts the Next.js development server. ```bash pnpm dev ``` -------------------------------- ### Install and Run Server Source: https://github.com/coinbase/x402/blob/main/examples/typescript/servers/offer-receipt/README.md Installs dependencies, builds the project, and starts the offer-receipt server. Ensure you are in the correct directory before running. ```bash cd ../.. pnpm install && pnpm build cd servers/offer-receipt pnpm dev ``` -------------------------------- ### Install Dependencies and Build Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/offer-receipt/README.md Install and build project dependencies before running the client example. Navigate to the project root, install, build, and then change to the client directory. ```bash cd ../../ pnpm install && pnpm build cd clients/offer-receipt ``` -------------------------------- ### Run the Client Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/axios/README.md Command to execute the example client after setup and configuration. ```bash pnpm start ``` -------------------------------- ### Install Dependencies and Build Packages Source: https://github.com/coinbase/x402/blob/main/examples/typescript/README.md Run these commands from the examples/typescript directory to install dependencies and build the X402 packages before running any examples. ```bash pnpm install pnpm build ``` -------------------------------- ### Setup Environment Source: https://github.com/coinbase/x402/blob/main/examples/go/clients/payment-identifier/README.md Copies the example environment file and prompts to fill in necessary variables like EVM private key and server URL. ```bash cp .env-example .env # Fill in the environment variables: # EVM_PRIVATE_KEY - Your Ethereum private key # SERVER_URL - URL of the server (default: http://localhost:4021/order) ``` -------------------------------- ### Install and Build Packages Source: https://github.com/coinbase/x402/blob/main/examples/typescript/fullstack/next/README.md Navigates to the project root, installs dependencies using pnpm, builds all packages, and then changes directory to the Next.js example. ```bash cd ../.. pnpm install && pnpm build cd fullstack/next ``` -------------------------------- ### Start x402 Server Source: https://github.com/coinbase/x402/blob/main/examples/go/clients/advanced/README.md Run this command to start the x402 server, which is required for testing the client examples. ```bash cd ../../servers/gin go run main.go ``` -------------------------------- ### Install and Build Packages Source: https://github.com/coinbase/x402/blob/main/examples/typescript/fullstack/miniapp/README.md Installs and builds all packages from the typescript examples root directory. ```bash cd ../.. pnpm install && pnpm build cd fullstack/miniapp ``` -------------------------------- ### Install Dependencies and Build Source: https://github.com/coinbase/x402/blob/main/examples/typescript/servers/bazaar/README.md Install all project dependencies and build the packages from the typescript examples root directory. ```bash cd ../.. pnpm install && pnpm build cd servers/bazaar ``` -------------------------------- ### Install Dependencies and Run Facilitator Source: https://github.com/coinbase/x402/blob/main/examples/go/facilitator/basic/README.md Download Go module dependencies and start the facilitator server. This command assumes you are in the project's root directory. ```bash go mod download go run . ``` -------------------------------- ### Install and Build Packages Source: https://github.com/coinbase/x402/blob/main/examples/typescript/facilitator/basic/README.md Installs dependencies and builds all packages from the typescript examples root directory. Navigate to the facilitator/basic directory afterwards. ```bash cd ../.. pnpm install && pnpm build cd facilitator/basic ``` -------------------------------- ### Complete X402 Payment Middleware Example Source: https://github.com/coinbase/x402/blob/main/go/http/echo/README.md A comprehensive example demonstrating the setup of the Echo web server with X402 payment middleware, including facilitator client configuration, route definitions with payment options, paywall configuration, and custom handlers. ```go package main import ( "log" "net/http" "time" x402 "github.com/x402-foundation/x402/go" x402http "github.com/x402-foundation/x402/go/http" echomw "github.com/x402-foundation/x402/go/http/echo" evм "github.com/x402-foundation/x402/go/mechanisms/evm/exact/server" "github.com/labstack/echo/v4" ) func main() { e := echo.New() facilitator := x402http.NewHTTPFacilitatorClient(&x402http.FacilitatorConfig{ URL: "https://facilitator.x402.org", }) routes := x402http.RoutesConfig{ "GET /api/data": { Accepts: x402http.PaymentOptions{ { Scheme: "exact", PayTo: "0xYourAddress", Price: "$0.10", Network: "eip155:84532", }, }, Description: "Basic data access", }, "POST /api/compute": { Accepts: x402http.PaymentOptions{ { Scheme: "exact", PayTo: "0xYourAddress", Price: "$1.00", Network: "eip155:8453", }, }, Description: "Compute operation", }, } paywallConfig := &x402http.PaywallConfig{ AppName: "My API Service", AppLogo: "/logo.svg", Testnet: true, } e.Use(echomw.PaymentMiddlewareFromConfig(routes, echowm.WithFacilitatorClient(facilitator), echowm.WithScheme("eip155:*", evm.NewExactEvmScheme()), echowm.WithPaywallConfig(paywallConfig), echowm.WithTimeout(60*time.Second), echowm.WithSettlementHandler(func(c echo.Context, settlement *x402.SettleResponse) { log.Printf("Payment settled: %s", settlement.Transaction) }), )) e.GET("/api/data", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]interface{}{"data": "Protected content"}) }) e.POST("/api/compute", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]interface{}{"result": "Computation complete"}) }) e.GET("/", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]interface{}{"message": "Welcome"}) }) e.Logger.Fatal(e.Start(":8080")) } ``` -------------------------------- ### Quick Setup for Algorand Testnet Source: https://github.com/coinbase/x402/blob/main/typescript/packages/mechanisms/avm/README.md This bash script guides through generating keys, funding accounts with ALGO and USDC on testnet, and setting environment variables for private keys. ```bash # 1. Generate a key (or use an existing one) # AVM_PRIVATE_KEY is a Base64-encoded 64-byte key (seed + pubkey) # 2. Fund accounts with ALGO # Visit https://lora.algokit.io/testnet/fund # 3. Fund accounts with testnet USDC # Visit https://faucet.circle.com/ (select Algorand Testnet) # 4. Set environment variables export AVM_PRIVATE_KEY="" ``` -------------------------------- ### Complete X402 Payment Middleware Setup in Gin Source: https://github.com/coinbase/x402/blob/main/go/http/gin/README.md A full example demonstrating the setup of the X402 payment middleware in a Gin application, including facilitator client configuration, route definitions, and various middleware options. ```go package main import ( "log" "time" x402 "github.com/x402-foundation/x402/go" x402http "github.com/x402-foundation/x402/go/http" ginmw "github.com/x402-foundation/x402/go/http/gin" evmp "github.com/x402-foundation/x402/go/mechanisms/evm/exact/server" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() facilitator := x402http.NewHTTPFacilitatorClient(&x402http.FacilitatorConfig{ URL: "https://facilitator.x402.org", }) routes := x402http.RoutesConfig{ "GET /api/data": { Scheme: "exact", PayTo: "0xYourAddress", Price: "$0.10", Network: "eip155:84532", Description: "Basic data access", }, "POST /api/compute": { Scheme: "exact", PayTo: "0xYourAddress", Price: "$1.00", Network: "eip155:8453", Description: "Compute operation", }, } paywallConfig := &x402http.PaywallConfig{ AppName: "My API Service", AppLogo: "/logo.svg", Testnet: true, } r.Use(ginmw.PaymentMiddlewareFromConfig(routes, ginmw.WithFacilitatorClient(facilitator), ginmw.WithScheme("eip155:*", evmp.NewExactEvmScheme()), ginmw.WithPaywallConfig(paywallConfig), ginmw.WithTimeout(60*time.Second), ginmw.WithSettlementHandler(func(c *gin.Context, settlement *x402.SettleResponse) { log.Printf("✅ Payment settled: %s", settlement.Transaction) }), )) r.GET("/api/data", func(c *gin.Context) { c.JSON(200, gin.H{"data": "Protected content"}) }) r.POST("/api/compute", func(c *gin.Context) { c.JSON(200, gin.H{"result": "Computation complete"}) }) r.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{"message": "Welcome"}) }) r.Run(":8080") } ``` -------------------------------- ### Run Gas Extensions Example Source: https://github.com/coinbase/x402/blob/main/examples/typescript/facilitator/advanced/README.md Starts the facilitator service with gas-sponsoring extensions for exact and upto payment schemes, including EIP-2612 and ERC-20 approval. ```bash pnpm dev:gas-extensions ``` -------------------------------- ### Manual Component Setup Source: https://github.com/coinbase/x402/blob/main/e2e/README.md Manually sets up a specific component, such as the Go facilitators. This involves navigating to the component's directory and running its install and build scripts. ```bash cd facilitators/go bash install.sh bash build.sh ``` -------------------------------- ### Basic Gin Middleware Setup Source: https://github.com/coinbase/x402/blob/main/go/http/gin/README.md Set up the x402 payment middleware in a Gin application using functional options for configuration. This example demonstrates setting up routes, a facilitator client, and registering payment schemes. ```go package main import ( time x402 "github.com/x402-foundation/x402/go" x402http "github.com/x402-foundation/x402/go/http" ginmw "github.com/x402-foundation/x402/go/http/gin" evmp "github.com/x402-foundation/x402/go/mechanisms/evm/exact/server" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() facilitator := x402http.NewHTTPFacilitatorClient(&x402http.FacilitatorConfig{ URL: "https://facilitator.x402.org", }) routes := x402http.RoutesConfig{ "GET /protected": { Scheme: "exact", PayTo: "0xYourAddress", Price: "$0.10", Network: "eip155:84532", Description: "Access to premium content", }, } r.Use(ginmw.PaymentMiddlewareFromConfig(routes, ginmw.WithFacilitatorClient(facilitator), ginmw.WithScheme("eip155:*", evmp.NewExactEvmScheme()), )) r.GET("/protected", func(c *gin.Context) { c.JSON(200, gin.H{"message": "This content is behind a paywall"}) }) r.Run(":8080") } ``` -------------------------------- ### Install x402 Go Package Source: https://github.com/coinbase/x402/blob/main/README.md Install the x402 Go package using the go get command. See the go/ folder for code examples and integration guides. ```shell go get github.com/x402-foundation/x402/go ``` -------------------------------- ### Install x402 Python Package Source: https://github.com/coinbase/x402/blob/main/README.md Install the x402 package for Python using pip. Refer to the python/x402 folder for code examples and integration guides. ```shell pip install x402 ``` -------------------------------- ### Python Flask Server Setup Snippet Source: https://github.com/coinbase/x402/blob/main/docs/getting-started/quickstart-for-sellers.mdx A basic Flask application setup. This snippet is incomplete and serves as a starting point for integrating x402 payment middleware into a Flask application, similar to the FastAPI example. ```python from flask import Flask, jsonify ``` -------------------------------- ### Navigate to httpx example directory Source: https://github.com/coinbase/x402/blob/main/examples/python/clients/README.md Use this command to navigate to the httpx example directory and set up the environment. ```bash cd httpx cp .env-local .env # Edit .env with your EVM_PRIVATE_KEY and/or SVM_PRIVATE_KEY uv sync uv run python main.py ``` -------------------------------- ### Install Dependencies and Build Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/sign-in-with-x/README.md Navigate to the project root, install dependencies using pnpm, and build the project. Then, change the directory to the sign-in-with-x client. ```bash cd ../.. pnpm install && pnpm build cd clients/sign-in-with-x ``` -------------------------------- ### Install and Build Packages Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/advanced/README.md This command installs dependencies and builds all packages from the TypeScript examples root directory. It's a prerequisite for running the advanced client examples. ```bash cd ../.. pnpm install && pnpm build cd clients/advanced ``` -------------------------------- ### Install x402/go/mcp Source: https://github.com/coinbase/x402/blob/main/go/mcp/README.md Install the x402/go/mcp package using the go get command. ```bash go get github.com/x402-foundation/x402/go/mcp ``` -------------------------------- ### Run Go HTTP Client Example Source: https://github.com/coinbase/x402/blob/main/examples/go/servers/echo/README.md Navigates to the HTTP client directory and runs the example client. Ensure the .env file is set up before running. ```bash cd ../../clients/http # Ensure .env is setup go run main.go ``` -------------------------------- ### Setup: Start Payment-Identifier Server Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/payment-identifier/README.md Command to start the payment-identifier server in a separate terminal. ```bash cd ../../servers/payment-identifier pnpm dev ``` -------------------------------- ### Navigate to requests example directory Source: https://github.com/coinbase/x402/blob/main/examples/python/clients/README.md Use this command to navigate to the requests example directory and set up the environment. ```bash cd requests cp .env-local .env # Edit .env with your EVM_PRIVATE_KEY and/or SVM_PRIVATE_KEY uv sync uv run python main.py ``` -------------------------------- ### Run Chatbot Example Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/mcp-chatbot/TOUCHPOINTS.md Commands to install dependencies and run the chatbot example in another terminal. ```bash cd examples/typescript/client/mcp-chatbot pnpm install pnpm dev ``` -------------------------------- ### Run Client Example Source: https://github.com/coinbase/x402/blob/main/examples/python/servers/payment-identifier/README.md Navigate to the client directory and run the payment-identifier client example using uv. ```bash cd ../../clients/payment-identifier uv run python main.py ``` -------------------------------- ### Add Network Example Source: https://github.com/coinbase/x402/blob/main/CONTRIBUTING.md Add your chain to `examples//*/advanced/all_networks` in alphabetic order by network prefix for server, client, and facilitator. ```bash examples//*/advanced/all_networks ``` -------------------------------- ### Run the Example Source: https://github.com/coinbase/x402/blob/main/examples/python/clients/httpx/README.md Execute the main Python script using uv. ```bash uv run python main.py ``` -------------------------------- ### Install and Build Packages Source: https://github.com/coinbase/x402/blob/main/examples/typescript/facilitator/advanced/README.md Installs dependencies and builds all packages from the root of the typescript examples. Navigate to the advanced facilitator directory after completion. ```bash cd ../.. pnpm install && pnpm build cd facilitator/advanced ``` -------------------------------- ### Run x402 Client Example Source: https://github.com/coinbase/x402/blob/main/examples/go/clients/advanced/README.md Execute a specific advanced x402 client example using the 'go run' command. ```bash cd ../../clients/advanced go run . hooks ``` -------------------------------- ### Basic HTTP Client Setup with x402 Source: https://github.com/coinbase/x402/blob/main/go/CLIENT.md Set up a basic HTTP client that automatically handles payments using the x402 package. This involves creating a signer, initializing the x402 client, registering payment schemes, and wrapping the HTTP client. ```go package main import ( "net/http" x402 "github.com/x402-foundation/x402/go" x402http "github.com/x402-foundation/x402/go/http" evm "github.com/x402-foundation/x402/go/mechanisms/evm/exact/client" evmsigners "github.com/x402-foundation/x402/go/signers/evm" ) func main() { // 1. Create signer from private key signer, _ := evmsigners.NewClientSignerFromPrivateKey("0x...") // 2. Create x402 client and register schemes client := x402.Newx402Client(). Register("eip155:*", evm.NewExactEvmScheme(signer, nil)) // 3. Wrap HTTP client httpClient := x402http.WrapHTTPClientWithPayment( http.DefaultClient, x402http.Newx402HTTPClient(client), ) // 4. Make requests - payments handled automatically resp, _ := httpClient.Get("https://api.example.com/data") defer resp.Body.Close() } ``` -------------------------------- ### Start Payment-Identifier Server Source: https://github.com/coinbase/x402/blob/main/examples/python/clients/payment-identifier/README.md Starts the payment-identifier server using `uv` and Python. This server is required for the client example to function correctly. ```bash cd ../../servers/payment-identifier uv run python main.py ``` -------------------------------- ### Set Up Integration Test Environment Source: https://github.com/coinbase/x402/blob/main/go/CONTRIBUTING.md Copies the example environment file to be used for integration tests. ```bash # Set up environment (optional) cp .env.example .env ``` -------------------------------- ### Setup v2 Implementations Source: https://github.com/coinbase/x402/blob/main/e2e/README.md Sets up only the v2 implementations of the project components. ```bash pnpm setup ``` -------------------------------- ### Install Dependencies and Build Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/fetch/README.md Installs project dependencies using pnpm and builds all packages. This command should be run from the typescript examples root directory. ```bash cd ../.. pnpm install && pnpm build cd clients/fetch ``` -------------------------------- ### Install Minimal x402 Express Server (TypeScript) Source: https://github.com/coinbase/x402/blob/main/README.md Install the core x402 packages along with the Express server implementation for minimal setup. ```shell # Minimal express Server npm install @x402/core @x402/evm @x402/svm @x402/express ``` -------------------------------- ### Start X402 Facilitator Source: https://github.com/coinbase/x402/blob/main/e2e/facilitators/typescript/README.md Navigate to the facilitator directory, set necessary environment variables for private keys and port, and start the server using pnpm. ```bash cd e2e/facilitators/typescript export EVM_PRIVATE_KEY="0x..." export SVM_PRIVATE_KEY="..." export STELLAR_PRIVATE_KEY="S..." # optional export PORT=4025 pnpm start ``` -------------------------------- ### Install Dependencies and Run Source: https://github.com/coinbase/x402/blob/main/examples/go/clients/payment-identifier/README.md Installs project dependencies using Go modules and then runs the main application file. ```bash go mod download go run main.go ``` -------------------------------- ### Install Minimal x402 Fetch Client (TypeScript) Source: https://github.com/coinbase/x402/blob/main/README.md Install the core x402 packages along with the Fetch client implementation for minimal setup. ```shell # Minimal Fetch client npm install @x402/core @x402/evm @x402/svm @x402/fetch ``` -------------------------------- ### Basic Facilitator Server Setup in Go Source: https://github.com/coinbase/x402/blob/main/go/FACILITATOR.md Sets up a basic facilitator server using the x402 package and Gin. Requires a facilitator signer with RPC integration for registering payment schemes. ```go package main import ( "github.com/gin-gonic/gin" x402 "github.com/x402-foundation/x402/go" evm "github.com/x402-foundation/x402/go/mechanisms/evm/exact/facilitator" ) func main() { // 1. Create facilitator facilitator := x402.Newx402Facilitator() // 2. Register payment schemes // Note: Requires facilitator signer with RPC integration facilitator.Register("eip155:84532", evm.NewExactEvmScheme(evmSigner)) // 3. Create HTTP server r := gin.Default() // 4. Expose facilitator endpoints r.GET("/supported", handleSupported(facilitator)) r.POST("/verify", handleVerify(facilitator)) r.POST("/settle", handleSettle(facilitator)) r.Run(":4022") } ``` -------------------------------- ### Setup x402 Client with EVM Signer Source: https://github.com/coinbase/x402/blob/main/examples/go/clients/custom/README.md Initialize an x402 client and register the EVM payment scheme using a private key. ```go import ( x402 "github.com/x402-foundation/x402/go" evm "github.com/x402-foundation/x402/go/mechanisms/evm/exact/client" evmsigners "github.com/x402-foundation/x402/go/signers/evm" ) evmsigner, _ := evmsigners.NewClientSignerFromPrivateKey(os.Getenv("EVM_PRIVATE_KEY")) client := x402.Newx402Client(). Register("eip155:*", evm.NewExactEvmScheme(evmsigner)) ``` -------------------------------- ### Start Express Server Source: https://github.com/coinbase/x402/blob/main/examples/typescript/clients/advanced/README.md This command starts the Express server, which is a prerequisite for testing the advanced x402 client examples. Ensure you navigate to the correct directory first. ```bash cd ../../servers/express pnpm dev ``` -------------------------------- ### Server Usage Example Source: https://github.com/coinbase/x402/blob/main/typescript/packages/core/README.md Shows how to set up an x402 resource server, connect to a facilitator, register payment schemes, and configure routes with payment requirements. ```typescript import { x402ResourceServer, HTTPFacilitatorClient } from '@x402/core/server'; import { x402HTTPResourceServer } from '@x402/core/http'; import { ExactEvmScheme } from '@x402/evm/exact/server'; // Connect to facilitator const facilitatorClient = new HTTPFacilitatorClient({ url: 'https://x402.org/facilitator', }); // Create resource server with payment schemes const resourceServer = new x402ResourceServer(facilitatorClient) .register('eip155:*', new ExactEvmScheme()); // Initialize (fetches supported kinds from facilitator) await resourceServer.initialize(); // Configure routes with payment requirements const routes = { 'GET /api/data': { accepts: { scheme: 'exact', network: 'eip155:8453', payTo: '0xYourAddress', price: '$0.01', }, description: 'Premium data access', mimeType: 'application/json', }, }; // Create HTTP server wrapper const httpServer = new x402HTTPResourceServer(resourceServer, routes); ``` -------------------------------- ### Clone x402 Repo and Install Dependencies Source: https://github.com/coinbase/x402/blob/main/docs/guides/mcp-server-with-x402.md Clone the x402 repository, navigate to the TypeScript examples, and install dependencies. This sets up the necessary environment for the MCP client. ```bash git clone https://github.com/x402-foundation/x402.git cd x402/examples/typescript pnpm install && pnpm build cd clients/mcp ``` -------------------------------- ### Start the SIWX Client Source: https://github.com/coinbase/x402/blob/main/examples/typescript/servers/sign-in-with-x/README.md Initiates the SIWX client from its directory. Ensure the client's '.env' file is configured with necessary private keys for testing authentication and payment flows. ```bash cd ../../clients/sign-in-with-x # Ensure .env is setup with EVM_PRIVATE_KEY or SVM_PRIVATE_KEY pnpm start ``` -------------------------------- ### Bazaar Extension: GET Endpoint Specification Source: https://github.com/coinbase/x402/blob/main/specs/extensions/bazaar.md Example of the bazaar extension in a 402 Payment Required response for a GET endpoint. It details the input parameters and expected output format. ```json { "x402Version": 2, "error": "Payment required", "resource": { "url": "https://api.example.com/weather", "description": "Weather data endpoint", "mimeType": "application/json" }, "accepts": [ ... ], "extensions": { "bazaar": { "info": { "input": { "type": "http", "method": "GET", "queryParams": { "city": "San Francisco" } }, "output": { "type": "json", "example": { "city": "San Francisco", "weather": "foggy", "temperature": 60 } } }, "schema": { "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "input": { "type": "object", "properties": { "type": { "type": "string", "const": "http" }, "method": { "type": "string", "enum": ["GET", "HEAD", "DELETE"] }, "queryParams": { "type": "object", "properties": { "city": { "type": "string" } }, "required": ["city"] }, "headers": { "type": "object", "additionalProperties": { "type": "string" } } }, "required": ["type", "method"], "additionalProperties": false }, "output": { "type": "object", "properties": { "type": { "type": "string" }, "example": { "type": "object" } }, "required": ["type"] } }, "required": ["input"] } } } } ``` -------------------------------- ### Declare Bazaar Discovery Extension for GET Endpoint Source: https://github.com/coinbase/x402/blob/main/python/x402/extensions/README.md Configure route metadata to declare discovery instructions for resource servers. This example shows a GET endpoint for weather data. ```python from x402.extensions.bazaar import declare_discovery_extension routes = { "GET /api/weather": { "accepts": { "scheme": "exact", "price": "$0.001", "network": "eip155:84532", "payTo": "0x...", }, "extensions": { **declare_discovery_extension( input={"city": "San Francisco"}, input_schema={ "properties": {"city": {"type": "string"}}, "required": ["city"], }, output={"example": {"temp": 15, "weather": "foggy"}}, ), }, }, } ``` -------------------------------- ### Go (Gin) Server Setup with X402 Source: https://github.com/coinbase/x402/blob/main/docs/getting-started/quickstart-for-sellers.mdx Initializes a Gin server and imports necessary X402 packages for payment middleware integration. This snippet shows the setup and import statements. ```go package main import ( "net/http" "time" x402 "github.com/x402-foundation/x402/go" x402http "github.com/x402-foundation/x402/go/http" ginmw "github.com/x402-foundation/x402/go/http/gin" evmscheme "github.com/x402-foundation/x402/go/mechanisms/evm/exact/server" svmscheme "github.com/x402-foundation/x402/go/mechanisms/svm/exact/server" "github.com/gin-gonic/gin" ) func main() { evmschemeAddress := "0xYourEvmAddress" svmschemeAddress := "YourSolanaAddress" router := gin.Default() // Initialize X402 facilitator client facilitatorClient := x402http.NewHTTPFacilitatorClient("https://x402.org/facilitator") // Register payment middleware router.Use(ginmw.PaymentMiddleware( router, facilitatorClient, map[string]*ginmw.RoutePaymentConfig{ "GET /weather": { Accepts: []x402.AcceptOffer{ { Scheme: "exact", Price: "$0.001", Network: "eip155:84532", // Base Sepolia PayTo: evmschemeAddress, }, { Scheme: "exact", Price: "$0.001", Network: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", // Solana Devnet PayTo: svmschemeAddress, }, }, Description: "Weather data", MimeType: "application/json", }, }, // Register payment schemes map[string]x402.PaymentScheme{ "eip155:84532": evmscheme.NewExactEvmScheme(), "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1": svmscheme.NewExactSvmScheme(), }, )) // Define the weather endpoint router.GET("/weather", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "report": gin.H{ "weather": "sunny", "temperature": 70, }, }) }) router.Run(":4021") } ``` -------------------------------- ### Declare Bazaar Discovery Extension for GET Endpoint Source: https://github.com/coinbase/x402/blob/main/typescript/packages/extensions/README.md Declare discovery metadata for a GET endpoint, including input parameters, input schema, and an example output. The HTTP method is inferred from the route key. ```typescript import { declareDiscoveryExtension } from "@x402/extensions/bazaar"; const resources = { "GET /weather": { accepts: { scheme: "exact", price: "$0.001", network: "eip155:84532", payTo: "0xYourAddress" }, extensions: { ...declareDiscoveryExtension({ input: { city: "San Francisco" }, inputSchema: { properties: { city: { type: "string" }, units: { type: "string", enum: ["celsius", "fahrenheit"] } }, required: ["city"] }, output: { example: { city: "San Francisco", weather: "foggy", temperature: 15, humidity: 85 } }, }), }, }, }; ``` -------------------------------- ### Example Directory Structure Source: https://github.com/coinbase/x402/blob/main/CONTRIBUTING.md Examples for each SDK are organized into language-specific directories: `typescript/`, `python/`, and `go/`. ```bash examples/ ├── typescript/ # TypeScript examples ├── python/ # Python examples └── go/ # Go examples ``` -------------------------------- ### Project Dependencies Source: https://github.com/coinbase/x402/blob/main/docs/guides/mcp-server-with-x402.md Lists the necessary x402 v2 packages for this example project. Ensure these are installed in your project. ```json { "dependencies": { "@modelcontextprotocol/sdk": "^1.9.0", "@x402/axios": "workspace:*", "@x402/evm": "workspace:*", "@x402/svm": "workspace:*", "axios": "^1.13.2", "viem": "^2.39.0", "@solana/kit": "^2.1.1", "@scure/base": "^1.2.6" } } ``` -------------------------------- ### Run MCP Client Example Source: https://github.com/coinbase/x402/blob/main/examples/go/servers/mcp/README.md Connect to and test the MCP server using the provided MCP client example. Navigate to the client directory and run the simple client. ```bash cd ../../clients/mcp go run . simple ``` -------------------------------- ### GET /discovery/resources Response Example Source: https://github.com/coinbase/x402/blob/main/e2e/facilitators/text-facilitator-protocol.txt Response from the /discovery/resources endpoint, listing discovered resources with pagination details. ```json { "x402Version": 1, "items": [ { "resource": "https://api.example.com/endpoint", "type": "http", "x402Version": 2, "accepts": [...], "discoveryInfo": {...}, "lastUpdated": "2024-01-01T00:00:00Z", "metadata": {} } ], "pagination": { "limit": 100, "offset": 0, "total": 1 } } ```