#digdug
1 messages · Page 1 of 1 (latest)
Hello! Can you tell me more about your use case so I can provide the best recomendation?
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
'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.'
You can use the search API to get only the ones that have the metadata you're looking for: https://stripe.com/docs/api/pagination/search
Will this help with api limit error
You'll likely be making fewer requests, yes. How many Invoices are you trying to list at one time?
anywhere between 1-30
Does the size of the request affect the limit i guess is my question
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?
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.'
What does your code look like?
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;
}
};
How often is this handler function being called?
any time that we load a project
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
You can see in the API reference that the payment_intent property on an Invoice is expandable: https://stripe.com/docs/api/invoices/object#invoice_object-payment_intent
Also, when using our list APIs, auto-pagination is strongly recommended: https://stripe.com/docs/api/pagination/auto
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.
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);
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.
Wrap the ${projectId} in double quotes.
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
I'm not sure why you're getting that error unless you're making a ton of repeat requests. Can you give me one of the request IDs from a successful request so I can find out what's going on? Here's how you can find a request ID: https://support.stripe.com/questions/finding-the-id-for-an-api-request