stripe-payments

How to expand items.data.price.product in a stripe subscription


Given the API documentation about the Subscription object I should be able to use

stripe subscriptions list --customer cus_QgBmuycCay7CGu --expand data.items.data.price.product

However, I get this error:

{
  "error": {
    "message": "You cannot expand more than 4 levels of a property. Property: data.items.data.price.product",
    "request_log_url": "https://dashboard.stripe.com/test/logs/req_OkWSMMvr4CH5i1?t=1723990163",
    "type": "invalid_request_error"
  }
}

Solution

  • It seems that you can't handle multiple layers in stripe so you can't chain different objects which >= 4 as shown from your query

    data.items.data.price.product

    You can do this first

    stripe subscriptions list --customer cus_QgBmuycCay7CGu --expand data.items.data.price
    

    Then run this command after:

    stripe prices retrieve price_ID --expand product

    You can go further to create a customized function

    import stripe
    
    # Set your secret key
    stripe.api_key = "your_secret_key"
    
    # First, get the subscriptions with expanded prices
    subscriptions = stripe.Subscription.list(
        customer="cus_QgBmuycCay7CGu",
        expand=["data.items.data.price"]
    )
    
    def get_products(subscriptions):
       # Now, get the product details for each price
       for sublist in subscriptions.data:
           for item in sublist.items.data:
              price_id = item.price.id
              price = stripe.Price.retrieve(price_id, expand=["product"])
           print(price.product)