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
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
Complete Account Setup
Access the Dashboard
Step 2: Get Your Stripe API Keys
Navigate to API Keys
Copy Test Keys
- Publishable key: Starts with
pk_test_ - Secret key: Starts with
sk_test_(click “Reveal test key” to view)
Step 3: Create Subscription Products
Before configuring payments, set up your subscription products in Stripe:Navigate to Products
Create a Product
- Name: e.g., “Pro Plan”
- Description: Your plan description
Add Pricing
- Pricing model: Standard pricing
- Price: e.g., $29.00
- Billing period: Monthly or Yearly
price_) - you’ll need this later.Step 4: Configure Stripe in Cloud Admin
Now configure your project to use Stripe:Open Cloud Admin
Go to Payments Tab
Configure Test Mode
- Mode: Test
- Publishable Key: Your
pk_test_key - Secret Key: Your
sk_test_key - Leave webhook secret empty for now
Save Configuration
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:Install Stripe CLI
- macOS
- Windows
- Linux
Login to Stripe CLI
Forward Webhooks
{your-project-id} with your actual project UUID.Copy Webhook Signing Secret
whsec_. Copy this value.Update Cloud Admin Configuration
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: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
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
- Starter Kit running locally (
npm run dev) - Cloud API running locally or using the hosted API
- Stripe CLI forwarding webhooks (
stripe listen ...)
Navigate to Pricing
Click Subscribe
Use Test Card
Complete Payment
Verify Success
Test Card Numbers
Stripe provides various test card numbers for different scenarios: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
- Add business details
- Connect a bank account for payouts
- Verify your identity
Create Live Products
Get Live API Keys
pk_live_ and sk_live_).Configure Live Mode in Cloud Admin
- Switch to Live mode
- Enter your live API keys
- Enter your live webhook signing secret
Set Up Production 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
Update Price IDs
Update testMode Parameter
testMode from true to false in your billing actions to use live Stripe credentials: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

