TL;DR
- Install Stripe through the Vercel Marketplace and connect your account.
- Provision environment variables automatically—no manual API key management.
- Set up a Stripe Checkout Session to start accepting payments immediately.
Prerequisites
Before you begin, ensure you have the following:
- A basic understanding of Vercel and Stripe.
- An active Vercel account.
- An active Stripe account.
- Node.js and npm installed on your development machine.
Step-by-step Instructions
Follow these steps to move from idea to a fully functional secure checkout system using Stripe and Vercel.
Step 1: Install Stripe via Vercel Marketplace
- Log in to your Vercel account and navigate to the Vercel Marketplace.
- Search for "Stripe" in the Marketplace and click on it.
- Click "Install" to add Stripe to your project. You may be prompted to connect your Stripe account if you haven't already.
(Placeholder for Stripe installation screenshot)
Step 2: Connect Your Stripe Account
- After installation, click on "Connect Stripe Account".
- Follow the prompts to authenticate and link your existing Stripe account with Vercel.
- This automatically generates and configures necessary environment variables for you.
(Placeholder for account connection screenshot)
Step 3: Prepare Your Vercel Environment
- Ensure the environment variables are set in Vercel. These should include:
STRIPE_SECRET_KEY=your-secret-key NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=your-publishable-key- You can verify this through Vercel's Environment Variables settings under your project.
Step 4: Create a Checkout Session
- In your Vercel project, create a new API route file (e.g.,
/api/create-checkout-session.js) with the following code:const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); export default async (req, res) => { const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], line_items: [ { price_data: { currency: 'usd', product_data: { name: 'T-shirt', }, unit_amount: 2000, }, quantity: 1, }, ], mode: 'payment', success_url: `${req.headers.origin}/success?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${req.headers.origin}/cancel`, }); res.status(200).json({ sessionId: session.id }); }; - Deploy your changes using Vercel CLI or directly from the Vercel dashboard to ensure your API route is available.
Step 5: Test the Checkout Flow
- On the frontend, create a button that calls this Checkout session endpoint:
<button id="checkout-button">Checkout</button> <script src="https://js.stripe.com/v3/"></script> <script> const stripe = Stripe('NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY'); document.getElementById('checkout-button').addEventListener('click', () => { fetch('/api/create-checkout-session', { method: 'POST', }) .then(response => response.json()) .then(session => { return stripe.redirectToCheckout({ sessionId: session.id }); }) .then(result => { if (result.error) { alert(result.error.message); } }) .catch(error => { console.error('Error:', error); }); }); </script> - Navigate to your Vercel-deployed site and test the checkout flow by clicking the "Checkout" button.
Tips and Best Practices
- Always test using Stripe's test mode before moving to a live transaction to ensure your flow works seamlessly.
- Keep your server environment file (
.env) secure and don't commit secret keys to version control. - Use Stripe Webhooks to handle post-payment processes such as sending confirmation emails or updating order databases.
Common Issues
- Environment Variables Not Set: Verify that the keys are properly set in your Vercel project settings.
- HTTP/S Errors: Ensure your API is accessible over HTTPS, as Stripe requires a secure connection for all interactions.
- Session Creation Fails: Check the server logs in Vercel for any error messages related to Stripe API calls.
Next Steps
- Explore deeper integration of Stripe's subscription APIs to handle recurring payments.
- Implement advanced UI flows using AI-driven tools to enhance your storefront experience.
- Explore the Vercel Marketplace for other integrations that can simplify your deployment and scaling processes.
