Stripe's JavaScript SDK now runs natively on Cloudflare Workers
Stripe has long been a go-to for developers building payment infrastructure, evolving from simple transactions to full marketplace management through products like Connect. But for Cloudflare Workers users, there was a catch: the official Stripe JS SDK depended on Node.js core modules such as net/http, which don't exist in the V8-based Workers runtime. That forced developers to work directly with Stripe's REST API, losing conveniences like editor type-checking and straightforward calls such as stripe.customers.create().
That gap has now closed. The stripe package is generally available for Cloudflare Workers, meaning you can install it with a simple npm i stripe and use the SDK as you would in any Node.js environment. This follows Cloudflare's earlier commitment to expand compatibility for popular JavaScript libraries within Workers.
Pairing Stripe with Cloudflare Pages Functions
The timing is particularly useful given the recent addition of serverless functions to Cloudflare Pages. You can now drop Stripe into a Pages project—handling payments for a digital product or managing subscriptions—by adding just a few lines of JavaScript in the new functions folder. No extra configuration is required, and the functions scale automatically alongside your site.
To make this concrete, Cloudflare has published an example repository demonstrating Stripe Checkout integration on Pages. The sample accepts payments and redirects users to Stripe's hosted checkout page, all from a minimal serverless function:
import Stripe from 'stripe/lib/stripe.js';
// use web crypto
export const webCrypto = Stripe.createSubtleCryptoProvider();
export function getStripe({env}){
if(!env?.STRIPE_KEY){
throw new Error('Can not initialize Stripe without STRIPE_KEY');
}
const client = Stripe(env.STRIPE_KEY, {
httpClient: Stripe.createFetchHttpClient(), // ensure we use a Fetch client, and not Node's `http`
});
return client;
}
export default {
async fetch(request, env) {
const stripe = getStripe({ env })
const session = await stripe.checkout.sessions.create({
line_items: [{
price_data: {
currency: 'usd',
product_data: {
name: 'T-shirt',
},
unit_amount: 2000,
},
quantity: 1,
}],
payment_method_types: [
'card',
],
mode: 'payment',
success_url: `${YOUR_DOMAIN}/success.html`,
cancel_url: `${YOUR_DOMAIN}/cancel.html`,
});
return Response.redirect(session.url)
}
}
You can see the live demo at stripe.pages.dev or explore the source code on GitHub.
Beyond payments: webhooks and templates
Native SDK support means you're not limited to just initiating payments. Any Node.js example from Stripe's documentation now works in Workers without modification. That's especially valuable for webhook handling—you can validate incoming requests securely and run business logic at the edge without provisioning any new infrastructure:
import Stripe from 'stripe/lib/stripe.js';
// use web crypto
export const webCrypto = Stripe.createSubtleCryptoProvider();
export function getStripe({env}){
if(!env?.STRIPE_KEY){
throw new Error('Can not initialize Stripe without STRIPE_KEY');
}
const client = Stripe(env.STRIPE_KEY, {
httpClient: Stripe.createFetchHttpClient(), // ensure we use a Fetch client, and not Node's `http`
});
return client;
}
export default {
async fetch(request, env) {
const stripe = getStripe({ env })
const body = await request.text()
const sig = request.headers.get('stripe-signature')
const event = await stripe.webhooks.constructEventAsync(
body
sig,
env.STRIPE_ENDPOINT_SECRET,
undefined,
webCrypto
);
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
// Then define and call a method to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent);
break;
case 'payment_method.attached':
const paymentMethod = event.data.object;
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached(paymentMethod);
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
// Return a response to acknowledge receipt of the event
return new Response(JSON.stringify({ received: true }), {
headers: { 'Content-type': 'application/json' }
})
}
}
Alongside the SDK support, Cloudflare and Stripe have released a shared Workers template to help you get started quickly. It covers both initiating payments and validating webhooks from a single codebase, following recommended practices from both companies. The goal is to have you accepting payments in under five minutes, with no server setup or scaling overhead.
"We're big fans of Cloudflare Workers over here at Stripe. Between the wild performance at the edge and fantastic serverless development experience, we're excited to see what novel ways you all use Stripe to make amazing apps."— Brian Holt, Stripe



