#yuvi00001
1 messages · Page 1 of 1 (latest)
When you submit the usage records, you can set action field to set instead of using increment, so that it will overwrite the usage quantity at the specified timestamp: https://stripe.com/docs/api/usage_records/create#usage_record_create-action
so in order to overwrite a created usage I need to use set action with the previous created record's timestamp ?
If the Usage Records that were originally created with action:increment, you cannot just set final quantity since a single timestamp could be tied to multiple Usage Records that were added together. Instead, you need to retrieve the current total usage reported for that timestamp, subtract the quantity they want to delete, and use that value as the quantity in the creation request.
For example,
curl https://api.stripe.com/v1/subscription_items/si_xxx/usage_records \
-u sk_test_xxx: \
-d quantity={{TOTAL_USAGE - QUANTITY_TO_DELETE}} \
-d timestamp={{TIMESTAMP}} \
-d action=set
I just checked my code base, every time I want to increase user's usage, I increase the quantity of metered sub, by creating a usage-record with action as "set" , I think I was having some trouble using action as "increment" previously when I was developing this.
Is there a way to reduce quantity if all other usage were created with action as "set" ?
or is it ideal to create a usage with action set to "increment" and then use "set" to settle quantity.?
For Usage Records that were originally created with action:set, you can reset the quantity reported for that timestamp by setting to the new quantity.
For example,
curl https://api.stripe.com/v1/subscription_items/si_xxx/usage_records \
-u sk_test_xxx: \
-d quantity={{NEW_QUANTITY}} \
-d timestamp={{TIMESTAMP}} \
-d action=set
Like, within a billing cycle,
if on the first day, user uses my product for 10 mins , I create a usage record with action as "set".
then, if on 10th day, they use product for 20 mins, I create a usage record with action as "set".
then , if on last day of month, they use product for 30 mins ,I create a usage record with action as "set".
is this an ideal approach to handle metered usage ?
Have you tried to test with set with lower quantity?
I created a usage record for 10 quantity with x timestamp,
i created another request with 0 quantity with the same timestamp. But it didnt reduce the estimated price of my upcoming invoice.
Can you share the subscription ID (sub_xxx)?
I only see two requests that set to quantity of 10 on two different timestamps:
okay, I get it so I need to store recordID and timestamp if I want to overwrite them.
Yup!