-
Notifications
You must be signed in to change notification settings - Fork 5
API - First x402 endpoint #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API - First x402 endpoint #178
Conversation
WalkthroughThis pull request integrates x402-express payment middleware into an Express application to enable payment-protected API routes. A new payment middleware factory is created, configured with wallet and facilitator details, and registered in the app. A Changes
Sequence DiagramsequenceDiagram
participant Client
participant PaymentMiddleware
participant Route Handler
Client->>PaymentMiddleware: GET /api/image/generate
Note over PaymentMiddleware: Validate payment headers<br/>(wallet, price, network)
alt Payment Valid
PaymentMiddleware->>Route Handler: Forward request
Route Handler->>Client: 200 JSON (weather report)
else Payment Invalid
PaymentMiddleware->>Client: 402 Payment Required
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Areas requiring extra attention:
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
lib/x402/paymentMiddleware.ts (1)
32-36: Add validation for middleware configuration.Consider adding runtime validation to ensure the middleware is configured correctly before returning it. This helps catch configuration issues early.
export const createPaymentMiddleware = (): RequestHandler => { + if (!RECEIVING_WALLET_ADDRESS) { + throw new Error("Payment middleware requires RECEIVING_WALLET_ADDRESS"); + } + if (!FACILITATOR_URL) { + throw new Error("Payment middleware requires FACILITATOR_URL"); + } return paymentMiddleware(RECEIVING_WALLET_ADDRESS, routeConfig, { url: FACILITATOR_URL, }); };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (5)
app.ts(2 hunks)lib/x402/paymentMiddleware.ts(1 hunks)package.json(2 hunks)routes.ts(1 hunks)tsconfig.json(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
app.ts (1)
lib/x402/paymentMiddleware.ts (1)
createPaymentMiddleware(32-36)
🔇 Additional comments (3)
tsconfig.json (1)
28-31: LGTM! Module resolution update is appropriate.The change to
NodeNextmodule andnodenextmoduleResolution is a standard configuration that provides better interoperability with modern ES modules while still outputting CommonJS (as noted in the comment). This aligns well with the new x402 dependencies being added.app.ts (1)
19-20: No action required—the middleware and route handler work correctly together.The original review mischaracterizes the code. The payment middleware validates payment and then calls next() so your route handler runs. If the request is unpaid/invalid the middleware responds with the 402 paywall response (does not call next()). This is exactly what the code does:
- Payment middleware validates the request to
GET /api/image/generate- On valid payment: calls
next()and the route handler atroutes.ts:140executes- On invalid payment: returns 402 response directly
There is no conflict. This is standard Express middleware chaining behavior and the implementation is correct.
Likely an incorrect or invalid review comment.
package.json (1)
21-21: No action required—library versions are current and secure.Verification confirms that both
@coinbase/x402andx402-expressat version 0.7.3 are the latest available releases and are free from known vulnerabilities. The high-severity advisory for x402-express affects only versions < 0.5.2, and the current version 0.7.3 is well beyond that threshold.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| res.send({ | ||
| report: { | ||
| weather: "sunny", | ||
| temperature: 70, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Response shape mismatched to advertised schema
The payment middleware publishes an outputSchema with top-level weather and temperature fields for GET /api/image/generate (lib/x402/paymentMiddleware.ts lines 21-26), but the new handler returns them nested inside report, so paid callers receive a different shape than promised and any client validating against the advertised schema will fail on every call.
Useful? React with 👍 / 👎.
Summary by CodeRabbit
New Features
/image/generateendpoint.Dependencies
Configuration
✏️ Tip: You can customize this high-level summary in your review settings.