In Stripe (using Next.js and Node.js), I'm trying to retrieve all products with the same key-value pair in their metadata:
example: retrieve all products with
metadata: {category: 'foo'}
In attempts to render category-specific pages, I don't want to list and parse through my whole catalog but I also know that products can only be retrieved through id (docs), inferring that I would have to:
list all products
for all products:
if metadata matches,
add id to array
for id array:
retrieve each
This is brute forcing though, and not a good idea.
What would be the best way to retrieve conditionally based on metadata?
Any help is appreciated.
Stripe’s API currently doesn’t support querying Products using metadata. You’d have to retrieve all Products and filter through them. Stripe does have a Search API [2] which is currently in Beta so you might want to write in to see if you qualify.
If you happen to keep a track of Products server-side in a database, you could add an additional column for category. You can query your database by category to find those specific Products and then include just those Product IDs using the ids [2] parameter when you retrieve them through Stripe.
[1] https://stripe.com/docs/search-api
[2] https://stripe.com/docs/api/products/list#list_products-ids