Next’s proxy (middleware) never sees a route’s response body, so Forge wraps each paid route handler instead. With @x402/next, put Forge outermost:
app/api/text-stats/route.ts

Add Forge

1

Create Forge once

lib/forge.ts
Missing env (for example during next build) doesn’t break the build: Forge warns and turns itself off.
2

Wrap each paid route

export const POST = forge.withForge(withX402(handler, route, resourceServer));Your handler receives the request without agent context. It stays a NextRequest, so nextUrl works as usual.
3

Add Forge's routes

One catch-all route file answers /feedback, /feedback/rate and /feedback/summary:
app/feedback/[[...path]]/route.ts
4

Serve your OpenAPI document through Forge (optional)

app/openapi.json/route.ts

Using paymentProxy instead

If you protect routes with @x402/next’s paymentProxy in proxy.ts (middleware), wrap it so its 402s carry the rating ask, and still wrap the paid route handlers with withForge for the body fields:
proxy.ts
The proxy never mints feedback IDs; withForge on the route does, so the body and header always carry the same ID. In this mode the payment receipt isn’t decorated, because settlement happens outside the route.

Complete example

From examples/nextjs in the Forge SDK repository. Forge lines are marked (forge).
lib/forge.ts
app/api/text-stats/route.ts
app/feedback/[[...path]]/route.ts
app/openapi.json/route.ts
Run it on Base Sepolia (copy .env.example to .env.local and fill it in):