FG
๐Ÿ”Œ APIs & SDKsStripe

An error occurred with our connection to Stripe. Request was retried 2 times.

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score52%
52%

Problem

Describe the bug I'm running a nextJS app project on Cloudflare workers. Using the latest Stripe node sdk version 20. On the production server when I try to create a checkout session from my api route, I see this error. `An error occurred with our connection to Stripe. Request was retried 2 times.` On localhost the exact same code is working perfectly fine. I double checked my env vars to make sure they're properly passed and correct in production, yet i'm still getting the error. ----- For now what I did is omit the SDK and just use a raw fetch request and it works in production. ----- To Reproduce 1. `npm create cloudflare@latest -- my-next-app --framework=next` https://developers.cloudflare.com/workers/framework-guides/web-apps/nextjs/ 2. `npm i stripe --save` 2. Create an api route like "/api/stripe-checkout" that uses the sdk like this `const session = await stripe.checkout.sessions.create({ ... })` This will cause the error 3. Deploy to Cloudflare workers 4. Call the endpoint Expected behavior The Stripe Checkout page should open the same way it does when I'm doing it from localhost. Code snippets [code block] OS macos Node version 21 Library version 20.0.0 API version 2025-11-17.clover Additional context _No response_

Error Output

error occurred with our connection to Stripe. Request was retried 2 times.`

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix Stripe SDK Connection Issue on Cloudflare Workers

Medium Risk

The error occurs due to the Cloudflare Workers environment having restrictions on outgoing network requests, which may not align with the Stripe SDK's internal handling of connections. The SDK might be trying to use a connection method that is incompatible with the Cloudflare Workers runtime, leading to the retries and eventual failure.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Switch to Raw Fetch for Stripe API Calls

    Since using the Stripe SDK is causing connection issues, revert to using raw fetch requests to interact with the Stripe API. This method is more compatible with the Cloudflare Workers environment.

    javascript
    const response = await fetch('https://api.stripe.com/v1/checkout/sessions', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${process.env.STRIPE_SECRET_KEY}`,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: new URLSearchParams({
        // Add your session parameters here
      })
    });
    const session = await response.json();
  2. 2

    Check Environment Variables

    Ensure that the environment variables, especially STRIPE_SECRET_KEY, are correctly set in the Cloudflare Workers environment. You can do this by logging the variable before making the fetch request.

    javascript
    console.log('Stripe Secret Key:', process.env.STRIPE_SECRET_KEY);
  3. 3

    Increase Timeout for Fetch Requests

    If the fetch request is timing out, consider increasing the timeout duration. This can help if the connection to Stripe is slow. You can implement a timeout mechanism around the fetch request.

    javascript
    const fetchWithTimeout = (url, options, timeout = 5000) => {
      return Promise.race([
        fetch(url, options),
        new Promise((_, reject) => setTimeout(() => reject(new Error('Request timed out')), timeout))
      ]);
    };
  4. 4

    Test API Endpoint

    After making the changes, deploy the updated code to Cloudflare Workers and test the API endpoint to ensure that the Stripe Checkout session is created successfully.

    bash
    curl -X POST https://your-cloudflare-worker-url/api/stripe-checkout -d '{...}'

Validation

Confirm the fix by testing the API endpoint in production. The response should return a valid checkout session object without errors. Additionally, check the logs for any connection issues.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

stripepaymentsapibug