@dodopayments/nextjs package gives your Next.js App Router project three route handlers. Checkout returns checkout URLs, CustomerPortal sends a customer to the Customer Portal, and Webhooks verifies webhook events and routes them to your code. The package supports Next.js 14, 15, and 16.
Checkout Handler
Create checkout URLs with static and checkout session flows.
Customer Portal
Let customers manage their subscriptions and details.
Webhooks
Receive and process Dodo Payments webhook events.
Installation
1
Install the Package
Run this command in your project root:The package also requires Zod 3.25 or Zod 4 as a peer dependency.
2
Set Up Environment Variables
Create a
.env file in your project root. Create the API key under Developer → API Keys and the webhook secret under Developer → Webhooks in the dashboard:DODO_PAYMENTS_RETURN_URL is where customers land after checkout. If you don’t pass an environment, the handlers use live_mode.Route Handler Examples
All examples assume you use the Next.js App Router.
- Checkout Handler
- Customer Portal Handler
- Webhook Handler
Use this handler to add Dodo Payments checkout to your app. A
GET handler serves static checkout. A POST handler serves checkout sessions.Checkout Route Handler
The checkout handler supports all three ways to take payments with Dodo Payments:- Static Payment Links: Shareable URLs that collect payments without code.
- Dynamic Payment Links (deprecated): Payment links you generate with custom details. They use deprecated endpoints.
- Checkout Sessions: Hosted checkout with a product cart, customer details, and customization options. This is the recommended flow.
Static Checkout (GET)
Static Checkout (GET)
Supported Query Parameters
Fixes the amount charged, in major currency units, for example
12.5 for $12.50. Works with Pay What You Want products only, and is ignored if it’s below the product’s minimum price.returnUrl from its config to the link as redirect_url.Response Format
Static checkout returns a JSON response with the checkout URL. In test mode, the URL usestest.checkout.dodopayments.com.Dynamic Checkout (POST)
Dynamic Checkout (POST)
Checkout Sessions (POST)
Checkout Sessions (POST)
Checkout sessions create a hosted checkout for one-time purchases and subscriptions, with full control over customization.
product_cart is the only required field, and it needs at least one product. If the body has no return_url, the handler uses returnUrl from its config.For more details and every supported field, see the Checkout Sessions Integration Guide.A session created with payment_method_id returns no checkout URL, so the handler responds with 400. To charge a saved payment method, create the session with the SDK instead.Response Format
Checkout sessions return a JSON response with the checkout URL:Customer Portal Route Handler
The Customer Portal route handler creates a Customer Portal session for the customer you pass and redirects the browser to it.Query Parameters
The handler returns 400 ifcustomer_id is missing, and 500 if the portal session can’t be created.
Webhook Route Handler
The webhook route handler verifies each request before it runs your code:- Method: Only POST requests are supported. Other methods return 405.
- Signature Verification: Verifies the raw request body against the
webhook-id,webhook-timestamp, andwebhook-signatureheaders withwebhookKey. Returns 401 if verification fails. - Payload Validation: Parses the verified body as JSON and validates it with Zod. Returns 400 when a parsed payload doesn’t match the webhook schema.
- Error Handling:
- 401: Invalid signature
- 400: Invalid payload
- 500: Unexpected verification errors, malformed JSON, or errors thrown by your callbacks
- Event Routing: Calls
onPayloadfor every event, then the handler for the event’s type, and returns 200.