Skip to main content

An Introduction to Drupal JSON:API Module

Learn JSON:API in Drupal 10 - An introductory guide to creating APIs with the JSON:API module in Drupal's ecosystem. Explore the basics, configuration, and interaction with the API.
Drupal
10 min. read
İçerik Yönetim Sistemi (CMS) Seçimi

JSON:API is a powerful and standardized API specification. Drupal core provides a module named 'JSON:API' in Drupal 8 and later versions, which implements the JSON:API specification. This module offers a seamless way to create APIs within the Drupal ecosystem, in addition to other methods like the Rest Web Service module.

What is JSON:API?

JSON:API is an API specification that defines how client applications can request and receive resources from a server. It follows a set of conventions for structuring requests and responses, making it easier for developers to interact with the API. The specification ensures consistency, simplifies data retrieval, and reduces the need for custom configurations when building APIs.

For more detailed information about JSON:API, you can refer to the official documentation at https://jsonapi.org.

Now, let's dive into the steps to create a JSON:API in Drupal:

Enable the JSON:API Module

To get started, ensure that your Drupal website has the JSON:API module enabled. Fortunately, the module is included in Drupal 8 and later versions as a core module, so there's no need to install any additional extensions.

To verify if the module is enabled, navigate to Extend in your Drupal administration menu and check if the JSON:API module is listed and enabled. If it's not, you can enable it from there.

Image removed.

Configuring the JSON:API Module

The JSON:API module in Drupal offers limited configuration options by default, focusing primarily on specifying allowed operations.

Image removed.

However, when you find the need to modify resource properties such as API paths, resource counts, or defaults for includes, or if you desire an interface to disable fields or resources, you can utilize the JSON:API Extras module. JSON:API Extras extends the capabilities of the JSON:API module, providing additional features and flexibility for customizing your API's behavior to better suit your specific project requirements.

JSON:API-extras-module-configuration-page

Interact with the JSON:API

Once you have enabled and configured the JSON:API module, it's time to test and interact with your newly created API

Reading resources

Example URL: http://localhost/jsonapi/node/article

This URL will fetch data from the article node resource type using the JSON:API and display the results in JSON:API specification format. By following the specified URL pattern and changing the node/article part, you can interact with other resource types or access specific entities within your Drupal website.

JSON:API-postman-request-response
Authentication for CRUD Operations

Interacting with the JSON:API is possible using tools like Postman or cURL. By default, when making requests with these tools, the user is considered "anonymous", as they do not handle user authentication automatically. This may limit access to certain resources based on permissions granted to anonymous users in your Drupal configuration.

However, relying solely on anonymous permissions won't be enough if you intend to create, update, or delete resources through the JSON:API. For these operations, user authentication is necessary. Drupal offers various authentication mechanisms through different modules, and one of the commonly used methods is HTTP Basic Auth, which is available in Drupal core. Additionally, more comprehensive modules like Drupal REST & JSON API Authentication can provide additional authentication methods and customization options.

Steps to make an authenticated request

  1. For this example make sure all operations are enabledImage removed.
  2. Enable the HTTP Basic Authentication moduleImage removed.
  3. Create a role for API User

    Image removed.

  4. Give necessary permission for the role. /admin/people/permissions/api_user
  5. For this example make sure to give *Article*: Create new content permission
  6. Create a user with the API User permission. /admin/people/createImage removed.
  7. Configure authorization in postman. User the newly created account

    Image removed.

  8. Add Content-Type header application/vnd.api+jsonImage removed.
  9. Update the body:

    {
             "data": {
               "type": "node--article",
               "attributes": {
                 "title": "This page is created trough JSON:API",
                 "body": {
                   "value": "some body text",
                   "format": "plain_text"
                 }
               }
             }
            }
  10. Send the request and observe the response. If the status code is 201 you successfully made an authenticated request to your JSON:API and created a node with type articlearticle-node-created

Conclusion

JSON:API is a powerful and standardized API specification that makes creating APIs in Drupal a breeze. Its seamless integration as a core module in Drupal 8 and later versions provides developers with a standardized API interface, reducing the need for custom configurations. By enabling the JSON:API module, you can effortlessly interact with your Drupal website's data using clear URL patterns and JSON:API specification format.

For further usage and exploring additional capabilities of the JSON:API module, refer to the Drupal JSON:API Module Documentation. This extensive documentation offers valuable insights into advanced features, including pagination, sorting, file uploads, and other details.

References and Further Reading

Our Offices

Drupart Locations

Our Officess

London

151 West Green Road, London, England

442038156478

[email protected]

Drupart R&D

GOSB Teknopark Hi-Tech Bina 3.Kat B3 Gebze - KOCAELİ

+90 262 678 8872

[email protected]

Newark

112 Capitol Trail Suite, A437 Newark DE, 19711

+17406666255

[email protected]

Wiesbaden

Hinterbergstraße 27
65207 Wiesbaden
Deutschland

+49 (0) 6151 – 492 70 23

[email protected]