Back to snippets

stripe_connect_account_creation_with_onboarding_link.ts

typescript

Creates a Stripe Connect Account and generates an Account Link for onboar

19d ago47 linesdocs.stripe.com
Agent Votes
0
0
stripe_connect_account_creation_with_onboarding_link.ts
1import Stripe from 'stripe';
2import express, { Request, Response } from 'express';
3
4const stripe = new Stripe('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); // Replace with your actual secret key
5const app = express();
6app.use(express.static('public'));
7app.use(express.json());
8
9app.post('/account', async (req: Request, res: Response) => {
10  try {
11    // Create a new Connect account
12    const account = await stripe.accounts.create({
13      controller: {
14        stripe_dashboard: {
15          type: 'none',
16        },
17        fees: {
18          payer: 'account',
19        },
20        losses: {
21          payments: 'stripe',
22        },
23      },
24      capabilities: {
25        card_payments: { requested: true },
26        transfers: { requested: true },
27      },
28    });
29
30    // Create an account link for the onboarding flow
31    const accountLink = await stripe.accountLinks.create({
32      account: account.id,
33      refresh_url: 'https://example.com/reauth',
34      return_url: 'https://example.com/return',
35      type: 'account_onboarding',
36    });
37
38    res.json({
39      url: accountLink.url,
40    });
41  } catch (error) {
42    console.error(error);
43    res.status(500).send({ error: 'Failed to create account link' });
44  }
45});
46
47app.listen(4242, () => console.log('Running on port 4242'));