Sometimes you would like to create an api key and automatically invalidate it after a certain number of uses.

Example: You are selling API access and a customer has purchased 1000 api requests from you.

Unkey allows you to set/update usage limits on individual keys and we take care of invalidating a key after it has reached its limit.

Example

Let’s create a new key, which can be used 100 times.

curl --request POST \
  --url https://api.unkey.dev/v1/keys.createKey \
  --header 'Authorization: Bearer <UNKEY>' \
  --header 'Content-Type: application/json' \
  --data '{
	"apiId":"<API_ID>",
	"remaining": 100
}'

Now when you verify the new key, you will receive back the remaining verifications and after all of them are used up, the key is invalid.

curl --request POST \
  --url https://api.unkey.dev/v1/keys.verifyKey \
  --header 'Content-Type: application/json' \
  --data '{
		"key": "<NEW_KEY>"
	}'
{
	"valid": true,
	"remaining": 99
}

The returned remaining value represents how many verifications are remaining after the current one. A value of 3, means you can verify the key successfully 3 more times.