What You’ll Build
By the end of this tutorial, your application will:- Accept subscription payments through Stripe Checkout
- Process webhook events for subscription lifecycle management
- Provide self-service billing through the customer portal
- Handle upgrades, downgrades, and cancellations
Prerequisites
Before starting this tutorial:- A Dev Kit for AI account with at least one project (register here)
- Your Starter Kit application running locally
- Node.js 18+ and npm installed
- Basic understanding of TypeScript and React
Your Starter Kit requires these environment variables for payment integration:
DEVKIT4AI_DEVELOPER_KEY- Your developer key from Cloud AdminDEVKIT4AI_PROJECT_ID- Your project UUIDDEVKIT4AI_PROJECT_KEY- Your project API keyNEXT_PUBLIC_API_URL- Cloud API URL (default:https://api.devkit4ai.com)NEXT_PUBLIC_APP_URL- Your application URL for redirects
Step 1: Create a Stripe Account
Register for Stripe
Go to stripe.com and create an account. You can start in test mode immediately without providing banking details.
Complete Account Setup
Fill in your business details. For testing, you can use placeholder information.
Access the Dashboard
Navigate to the Stripe Dashboard. Make sure “Test mode” is enabled (toggle in the top-right corner).
Step 2: Get Your Stripe API Keys
Navigate to API Keys
In the Stripe Dashboard, go to Developers → API keys or visit dashboard.stripe.com/apikeys.
Step 3: Create Subscription Products
Before configuring payments, set up your subscription products in Stripe:Navigate to Products
Go to Products in the Stripe Dashboard or visit dashboard.stripe.com/products.
Create a Product
Click “Add product” and fill in:
- Name: e.g., “Pro Plan”
- Description: Your plan description
Step 4: Configure Stripe in Cloud Admin
Now configure your project to use Stripe:Open Cloud Admin
Log in to Cloud Admin and navigate to your project.
Configure Test Mode
Click “Configure Stripe” and enter:
- Mode: Test
- Publishable Key: Your
pk_test_key - Secret Key: Your
sk_test_key - Leave webhook secret empty for now
Step 5: Set Up Webhook Forwarding
Webhooks allow Stripe to notify your application about events like successful payments. For local development, use the Stripe CLI:Forward Webhooks
Start webhook forwarding to your local Cloud API:Replace
{your-project-id} with your actual project UUID.Copy Webhook Signing Secret
The CLI displays a webhook signing secret starting with
whsec_. Copy this value.Step 6: Create Your First Checkout Session
Now let’s test the payment flow. Add a checkout button to your Starter Kit application:Server Action
Create the checkout action:The
testMode parameter defaults to true for development. Set to false when processing live payments in production. This determines whether the API uses your test or live Stripe credentials.Subscribe Button Component
Pricing Page
Replace
price_basic_monthly_id and price_pro_monthly_id with your actual Price IDs from Stripe.Step 7: Test the Payment Flow
Now test the complete payment flow using Stripe’s test cards:Ensure Services Are Running
Make sure you have:
- Starter Kit running locally (
npm run dev) - Cloud API running locally or using the hosted API
- Stripe CLI forwarding webhooks (
stripe listen ...)
Use Test Card
Enter the test card details:
| Field | Value |
|---|---|
| Card number | 4242 4242 4242 4242 |
| Expiry | Any future date (e.g., 12/34) |
| CVC | Any 3 digits (e.g., 123) |
| Name | Any name |
| Country | Any country |
Test Card Numbers
Stripe provides various test card numbers for different scenarios:| Scenario | Card Number |
|---|---|
| Successful payment | 4242 4242 4242 4242 |
| Requires authentication | 4000 0025 0000 3155 |
| Declined (generic) | 4000 0000 0000 9995 |
| Declined (insufficient funds) | 4000 0000 0000 9995 |
| Expired card | 4000 0000 0000 0069 |
Step 8: Verify Webhook Events
Check that webhooks are being processed correctly:In Stripe CLI
Your terminal runningstripe listen should show events like:
In Cloud Admin
Navigate to your project’s Payments tab. You should see:- A new subscription in the subscriptions list
- A payment transaction in the transaction history
Webhook Event Flow
Step 9: Implement Billing Management
Add subscription status display and billing management:Get Subscription Status
Customer Portal Access
Step 10: Switch to Live Mode
Once you’ve thoroughly tested in test mode, switch to live payments:Complete Stripe Account Setup
In the Stripe Dashboard, complete your account activation:
- Add business details
- Connect a bank account for payouts
- Verify your identity
Create Live Products
Create your subscription products in live mode (they don’t transfer from test mode).
Get Live API Keys
Go to Developers → API keys and switch to live mode. Copy your live keys (
pk_live_ and sk_live_).Configure Live Mode in Cloud Admin
Go to your project’s Payments tab → Configure Stripe:
- Switch to Live mode
- Enter your live API keys
- Enter your live webhook signing secret
Set Up Production Webhooks
In Stripe Dashboard → Developers → Webhooks:
- Add endpoint:
https://api.devkit4ai.com/api/v1/payments/stripe/webhooks/{project-id}/live - Select events:
checkout.session.completed,customer.subscription.*,invoice.* - Copy the signing secret to Cloud Admin
Troubleshooting
Checkout Session Fails to Create
Error: “Failed to create checkout session” Solutions:- Verify your Stripe credentials are correctly configured in Cloud Admin
- Check that the Price ID exists in your Stripe account
- Ensure the user is authenticated before starting checkout
- Check Cloud API logs for detailed error messages
Webhook Events Not Received
Error: Subscriptions not appearing after payment Solutions:- Verify Stripe CLI is running and forwarding to the correct URL
- Check the webhook signing secret matches in Cloud Admin
- Look for errors in the Stripe CLI output
- Verify the webhook URL includes the correct project ID and mode (test/live)
Customer Portal Not Opening
Error: “Failed to create portal session” Solutions:- Ensure the user has an existing Stripe customer record (created during checkout)
- Check that customer portal is enabled in your Stripe Dashboard (Settings → Billing → Customer portal)
- Verify the return URL is valid
Webhook Signature Verification Failed
Error: 400 error on webhook endpoint Solutions:- Copy the webhook secret exactly (including
whsec_prefix) - Ensure you’re using the correct secret for test vs live mode
- Check that the webhook secret hasn’t expired (restart Stripe CLI gets a new secret)
Complete Implementation Example
For a complete working example, see the Starter Kit billing implementation which includes:- Full billing page with subscription status
- Payment history display
- Customer portal integration
- Feature gating based on subscription status

