stripe / stripe-apps

Stripe Apps lets you embed custom user experiences directly in the Stripe Dashboard and orchestrate the Stripe API.
https://stripe.com/docs/stripe-apps
MIT License
148 stars 73 forks source link

Error while communicating with one of our backends #954

Closed NoamKashtan closed 8 months ago

NoamKashtan commented 8 months ago

Describe the bug I'm running Stripe Charges API with pagination and get an error message

To Reproduce This is the GET URL I'm sending (Using Airflow. To reproduce use Postman) and get this error message (https://api.stripe.com/v1/charges?limit=100&created[gte]=1708387200&created[lt]=1709683200&expand[0]=data.on_behalf_of&starting_after=ch_3OqoIjCU1g4y0fco10XXeE5G

Expected behavior Suddenly it started to happen.

Screenshots image

karlr-stripe commented 8 months ago

Hi! this doesn't seem related specifically to a Stripe App and is more an intermittent API error: https://docs.stripe.com/error-low-level#server-errors You'd need to reach out directly to Stripe support to look into the errors.

But in general if you're listing objects like this, please use auto-pagination and either do not set limit or set it to a small value. If you use something like the maximum limit of 100 the tradeoff is that each 'page' you retrieve is much larger and thus each individual API request has much more processing involved, and that will cause a higher chance of erroring like this. That's especially true if you're also using expand to include other objects in the response as that adds more processing to the API response. Regardless of what limit you pass you'll aways get the full results when using auto-pagination so there's no need to pass a high limit.

Please follow up with https://support.stripe.com/?contact=true for any questions about this error and we'd be glad to help!

bensontrent commented 8 months ago

In Stripe Apps, be sure you're using the API Key provided by the Stripe Apps Environment.

import { STRIPE_API_KEY, createHttpClient } from "@stripe/ui-extension-sdk/utils/httpClient";
import Stripe from "stripe";

const stripeClient = new Stripe(STRIPE_API_KEY, {
    httpClient: createHttpClient(),
    apiVersion: '2023-08-16',
});

const charge =  await stripeClient.charges.retrieve("ch_3OqoIjCU1g4y0fco10XXeE5G")
console.log(charge)

If you're calling another external API, usually you'll need to proxy those to add an "Access-Control-Allow-Origin" header set to *

For example to proxy the PrintNode API in your backend:

import cors from "cors";
import express from "express";

import { createProxyMiddleware } from 'http-proxy-middleware';

const app = express();

app.use(cors({
  credentials: true,
  origin: '*',
  optionsSuccessStatus: 200
}));

const PrintNodeProxy = createProxyMiddleware({
  target: 'https://api.printnode.com', // target host with the same base path
  changeOrigin: true,
  autoRewrite:true,
  logLevel: 'debug',
  pathRewrite: {
    [`^/api/printnode`]: '',
},
});
app.use('/api/printnode', PrintNodeProxy);

app.use(express.json());

const port = process.env.PORT || 3000;

app.listen(port, () => {
  console.log(`server started at http://localhost:${port}`)
})
NoamKashtan commented 8 months ago

Hey @karlr-stripe , Thanks a lot for your response! Will try your suggestions soon.

@bensontrent , Thanks. I guess the @karlr-stripe answer could be the root cause of this issue.