API Key Authentication

Last updated on
19 September 2025

API Key Authentication is one of the simplest methods to protect Drupal REST APIs. Once you have generated API Keys for all your users, you can then use those keys to secure access to your Drupal REST APIs.

You can do so by sending the user’s Drupal username and API key in the Authorization header of your every API request. The Drupal API Authentication module will then authenticate the request based on the username and corresponding API key. This module is compatible with Drupal 8, Drupal 9, Drupal 10, and Drupal 11.

 Download    Know more

Setup Video:

 Drupal REST API Key Authentication Youtube Video

Pre-requisites: Download and Installation:

  • Download & install the Drupal REST & JSON API Authentication module.
  • REST UI: This module provides you with a user interface for configuring the REST module. 
  • Enable the following Web Services modules from under the Extend section(/admin/modules) of your Drupal site:
    • REST UI
    • RESTful Web Services
    • Serialization

Drupal API Authentication - Install web services

Enable the API and assign methods and operations as follows:

  • The first step is to enable the API and also assign methods and operations allowed on that particular API. This can be done using the REST UI module, or you can modify the config.
  • To enable the API using the REST UI module, click the Configure button of the REST UI module (as shown below).

Drupal API Authentication - configure REST UI module

  • In our example, we need to enable the /entity/user API. Do this by selecting the Enable option in front of it.

Drupal API Authentication - Select User Resource

  • Now, as our goal is to create a user in Drupal, select the following configs:
    • Method: POST
    • Format: json
    • Authentication provider: rest_api_authentication.
  • Selecting rest_api_authentication allows the miniOrange REST API Authentication module to authenticate your /entity/user API. Click the Save configuration button to continue.

Drupal API Authentication - Settings for resources for user

Setup API Key Authentication Method:

  • In this step, we will generate an API Key. To do this, navigate to the API Authentication tab of the module (/admin/config/people/rest_api_authentication/auth_settings).
    • Under Basic Configuration, enable the Enable Authentication toggle.
    • Enter the Application Name and select API Key from the Authentication Method section.

Drupal API Authentication - Select API Key method

  • Scroll down to the API Key Configuration section on the same tab.
    • Select the Authentication Type from the dropdown.
    • In the Enter Username text field, type the username for which you want to generate the API key, then click the Generate button.
    • Once the API Key is generated, click the Save Configuration button at the bottom of the page.
    • Click the Save Configuration button.
    • If you want to generate a key for all users, click the Generate Key for All Users button.

Note: This is a premium feature. In the free version of the module, authentication is handled using a universal key.

Drupal API Authentication - API Key Configuration

  • You have successfully configured the API Key Authentication method.

Note: Use the application-specific unique header when authenticating the API.

Drupal API Authentication - API Key Configured Successfully

  • You can now view the generated API key in the API Key field of your user profile.

Drupal API Authentication - Show API Key

  • If you want end users to also generate their API key, the API key management will be available to them if you enable the permission, as shown in the image.

Drupal API Authentication - Allowed Permissions of User

Grant Role Permissions to Create Users:

  • If needed, you can allow non-admin Drupal roles to create users. To do this, assign the Administer users permission to the desired roles from the Permissions page (/admin/people/permissions) of your Drupal site.

Drupal API Authentication - Admin User permission

That’s it! Now, let’s create a user in Drupal through an API call using the API key for authentication.

Example:

  • To create a user in Drupal, make a POST request with the user’s username and the API key issued by the miniOrange REST API Authentication module. Both the username and API key must be in Base64-encoded format. Refer to the format below to make the call.
HTML Request Format-

Request: POST  <your_drupal_base_url>/entity/user?_format=json

Header:      
             AUTH-METHOD: application_id
             Accept: application/json
             Content-Type: application/json
             API-KEY: base64_encoded<username:api-key>
             (The value should be Base64-encoded in the format: username:api-key.)

Body: 

                {
                "name": {
                    "value": "<username>"
                },
                "mail": {
                    "value": "<email>"
                },
                "pass": {
                    "value": "<password>"
                },
                "status": {
                    "value": "1"
                }
                }

CURL Request Format-

curl --location --request POST  ‘<your_drupal_base_url>/entity/user?_format=json' \
                --header 'AUTH-METHOD: application_id' \
                --header 'Accept: application/json' \
                --header 'Content-Type: application/json' \
                --header 'Authorization: Basic base64encoded<username:API key>’ \
                --data-raw '  

                {
                "name": [
                    { "value": "Username" }
                ],
                "mail": [
                    { "value": "email" }
                ],
                "pass": [
                    { "value": "Password" }
                ],
                "status": [
                    { "value": "1" }
                ]
                }
  • You can also refer to the Postman request image shown below.

Drupal API Authentication - Postman Request

  • A successful response will return the details of the user you created (see the image below).

Drupal API Authentication - Postman response

  • If you receive an error in the response, refer to the table below for the error description and possible solutions.
Error

Description

MISSING_HEADER

You will get this error whenever you don't send an Authorization Header in the API request or if it was removed by your server due to some reasons.

Example:
{
  "status": "error",
  "error": "MISSING_AUTHORIZATION_HEADER",
  "error_description": "Authorization header not received."
}

MISSING_API_KEY_HEADER

You will get this error whenever the API key sent in the API call does not match.

Example:

{
"status": "error",
"http_code": 401,
"error": "MISSING_API_KEY_HEADER",
"error_description": "API key header is missing."
}

MISSING_AUTHORIZATION_HEADER

You will get this error if you don’t send an Authorization header in the API request, or if your server removes it for some reason.

Example:

{
"status": "error",
"http_code": "401",
"error": "MISSING_AUTHORIZATION_HEADER",
"error_description": "Authorization header not received."
}

AUTHENTICATION_FAILED

You will get this error whenever the entered API key is incorrect.

Example:

{
"status": "error",
"http_code": 401,
"error": "AUTHENTICATION_FAILED",
"error_description": "Authentication failed."
}

INVALID_CREDENTIALS

You will get this error when either the username or password is incorrect.

Example:

{
"status": "error",
"http_code": "401",
"error": "INVALID_CREDENTIALS",
"error_description": "Invalid username or password."
}

USER_DOES_NOT_EXIST

You will get this error whenever the module does not find any account belonging to the username that you have sent in the request.

Example:
{
  "status": "error",
  "error": "USER_DOES_NOT_EXIST",
  "error_description": "The user does not exist."
}

Congratulations!!!! You can now authenticate any calls to your Drupal APIs using the API Key-Based Authentication method.

We hope you found this document useful and informative.

Contact our 24*7 support team

Feel free to reach out to our Drupal experts if you need any sort of assistance in setting up REST & JSON API Authentication on your Drupal site.   

 Get In Touch With Us Join Our Slack Channel

back to top Back to top  

Help improve this page

Page status: No known problems

You can: