Accept Payments with Stripe

Install the Stripe addon, create a checkout endpoint, and handle webhooks.

Accept Payments with Stripe

Set up Stripe checkout in your API — create payment sessions, redirect users to pay, and track order status with webhooks.

What you will build

- POST /api/checkout — create a Stripe checkout session
- POST /api/webhooks/stripe — handle payment status updates

Steps

Install the Stripe addon

Go to Addons, find Stripe, and click Install. Then add your Stripe secret key as STRIPE_SECRET_KEY in Settings > Environment Variables.

Create an orders table

In the Database screen, create an orders table:

| Column | Type | Notes |
    |--------|------|-------|
    | id | Serial | Primary key |
    | user_id | Integer | Foreign key to users |
    | stripe_session_id | Text | Links to Stripe |
    | status | Text | Default: 'pending' |
    | total | Decimal | Order amount |
    | created_at | Timestamp | Default: now() |

Build the checkout endpoint

Create POST /api/checkout with this function:

1. External API Call — Method: POST, URL: 'https://api.stripe.com/v1/checkout/sessions', Auth: Bearer env.STRIPE_SECRET_KEY, Content Type: form-encoded, Body: { mode: 'payment', success_url: input.successUrl, cancel_url: input.cancelUrl, 'line_items[0][price_data][currency]': 'usd', 'line_items[0][price_data][product_data][name]': input.productName, 'line_items[0][price_data][unit_amount]': input.amountCents, 'line_items[0][quantity]': input.quantity | default(1) }, Assign To: session
    2. Database Insert — Table: orders, Data: { user_id: vars.auth.userId, stripe_session_id: vars.session.body.id, status: 'pending', total: input.total }
    3. Respond — Status: 200, Body: { url: vars.session.body.url }

Your frontend redirects the user to the returned URL to complete payment.

Build the webhook endpoint

Create POST /api/webhooks/stripe. Stripe calls this URL when payment status changes.

1. Verify webhook signature — Validate the Stripe signature header with the webhook signing secret before trusting the payload.
    2. Set Variable — Name: event, Value: input
    3. Switch — Expression: vars.event.type
       - Case 'checkout.session.completed': Database Update — Table: orders, Data: { status: 'paid' }, Where: { stripe_session_id: vars.event.data.object.id }
       - Case 'charge.refunded': Database Update — Table: orders, Data: { status: 'refunded' }, Where: { stripe_session_id: vars.event.data.object.id }
    4. Respond — Status: 200, Body: { received: true }

Configure the webhook in Stripe

In your Stripe dashboard, go to Developers > Webhooks, add your /api/webhooks/stripe URL, and select the events checkout.session.completed and charge.refunded.

Important

Webhook signature verification must happen before you update orders. Use the signing secret from your Stripe dashboard and reject requests that fail verification.

Addons
/addons
Browse all available integrations
Build a REST API
/guides/build-rest-api
Create the product endpoints to sell