Issue Integrating Stripe with Cloudflare Workers: Script will never generate a response

I’m encountering an issue while integrating Stripe into my Workers project. Everything works perfectly in my local environment, where I can create Stripe checkout sessions and access the billing portal through an API endpoint that returns the URL for Stripe checkout. However, upon deployment to Cloudflare, sometimes I receive an internal server error with the message that the script never generates a response and sometimes it just works. I’ve tried using both the Stripe SDK for Node.js and direct API calls, but both approaches result in the same error. Here are the code snippets I’ve used: Direct API Call:
const response = await fetch('https://api.stripe.com/v1/billing_portal/sessions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.STRIPE_SECRET_KEY}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
customer: stripeId,
return_url: returnUrl
})
});
const response = await fetch('https://api.stripe.com/v1/billing_portal/sessions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.STRIPE_SECRET_KEY}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
customer: stripeId,
return_url: returnUrl
})
});
Stripe SDK
await stripe.billingPortal.sessions.create({
customer: stripeId,
return_url: fullUrl,
});
await stripe.billingPortal.sessions.create({
customer: stripeId,
return_url: fullUrl,
});
The same issue occurs when creating a checkout session. Has anyone faced a similar issue or have any ideas on how to resolve this? Any help would be greatly appreciated!
3 Replies
Chaika
Chaika5mo ago
Script will never generate a response usually means your request is somehow awaiting a promise from a different request, either from a library like storyblok or something else. All work/promises for a request must be completed in that request, can't use static/globals to make them persist and await them later If it doesn't work even with a direct fetch it's nothing to do with the fetch and to do with something else/another library you are using in your code ps. nice chatgpt reformatting lol
Karim Ahmed
Karim AhmedOP5mo ago
I forgot to mention it works sometimes and sometimes it's giving me that error, so I believe it's not a problem with a library.
Chaika
Chaika5mo ago
right, some libraries who have had that issue before like with storyblok have the same behavior because only after a certain amount of requests would it try to throttle them and then await it in a different request, just depends what's causing it. You can try your fetch request alone in a plain worker and it won't cause any issues
Want results from more Discord servers?
Add your server