fbpx

How to setup Drupal 8.x site as JSON rest API web services

Reading time: 4 minutes

Problem

How to Setup Drupal 8.x site for web services, rest API using JSON API module.

Final output

From below instructions, We able to setup Drupal 8.x site as web services for rest API using JSON API and REST module.

As we know the Drupal can be use as a powerful backend CMS. So for that we have to setup our Drupal environment to use it as web services rest API.

So, we can achieve it by using JSON API module providing in Drupal 8.x

If anyone interested in using Drupal as backend system and having a question in mind that “Is it possible to use Drupal only as a backend?” The Answer is Yes.

What is JSON:API in Drupal 8.x?
Specification for how a client should request that resources be fetched or modified, and how a server should respond to those requests.

JSON:API is designed to minimize both the number of requests and the amount of data transmitted between clients and servers. This efficiency is achieved without compromising readability, flexibility, or discoverability.

If you don’t know that how to setup and use Drupal site  as rest API, then you are on a right place.

So these are step by step easy guide to setup JSON API rest API in Drupal 8.x 🙂

Solution

1. Login to your Drupal 8 site then Go to Extend and scroll down to the bottom of the page. Install and enable all the modules under Web Services section.

Install Web services modules
Install and enable Web services modules

2. Now we have to install and enable another module which provides us a user interface for configuring web services rest modules easily.
The module is Rest UI

To install the module just go to Rest UI module page mentioned above and copy the link or address location for tar.gz file and paste it inside Install new module

Then simply to enable it go to Extend -> Web services section and enable REST UI module

Install Rest UI module

Awesome, Our basic structure to setup rest API’s and use our Drupal 8.x site as web services using JSON API module has been done.

3. After setup, let’s configure the JSON API module.

Go to Configuration -> Web services then JSON:API and Configuration JSON API module for your desired operations.

Configuration JSON API module
Configuration JSON API module

4. Now, have a look at configurations of the REST module.

Go to Configuration -> Web services -> REST

Here we can manage REST modules easily, By default Content REST resource is enabled for All operations.

NOTE
To use other operations like POST, PATCH and DELETE make sure to set allowed operation types inside JSON:API configuration as mentioned above.

Configuration REST module
Configurations of REST module

Finally the good news, we are all done and ready to do test with our first rest API for using Drupal 8.x site as web services.

We are going to test Login and Logout API’s by using Postman API testing tool.

Login rest API

* Request:
Method: POST
URL: {{BASE_URL}}/user/login?_format=hal_json
Body:

{
    "name": "[USRENAME]]",
    "pass": "[PASSWORD]]"
}

* Response:

{
    "current_user": {
        "uid": "1",
        "roles": [
            "authenticated",
            "administrator"
        ],
        "name": "admin"
    },
    "csrf_token": "Hwj05HZ5cadd5wRuHXYt_MMIOgKGas4xzJObDCjTPns",
    "logout_token": "zv4c1269f8lnUDYMInXIiHwDDTvJn7gZqT8YWOZhOhg"
}

In Response we will get some user information, csrf_token and logout_token

NOTE:
CSRF token
will be use later for authentication purposes.
Logout token will have to send when we do logout rest API.

Drupal 8.x Login rest API
Drupal 8.x Login rest API

Logout rest API

* Request:

Method: GET
URL: {{BASE_URL}}/user/logout?_format=hal_json&token=[LOGOUT_TOKEN]

NOTE: Pass Logout token which we get in Login API response as URL parameter

Drupal 8.x Logout rest API
Drupal 8.x Logout rest API

Congratulations now we can user our Drupal 8.x site as web services via rest API’s using JSON API and REST module 🙂

Share your love with us 😉

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Leave a Reply