A drop-in x402 middleware that detects AI agents, processes USDC micropayments, and automatically serves LLM-optimized Markdown instead of standard HTML.
Export middleware, configure matcher, export API route. No refactoring needed.
Measured on this demo: HTML 28.3KB. Markdown 2KB. 93% smaller.
Processes rendered HTML. Works whether you use CMS, database, MDX, or React components.
npm install @x402/next-sdkexport { middleware } from '@x402/next-sdk/middleware';
export const config = {
matcher: ['/blog/:path*'],
};export { GET } from '@x402/next-sdk/route';X402_PRICE_USDC=500000
X402_MERCHANT_WALLET_ADDRESS=0xYourAddress
X402_CHAIN_ID=84532AI agent requests /blog/post with User-Agent: ClaudeBot
Middleware intercepts the request and returns 402 Payment Required
Agent pays 0.50 USDC on the Base network
Middleware verifies the payment and rewrites the request internally to /api/x402
The API route fetches the HTML, extracts core content using Readability, and converts it with Turndown
Agent receives clean, parsed Markdown (no navigation, ads, or styling)
Simulate an AI agent request by passing the appropriate headers:
curl -H "User-Agent: ClaudeBot/1.0" \
http://localhost:3000/blog/intro-to-x402