How to change id of a key meanwhile keep quota and quota_remaing

Hi,

I used to using API-KEY for authentication. So If someone subscribe a plan, I would create a key with some quota for him. After a time, may this guy consider his key is leaked, so he want to change key.
The problem is if I create a new key the quota_remaing would be 0, so how can I get a new key that inherit quota_remaing from old one

Thank you in advance !

Hello @bomb_magical and welcome to the community. Can you explain in detail when you say “The problem is if I create a new key the quota_remaing would be 0”?

A quota_remaining value with 0 means that the quota has been exhausted. Unless quota_max has also been set to 0 or -1. Which means that an unlimited quota has been specified. I will have to check and get back to you if quota_remaining can be modified directly (but I am not sure it can).

I know you could use the Create Custom Key / Import Key to create a new key. You would export the key definition the of the previous key with the quota intact and modify the quota_max value to be the same as the quota_remaining. This way the remainder of the quota is what the previous user will start with and use until exhaustion.

Oh… take back, actually I wana say “The problem is if I create a new key the quota_remaing would be equal to quota_max”.

Let me rephrase it, I wana if there is way I could change id of key and all other fields keep unchaged

When you say ID of the key, do you mean the value of the key or the alias of the key. In this case value means the string of characters used in authorizing an API. And the alias is simply the custom identifier of the key. It can be seen in the key definition a shown below

image

Yes, you can definitely change both. However, not all values may remain the same. For example
the last_updated and the date_created fields are generated internally and would be modified.

Another one and one you already know is the quota_remaining will take the value from the quota_max. I am waiting on feedback if this can actually be modified.

In the meantime, you could temporarily give the user a new key based on the remaining quota. You can use the Create Custom Key / Import Key. So what you want is:

new quota_max = old quota_remaining
new quota_remaining = old quota_remaining
new quota_renewal_rate = 0
new quota_renews = 0

This way the user would at least get to finish the quota he had on the old key and the quota would not renew.

Then afterwards you can provide him with a new key with the quota_max set to the initial figure.

@bomb_magical

Found the answer to it. Currently there is a bug where import existing keys does not add the quota already used.

Like the keys which have an apikey- prefix, quotas already used also have a quota- prefix as well. The quota- prefix contains the amount of the used quotas. For example

quota-40dcaf04=175

This value is used to calculate the quota remining in the key.

So, to mitigate and solve your issue, you would need to create a key using the Create Custom Key / Import Key as earlier suggested. Then add a quota-{KEY_HASH} in the RedisDB. So for example, if the hash created for the key is 40dcaf04, then you would need to run the SET Redis command

SET quota-40dcaf04 "75"

Note: The value must be the currently used quota.

Once this is done. You can now make your requests and the quota of the key should update as normal.

Let us know if you got this working.