#digdug

1 messages · Page 1 of 1 (latest)

fading driftBOT
ebon summit
#

Hello! Can you tell me more about your use case so I can provide the best recomendation?

quasi wigeon
#

I am listing all of our invoices in a table per metadata which we have set which is projectId. I want to retrieve just those that have the matching project id. When i use invoices.list i hit an api limit

ebon summit
quasi wigeon
#

Will this help with api limit error

ebon summit
#

You'll likely be making fewer requests, yes. How many Invoices are you trying to list at one time?

quasi wigeon
#

anywhere between 1-30

#

Does the size of the request affect the limit i guess is my question

ebon summit
#

Size of the request? You mean like the number of parameters you're passing to the API? If so, no, that has no impact on rate limits.

#

What specific rate limits are you hitting?

quasi wigeon
#

I am using invoices/list and i get an error 'Testmode request rate limit exceeded, the rate limits in testmode are lower than livemode. You can learn more about rate limits here https://stripe.com/docs/rate-limits.'

ebon summit
#

What does your code look like?

quasi wigeon
#

const stripe = require('stripe')(process.env.STRIPE_KEY);

export const handler = async (event) => {
const projectId = event.pathParameters.id;
try {
if (!projectId) {
throw new Error('Project ID invalid');
}

console.log("PROJECT ID IS ", projectId);

const invoices = await stripe.invoices.list({ limit: 100 });

const filteredInvoices = invoices.data.filter(
  (invoice) =>
    invoice.metadata.projectId === projectId && invoice.status !== 'draft'
);

const invoiceWithPaymentIntent = await Promise.all(
  filteredInvoices.map(async (invoice) => {
    if (invoice.payment_intent) {
      const paymentIntent = await stripe.paymentIntents.retrieve(
        invoice.payment_intent
      );
      return {
        ...invoice,
        paymentIntent,
      };
    } else {
      return invoice;
    }
  })
);

const headers = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Credentials': false,
};

const response = {
  statusCode: 200,
  headers,
  body: JSON.stringify(invoiceWithPaymentIntent),
};

console.log("Response:", response); // Log the response object

return response;

} catch (error) {
console.error("Error:", error); // Log the error

const response = {
  statusCode: 500,
  body: JSON.stringify({ message: 'An error occurred' }),
};

console.log("Response:", response); // Log the response object

return response;

}
};

ebon summit
#

How often is this handler function being called?

quasi wigeon
#

any time that we load a project

ebon summit
#

Okay, so I have some recommendations...

#

First, you don't need to get the Invoice and then separately get the Payment Intent with a separate API call. You can expand the Payment Intent into a full object when you retrieve the Invoice in the first place: https://stripe.com/docs/expand

#

If you continue to use any list APIs I encourage you to use auto-pagination with a lower limit, probably something in the 20-50 range to start, then tweak it as you go. The Stripe library will handle making the invidual requests for you behind the scenes; the limit only specifies how many results to get per API request. With auto-pagination the library will make as many requests as needed to get all of the results you want based on your other list parameters.

#

Finally, for this specific use case, switching from the list API to the search API should work much better. You can search the Invoices by metadata, so only the ones you want are returned instead of having to fetch all of them and then filter on your end.

quasi wigeon
#

Thank you for the advice. Ill put this together. One more thing when i use search i get this error Error: StripeInvalidRequestError: We were unable to parse your search query. Try using the format metadata["key"]:"value" to query for metadata or key:"value" to query for other fields.

#

const searchOptions = {
query: metadata["projectId"]:${projectId},
limit: 100,
};

const searchResults = await stripe.invoices.search(searchOptions);
ebon summit
#

The query needs to be a string... I think Discord may have messed up your code formatting though?

#

Try wrapping your code in triple backticks.

#

Like in Markdown.

quasi wigeon
ebon summit
#

Wrap the ${projectId} in double quotes.

quasi wigeon
#

l.ol I cant even test it message: 'Testmode request rate limit exceeded, the rate limits in testmode are lower than livemode. You can learn more about rate limits here https://stripe.com/docs/rate-limits.',

#

dang

ebon summit
quasi wigeon
#

its working on my front end now. I will touch back in a bit if the issue persists but will take some time to implement pagination, search

#

I think the issue was the additional calls to payment intent

#

Thank you so much for your assistance