@dodopayments/convex component adds Dodo Payments to your Convex backend. It provides a checkout function that creates checkout sessions, a customerPortal function that opens the Customer Portal for the signed-in user, and createDodoWebhookHandler, which verifies webhooks in a Convex HTTP action. It requires Convex 1.26 or later.
Checkout Function
Create checkout sessions from Convex actions.
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:
2
Add Component to Convex Config
Add the Dodo Payments component to your Convex configuration:After you edit
convex.config.ts, run npx convex dev once to generate the types.3
Set Up Environment Variables
Set environment variables in your Convex dashboard under Settings → Environment Variables. To open the dashboard, run:Add these environment variables:
DODO_PAYMENTS_API_KEY: Your Dodo Payments API key, from Developer → API Keys in the Dodo Payments dashboard.DODO_PAYMENTS_ENVIRONMENT:test_modeorlive_mode.DODO_PAYMENTS_WEBHOOK_SECRET: Your webhook secret, from Developer → Webhooks. Required for webhook handling. The webhook handler reads this exact variable name.
Component Setup Examples
1
Create Internal Query
Create an internal query that finds a customer in your database by auth ID. The
identify function in the next step uses it to get the signed-in user’s Dodo Payments customer ID for the customer portal.2
Configure DodoPayments Component
Create the client.
identify maps the signed-in Convex user to a Dodo Payments customer ID. It returns null if no user is signed in or no customer matches.- Checkout Function Setup
- Customer Portal Setup
- Webhook Handler Setup
Use this function to add Dodo Payments checkout to your Convex app. It creates a checkout session from the fields accepted by the component’s checkout payload validator.
Checkout Function
The Convex component creates checkout sessions, the recommended checkout flow for all payments. A session holds the product cart, customer details, and checkout options.Usage
Callcheckout from a Convex action, with the checkout session fields in payload:
checkout doesn’t call identify. To attach an existing customer, pass customer: { customer_id } in the payload. For more details and a complete list of supported fields, see Checkout Sessions.
A session created with payment_method_id returns no checkout URL, so checkout throws an error for it.
Response Format
The checkout function returns an object with the checkout URL:Customer Portal Function
The customer portal function returns a Customer Portal URL for the signed-in user.Usage
portal_url field.
Parameters
boolean
default:"false"
If set to
true, Dodo Payments also emails the portal link to the customer.customerPortal gets the customer from the identify function in your DodoPayments setup, which must return the customer’s dodoCustomerId. If identify returns null, customerPortal throws a User is not authenticated. error.Webhook Handler
createDodoWebhookHandler verifies each request before it runs your code:
- Method: Register the route with
method: "POST". Requests with other methods don’t reach the handler. - Signature Verification: Verifies the Standard Webhooks signature with the
DODO_PAYMENTS_WEBHOOK_SECRETenvironment variable. Returns 400 if verification fails. - Payload Validation: Validated with Zod. Returns 400 for invalid payloads.
- Error Handling:
- 400: Invalid signature, invalid payload, or an error thrown by one of your handlers
- 200: All handlers finished
- If
DODO_PAYMENTS_WEBHOOK_SECRETisn’t set, the handler throws an error and the request fails.
- Event Routing: Calls
onPayloadfor every event, then the handler for the event’s type.
Supported Webhook Event Handlers
Each handler receives the ConvexActionCtx and the verified payload for its event type:
Frontend Usage
Call the checkout and portal actions from your React components with theuseAction hook from convex/react.