PromptShop

Monetize Service

This skill creates an Express server that charges USDC for API access using the x402 payment protocol. It enables per-request payments in USDC on Base,.

Install

npx promptshop add monetize-service

Details

What This Skill Does

This skill creates an Express server that charges USDC for API access using the x402 payment protocol. It enables per-request payments in USDC on Base, eliminating the need for accounts or API keys. This skill is useful for developers looking to monetize their APIs.

When to Use

  • Charge USDC for API access.
  • Set up an x402 payment server.
  • Register service with the x402 Bazaar.
  • Get the wallet payment address.
  • Install required npm packages.
  • Protect routes with payment middleware.

Key Features

  • Uses the x402 payment protocol for per-request payments.
  • Eliminates the need for accounts or API keys.
  • Automatically discoverable by other agents via the x402 Bazaar.
  • Provides code snippets for setting up the Express server.
  • Requires authentication before setting up the server.
  • Uses HTTP 402 for payment requests.

Manual Installation

How It Works

x402 is an HTTP-native payment protocol. When a client hits a protected endpoint without paying, the server returns HTTP 402 with payment requirements. The client signs a USDC payment and retries with a payment header. The facilitator verifies and settles the payment, and the server returns the response. Services register with the x402 Bazaar so other agents can discover and pay for them automatically.

Confirm wallet is initialized and authed

npx awal@2.0.3 status

If the wallet is not authenticated, refer to the authenticate-wallet skill.

Step 1: Get the Payment Address

Run this to get the wallet address that will receive payments:

npx awal@2.0.3 address

Use this address as the payTo value.

Step 2: Set Up the Project

mkdir x402-server && cd x402-server npm init -y npm install express @x402/express @x402/core @x402/evm @x402/extensions

Create index.js:

const express = require("express"); const { paymentMiddleware } = require("@x402/express"); const { x402ResourceServer, HTTPFacilitatorClient } = require("@x402/core/server"); const { ExactEvmScheme } = require("@x402/evm/exact/server");

const app = express(); app.use(express.json());

const PAY_TO = "<address from step 1>";

// Create facilitator client and x402 resource server const facilitator = new HTTPFacilitatorClient({ url: "https://x402.org/facilitator" }); const server = new x402ResourceServer(facilitator); server.register("eip155:8453", new ExactEvmScheme());

// x402 payment middleware — protects routes below app.use( paymentMiddleware( { "GET /api/example": { accepts: { scheme: "exact", price: "$0.01", network: "eip155:8453", payTo: PAY_TO, }, description: "Description of what this endpoint returns", mimeType: "application/json", }, }, server, ), );

// Protected endpoint app.get("/api/example", (req, res) => { res.json({ data: "This costs $0.01 per request" }); });

app.listen(3000, () => console.log("Server running on port 3000"));

Step 3: Run It

node index.js

Test with curl — you should get a 402 response with payment requirements:

curl -i http://localhost:3000/api/example

API Reference

paymentMiddleware(routes, server)

Creates Express middleware that enforces x402 payments.

ParameterTypeDescription
routesobjectRoute config mapping route patterns to payment config
server

x402ResourceServer

| Pre-configured x402 resource server instance |

x402ResourceServer

Created with a facilitator client. Register payment schemes and extensions before passing to middleware.

const { x402ResourceServer, HTTPFacilitatorClient } = require("@x402/core/server"); const { ExactEvmScheme } = require("@x402/evm/exact/server");

const facilitator = new HTTPFacilitatorClient({ url: "https://x402.org" }); const server = new x402ResourceServer(facilitator); server.register("eip155:8453", new ExactEvmScheme());

MethodDescription
register(network, scheme)Register a payment scheme for a CAIP-2 network identifier

Route Config

Each key in the routes object is "METHOD /path". The value is a config object:

{ "GET /api/data": { accepts: { scheme: "exact", price: "$0.05", network: "eip155:8453", payTo: "0x...", }, description: "Human-readable description of the endpoint", mimeType: "application/json", extensions: { ...declareDiscoveryExtension({ output: { example: { result: "example response" }, schema: { properties: { result: { type: "string" }, }, }, }, }), }, }, }

Accepts Config Fields

The accepts field can be a single object or an array (for multiple payment options):

FieldTypeDescription
schemestringPayment scheme: "exact"
pricestringUSDC price (e.g. "$0.01", "$1.00")
networkstringCAIP-2 network identifier (e.g. "eip155:8453")
payTostringEthereum address (0x...) to receive USDC payments

Route-Level Fields

FieldTypeDescription
acceptsobject or arrayPayment requirements (single or multiple)
descriptionstring?What this endpoint does (shown to clients)
mimeTypestring?MIME type of the response
extensionsobject?Extensions config (e.g. Bazaar discovery)

Discovery Extension

The declareDiscoveryExtension function registers your endpoint with the x402 Bazaar so other agents can discover it:

const { declareDiscoveryExtension } = require("@x402/extensions/bazaar");

extensions: { ...declareDiscoveryExtension({ output: { example: { / example response body / }, schema: { properties: { / JSON schema of the response / }, }, }, }), }

FieldTypeDescription
output.exampleobjectExample response body for the endpoint
output.schemaobjectJSON schema describing the response format

Supported Networks

NetworkDescription
eip155:8453Base mainnet (real USDC)
eip155:84532Base Sepolia testnet (test USDC)

Patterns

Multiple endpoints with different prices

app.use( paymentMiddleware( { "GET /api/cheap": { accepts: { scheme: "exact", price: "$0.001", network: "eip155:8453", payTo: PAY_TO, }, description: "Inexpensive data lookup", }, "GET /api/expensive": { accepts: { scheme: "exact", price: "$1.00", network: "eip155:8453", payTo: PAY_TO, }, description: "Premium data access", }, "POST /api/query": { accepts: { scheme: "exact", price: