2 min read

How to create an API Key

You can create a new API key in the User Directory. Here you can generate a token and assign a role for the permissions.

Directus has a powerful API that allows you to connect your database with your projects. To start using the API, you will need an API key. This article will cover how to create an API key through Directus.

To create an API key, login to Directus and open the User Directory. Create a new user without a password and generate a token at the bottom.

The User Directory section contains all your Directus users. An API key is treated like any other user including the ability to assign a role with granular permissions.

Step 1 Create a User

Open the User Directory.

Example of the User Directory
c

Click the Plus (+) icon to add a new user.

Example of a new user

Use the First Name to give the user a descriptive name that appears in the internal activity logs. The Last Name can be used to group API accounts together using a common value such as SVC. The Email field is optional if you want to set up any notifications on the account's actions.

It is important to leave the password field blank so no one can log in using this account.

Step 2 Generate a Token (Key)

Scroll to the bottom of the page and you'll see the Token field. Click the Plus inside the field.

Example of token generation

Next choose a Role for the API. Avoid using the Administrator role as this has its risks. Instead, create a new role that is specific to the function of this account. I created one called API, but you can create one using the Plus icon within the role field, then set the permissions to what you need. Give it a name the relevant to the purpose such as "Website API Read Only".

Example of a Token and Role
?
Make sure to copy the token somewhere safe to use later.

Save changes using the big tick at the top.

Conclusion

Now you know how to create new API keys and how to assign them to a role. Be sure to read how you can customize the permissions and secure your API key.

If you keen to know how to use your API key to Authentication your API queries, check out this article:

How to use API Authentication in Directus
Every API is different when it comes to Authentication but in Directus, all you need is a Bearer token.

By continuing to use our website, you consent to use essential cookies. We also use optional tracking cookies which help us gather statistics to improve our services. Do you consent to these cookies?

I Consent Do not track