Quickstart
Create your first identity and key with Unkey
This quickstart will guide you through creating your first identity with shared ratelimits and a key that is connected to the identity.
The example is written in TypeScript, purposefully using the fetch
API to make requests as transparent as possible. You can use any language or library to make requests to the Unkey API.
Requirements
You will need your api id and root key to make requests to the Unkey API. You can find these in the Unkey dashboard.
The root key requires the following permissions:
Create an Identity
To create an identity, you need to make a request to the /v1/identities.createIdentity
endpoint. You can specify an externalId
and meta
object to store additional information about the identity.
Unkey does not care what the externalId
is, but it must be unique for each identity. Commonly used are user or organization ids. The meta
object can be used to store any additional information you want to associate with the identity.
Retrieve an Identity
Let’s retrieve the identity to make sure it got created successfully
Create a Key
Let’s create a key and connect it to the identity
Verify the Key
When you verify the key, you will receive the identity that the key is connected to and can act accordingly in your API handler.
Ratelimits
Ratelimits can be set on the identity level. Ratelimits set on the identity level are shared across all keys connected to the identity.
Verify the Key with Ratelimits
Now let’s verify the key again and specify the limits
In this case, we pretend like a user is requesting to use 200 tokens. We specify the requests
ratelimit to enforce a limit of 10 requests per day and the tokens
ratelimit to enforce a limit of 1000 tokens per minute. Additionally we specify the cost of the tokens to be 200.
That’s it, you have successfully created an identity and key with shared ratelimits. You can now use the key to verify requests and enforce ratelimits in your API handler.