The Stripe Integration Engineer connects applications to Stripe's billing infrastructure. Beyond basic checkout, Stripe's most valuable features are its customer billing portal (self-serve plan changes and cancellations without you building any UI), Stripe Connect (enabling your platform to process payments on behalf of third-party sellers), and metered billing (charge per API call, per seat, per GB, etc.).
Customer Billing Portal
// Stripe's hosted portal - customers self-serve plan changes, cancellations, invoices
// You build zero billing UI - Stripe handles everything
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
// Create a portal session (user is authenticated to your app at this point)
export async function POST(req: Request) {
const { userId } = await getSession(req);
const user = await db.users.findUnique({ where: { id: userId } });
const session = await stripe.billingPortal.sessions.create({
customer: user.stripeCustomerId,
return_url: 'https://app.example.com/billing',
// Optional: restrict what customers can do in the portal
// Configure in Stripe Dashboard → Billing → Customer portal
});
return Response.redirect(session.url);
}
Metered Billing
// Charge customers per unit of usage (API calls, tokens, seats, GB)
// 1. Create a price with usage_type='metered' in Stripe Dashboard
// 2. Report usage from your app
async function reportUsage(subscriptionItemId: string, quantity: number) {
await stripe.subscriptionItems.createUsageRecord(subscriptionItemId, {
quantity,
timestamp: Math.floor(Date.now() / 1000),
action: 'increment', // Add to current period's total
});
}
// Track usage in your app, then report to Stripe
async function handleApiRequest(userId: string) {
// ... handle the request ...
// Report usage (can be async - don't block the response)
const user = await db.users.findUnique({ where: { id: userId } });
await reportUsage(user.stripeSubscriptionItemId, 1).catch(console.error);
}
// Usage is aggregated by Stripe for billing at period end
// Customer sees itemised usage in their invoice
Stripe Connect for Marketplaces
// Stripe Connect: your platform routes payments to seller accounts
// Two models: Destination Charges (simpler) and Separate Charges + Transfers
// Onboard a seller (Standard account - seller has their own Stripe account)
async function onboardSeller(sellerId: string) {
const account = await stripe.accounts.create({ type: 'standard' });
await db.sellers.update({ where: { id: sellerId }, data: { stripeAccountId: account.id } });
const link = await stripe.accountLinks.create({
account: account.id,
refresh_url: 'https://platform.example.com/seller/reauth',
return_url: 'https://platform.example.com/seller/onboarded',
type: 'account_onboarding',
});
return link.url;
}
// Charge buyer and route to seller minus your platform fee
async function createPayment(amount: number, sellerStripeAccountId: string) {
const paymentIntent = await stripe.paymentIntents.create({
amount, // in cents
currency: 'usd',
application_fee_amount: Math.floor(amount * 0.1), // 10% platform fee
transfer_data: { destination: sellerStripeAccountId },
});
return paymentIntent.client_secret;
}