Discover how to seamlessly integrate Bolt.new AI with Stripe Connect using our step-by-step guide for secure, efficient payment processing.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Since Bolt.new AI does not have a terminal, you need to add the dependency manually by creating (or updating) a package file in your project. Create a file named package.json
in your project’s root (if it does not exist) and include the following content:
{
"dependencies": {
"stripe": "^10.0.0"
}
}
This tells your project to use the Stripe package. When your project builds, Bolt.new AI will include this dependency automatically.
Create a new file called stripe.ts
in your project’s source directory. This file initializes the Stripe API with your secret key. Replace YOURSTRIPESECRET_KEY
with your actual secret key which you can store securely (for example, using environment variables).
import Stripe from 'stripe';
const stripe = new Stripe('YOURSTRIPESECRET_KEY', {
apiVersion: '2022-11-15'
});
export default stripe;
Place this file in the same directory as other backend helper files or utilities.
Next, create a file named createAccountLink.ts
. This file will export a function that creates a Stripe Connect onboarding link for your users. Customize refreshurl
and returnurl
to point to the appropriate routes in your project.
import stripe from './stripe';
export async function createAccountLink(accountId: string): Promise<{ url: string }> {
// Create an account link for onboarding
const accountLink = await stripe.accountLinks.create({
account: accountId,
refresh_url: 'https://yourdomain.com/reauth',
return_url: 'https://yourdomain.com/return',
type: 'account_onboarding'
});
return { url: accountLink.url };
}
Place this file in the same directory as stripe.ts
.
Assuming you have a backend endpoint handling API requests (for example, using Express), add an endpoint that calls the onboarding function. If you have a main server file (e.g., server.ts
), add the following code snippet where you define your routes.
import express, { Request, Response } from 'express';
import { createAccountLink } from './createAccountLink';
const app = express();
app.use(express.json());
// Endpoint to start the Stripe Connect onboarding flow
app.post('/api/stripe/onboard', async (req: Request, res: Response) => {
try {
// Expect the Stripe account ID to be sent with the request
const { accountId } = req.body;
if (!accountId) {
return res.status(400).json({ error: 'Missing accountId' });
}
const { url } = await createAccountLink(accountId);
// Redirect the user to the Stripe Connect onboarding URL
res.redirect(url);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Your existing server code may go here...
app.listen(3000, () => console.log('Server running on port 3000'));
Insert this code into your main server file or the file where you define API endpoints. Adjust the port, routing, or error handling as needed.
To receive events from Stripe about account updates, create a new file named webhook.ts
with the following code. Remember to replace YOURENDPOINTSECRET
with your actual Stripe webhook secret.
import stripe from './stripe';
import { Request, Response } from 'express';
export function handleStripeWebhook(req: Request, res: Response) {
const sig = req.headers['stripe-signature'] as string;
let event;
try {
// Construct event using Stripe's library and your endpoint secret
event = stripe.webhooks.constructEvent(req.body, sig, 'YOURENDPOINTSECRET');
} catch (err: any) {
console.error('Webhook signature verification failed.', err);
return res.status(400).send(Webhook Error: ${err.message});
}
// Process the event (customize cases as needed)
switch (event.type) {
case 'account.updated':
console.log('Account updated:', event.data.object);
break;
default:
console.log(Unhandled event type: ${event.type});
}
res.json({ received: true });
}
Then add a route to your main server file similar to:
import { handleStripeWebhook } from './webhook';
app.post('/api/stripe/webhook', express.raw({ type: 'application/json' }), handleStripeWebhook);
Make sure that your request body is parsed correctly for webhook verification; Express’s express.raw
middleware is used in this example.
refreshurl
and returnurl
parameters to connect to your Bolt.new AI project’s pages.By following these steps, you integrate Stripe Connect into your Bolt.new AI project using TypeScript.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.