GuidesStripe Integration
Stripe Setup
Initial Stripe configuration and API key setup
Stripe Setup
Get started with Stripe integration by setting up your API keys and verifying the configuration.
Quick Start
Get Your Stripe Keys
- Create a Stripe account
- Go to API Keys
- Copy your Publishable key and Secret key
Add Environment Variables
# Stripe Keys
STRIPE_SECRET_KEY=sk_test_...
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_...
# Webhook Secret (after webhook setup)
STRIPE_WEBHOOK_SECRET=whsec_...Verify Configuration
The Stripe plugin is already configured in packages/auth/src/index.ts:
import { stripe } from "@better-auth/stripe";
export const auth = betterAuth({
plugins: [
stripe({
stripeSecretKey: process.env.STRIPE_SECRET_KEY!,
}),
],
});Test Mode vs Live Mode
Stripe has two modes:
Test Mode
- API keys start with
sk_test_andpk_test_ - Use test card numbers (e.g.,
4242 4242 4242 4242) - No real charges occur
- Perfect for development
Live Mode
- API keys start with
sk_live_andpk_live_ - Real charges to real cards
- Use only in production
- Requires business verification
Never commit API keys to version control. Always use environment variables.
Test Cards
Use these test cards in test mode:
| Card Number | Description |
|---|---|
4242 4242 4242 4242 | Successful payment |
4000 0000 0000 0341 | Requires authentication (3D Secure) |
4000 0000 0000 9995 | Declined (insufficient funds) |
4000 0000 0000 0069 | Expired card |
Use any future expiry date and any 3-digit CVC.
Switching to Live Mode
Complete Business Activation
- Go to Stripe Dashboard
- Complete your business profile
- Provide required documentation
- Wait for approval
Get Live API Keys
- Toggle to Live mode (top right)
- Go to API Keys
- Copy live Publishable key and Secret key
Update Production Environment
Update your production environment variables:
STRIPE_SECRET_KEY=sk_live_...
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_...Configuration Options
Advanced Stripe plugin configuration:
stripe({
stripeSecretKey: process.env.STRIPE_SECRET_KEY!,
// Optional: Custom configuration
apiVersion: '2023-10-16', // Lock API version
// Optional: Retry configuration
maxNetworkRetries: 2,
// Optional: Timeout
timeout: 30000, // 30 seconds
})Verification
Test your setup with a simple API call:
import { NextResponse } from 'next/server';
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function GET() {
try {
// Test API call
const account = await stripe.account.retrieve();
return NextResponse.json({
success: true,
account: {
id: account.id,
email: account.email,
type: account.type,
},
});
} catch (error) {
return NextResponse.json(
{ success: false, error: error.message },
{ status: 500 }
);
}
}Visit /api/test-stripe to verify your setup.
Troubleshooting
Invalid API Key
Error: "Invalid API Key provided"
- Check that the key matches the mode (test/live)
- Ensure no extra spaces or quotes
- Verify environment variable is loaded
CORS Errors
Error: "CORS policy: No 'Access-Control-Allow-Origin' header"
- Use Stripe publishable key on client-side only
- Secret key must stay server-side
- Use server actions or API routes
Network Errors
Error: "Network request failed"
- Check firewall settings
- Verify internet connection
- Stripe might be down (check status.stripe.com)
Next Steps
Now that Stripe is configured, proceed to: