Asked 1 month ago by MartianEnvoy338
How can I retrieve product-specific coupon associations from Stripe's API?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by MartianEnvoy338
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
The Stripe dashboard lets you associate coupons or promotions with specific products or prices (for example, a 50% off coupon for an annual subscription).
I’m trying to replicate this logic via the API. When a user enters a coupon code (e.g., 50OFFANNUAL
) in our checkout, I need to verify the coupon exists, check its restrictions (single-use, time-limited, usage limits, etc.), and ensure it applies only to the intended product/subscription.
I can retrieve the coupon code using the following code:
PHP$stripeCouponList = $stripe->promotionCodes->all([ 'limit' => 1, 'code' => $upperCode, 'active' => true, ]);
However, the API response does not include any products
or prices
objects, even when trying to use expand
. Similarly, querying from the product or price side doesn’t show a promotion_code
or promotion_codes
property. How can I obtain the associated product or price details from the API?
Comments 0
•Answers 2
•Views 32
Answer by TychoAI Agent • 1 month ago
Stripe’s API doesn’t expose the product or price associations you see in the dashboard with promotion codes. When you retrieve a promotion code (and its coupon), the response only includes the coupon’s discount details and generic restrictions (like duration or redemptions), but it does not include an “applies_to” field or a direct link to products/prices.
In other words, while the dashboard lets you attach a coupon to a product (or price) for a given use-case, that coupling isn’t surfaced through any API property on the promotion code or coupon object. This means you cannot expand or retrieve associated product or price details directly from the promotion code endpoints.
What does this mean for your use-case?
Example Enhancement
If you want to programmatically enforce product restrictions, consider creating the coupon (or promotion code) via the API and using metadata to record the associated product or price IDs:
PHP$stripeCoupon = $stripe->coupons->create([ 'percent_off' => 50, 'duration' => 'once', 'metadata' => [ 'allowed_price_id' => 'price_12345678', // your associated price/product reference ], ]);
Then, when a user enters a coupon code, retrieve the promotion code, check the linked coupon, and validate the purchase against the stored metadata.
At the moment, though, if you set the association only via the dashboard without saving it in metadata or your own system, the API won’t return that information.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by NovaWatcher292 • 1 month ago
https://docs.stripe.com/api/promotion_codes/object#promotion_code_object-coupon-applies_to is the field.
To get it:
https://docs.stripe.com/expand#lists + https://docs.stripe.com/expand#includable-properties since applies_to
is not included by default.
PHP$promoCodes = $stripe->promotionCodes->all([ 'code' => $upperCode, 'active' => true, 'expand' => ["data.coupon.applies_to"] ]); foreach ($promoCodes->autoPagingIterator() as $promoCode) { print_r($promoCode->coupon->applies_to); }
No comments yet.
No comments yet.