Specbee: Exposing Your API in Drupal 9 – A Brief Tutorial

Exposing Your API in Drupal 9 – A Brief Tutorial Sahana N 20 Dec, 2021

Drupal 8 brought about significant architectural changes offering modern techniques to build digital experiences. REST API was one of them and it came shipped in core. A RESTful API is an API that conforms to the architectural style of REST (representational state transfer). It accesses and uses data via HTTP requests to GET, PUT, POST and DELETE data types. 

In this post, you will be learning more about exposing your API in Drupal 9.

Drupal 8 has now reached EOL. Now is the time to migrate to Drupal 9. However, if you’re still trying your hands out at creating a custom module in Drupal 8, these methods will work for a Drupal 8 setup too. 

Exposing API

HTTP methods used in REST API

GET : This method is used to read the data from server.
POST : This method is used to create data on a server.
PATCH : This method is used to update the data on the server.
DELETE : This method is used to delete the data from the server.

 

The above image describes how REST API requests and gives the response to a client. Initially the client sends the request using HTTP methods. REST API interacts with the database and sends the response in JSON or XML formats.

Now let’s see how it take place in Drupal.

Drupal Modules to Expose your API

Mentioned below are some Drupal core and contributed modules that can help in exposing your APIs in Drupal.

    1. HAL

The HAL module is specifically for JSON or XML, and it provides the serialization plugin.
    
    2. HTTP Basic Authentication

The HTTP basic authentication module provides an authentication plugin which allows us to control access to our API resources based on the HTTP method.

    3. RESTful Web Services

The RESTful web services module handles the resources plugin system as well as defining initial resource routes. A good example of resource plugin can be found in the DB log module which provides an API endpoint for watchdog entries.

Resource plugin is responsible for retrieving the data from the database.

    4. Serialization

The serialization module is responsible for deserializing and serializing request response data.
    
    5. REST UI

Rest UI is a helpful module which exposes our RESTful web services configuration via the admin UI. This way we can configure our API endpoints by editing the YML files.

REST Response – How It Works

REST Response

 

This image describes the pipeline of the REST response.

According to the image, first the client sends a request, and the request goes to the request handler. The Request Handler is responsible for accepting the request.

The request handler interfaces with the REST module and is responsible for the requests. The requests are then sent to deserialize to normalize the data. 

Then deserialize sends to the request handler. The request handler matches the up the resource with the appropriate resource handler. 

Again, request is sent to the Resource plugin. The resource plugin takes the data from incoming request and retrieves the valid information from the database.

Then the resource plugin returns the data to the request handler and again acts with the serializer before sending as response.

How to configure the REST API in Drupal using Core Modules:

Here are 4 easy steps to configure REST API in Drupal using core modules:

Step 1: Initially, you need to install the core modules which are HAl, Http Basic Authentication, REST Web Services, and serialization module.

Step 2: Next, you need to download the REST UI module and you need to enable the module.

Web Services

 

Step 3: Next, you need to configure the REST API endpoints as required. To configure, go to Configuration -> REST -> then click on edit of the content row.

REST Resources

 

Step 4: When you click on edit, you will get the edit window. Configure as required then save the configuration.

Configuration

 

Example:

If you want to get the response of a particular node, you need to send the request using the postman app with URL – hostname/node/nodeid?_format=json. (If you don’t have the Postman app yet, you can either download it from here or get the web version.)

Postman

 

 

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Specbee: Exposing Your API in Drupal 9 – A Brief Tutorial

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.