Using JSON:API in Drupal

Drupal's JSON:API provides a standardized and efficient way to expose and consume Drupal content as JSON data, facilitating integration with front-end frameworks and external applications in a decoupled architecture.

This course covers the installation and configuration of the JSON:API module, making resource requests, handling relationships, and using advanced features like sparse fieldsets and filtering collections. Additionally, learners will explore error handling, security considerations, and best practices for building robust and efficient Web Services APIs using Drupal core’s JSON:API module. By the end of the course, you will be equipped to implement and secure JSON:API endpoints in your Drupal projects.

Key topics

  • Install and set up the JSON:API module
  • Understand how to make HTTP requests to retrieve data from Drupal using JSON:API
  • Explore how JSON:API represents and manages relationships between different resources
  • Learn to request, sort, and paginate lists of entities using JSON:API collections
  • Discover how to embed related resources in a single request
  • Limit the data returned in responses to only the fields you need
  • Apply filters to collections to retrieve specific subsets of data
  • Error handling and application security
Tutorials in this course
Categories
Drupal 8, 9, and 10
More information

The JSON:API module is our recommended starting point for creating REST APIs with Drupal. JSON:API module is now part of Drupal core as of 8.7, so installing the module no longer requires a separate download step.

In this tutorial we'll:

  • Walk through installing the JSON:API module for Drupal
  • Look at what you get out of the box with the JSON:API module

By the end of this tutorial you should be able to install the JSON:API module, and know what tools it provides you with.

Categories
Drupal 8, 9, and 10
More information

Being able to retrieve resources from an API is a fundamental first step.

In this tutorial we will learn how to:

  • Issue an HTTP request to extract information about a node from the JSON:API server
  • Examine the response from the server

By the end of this tutorial you should know how to use an HTTP Get request to return a resource from the JSON:API server, and know what the default response for the resource will contain.

Categories
Drupal 8, 9, and 10
More information

Drupal allows for a rich data model where entity reference fields can be used to relate any number of different items together in different ways. The data models that you can build with Drupal are often prolific in relationships, which means we need a way to handle these in our API. While Drupal treats a field with a string, and a field with an entity reference the same, JSON:API distinguishes between attributes and relationships.

In this tutorial we'll:

  • Look at how JSON:API represents relationships between two or more resources
  • How to distinguish between an attribute and a relationship in a response object
  • Learn about what information is available for each relationship and how we can use it

By the end of this tutorial, you should have a better understanding of how the JSON:API specification represents relationships modeled using Drupal entity reference fields.

Categories
Drupal 8, 9, and 10
More information

JSON:API includes a way to request a list of entities of a given resource from the server. Collections are the best way to find content based on filters, and to build listings into the consumers. Moreover, collections can be combined with all the options you can apply to a single resource, like sparse fieldsets and includes.

In this tutorial we'll:

  • Learn about what collections are in JSON:API
  • Learn how to request, sort, and paginate lists of content

By the end of this tutorial you should know how to retrieve a list of resources from the JSON:API server, and how to optionally sort and paginate the items in the list.

Categories
Drupal 8, 9, and 10
More information

Embedding resources at the consumer's demand is one of the crucial features of a modern API. We mentioned in Modern Web Services with JSON:API and GraphQL that multiple round trips to the server is harmful for performance. This issue can be overcome by making a request that embeds any required related resources into the response for the resource we're retrieving.

In this tutorial, we'll learn how to use JSON:API's include parameter to embed resources in a response.

By the end of this tutorial, you should be able to make a single request that retrieves multiple embeded resources in order to improve the performance of your application when interacting with a JSON:API server.

Categories
Drupal 8, 9, and 10
More information

By default, the JSON:API returns all the available data for an object in its response. Using JSON:API sparse fieldsets you can increase the performance of your consumer application by reducing the fields in the returned response object to just those that you need.

In this tutorial, we will learn how to reduce the output to get exactly the information that we need from the API.

This is one of the most important features of modern APIs like JSON:API.

By the end of this tutorial, you'll know what sparse fieldsets are, the role they fulfill, and how to use them when requesting data from a JSON:API server.

Categories
Drupal 8, 9, and 10
More information

Collections are a very powerful feature because they allow us to access multiple items at the same time. However, in many situations we do not want to access all the entities of a given type, but only the ones that meet some specific criteria. In order to reduce the set of entities in the collection to the ones we care about, we use filters.

In this tutorial we will:

  • Look at the filter query string parameter and how it can be used with JSON:API collections
  • Learn how to use filters in combination with the JSON:API module for Drupal to reduce the list of entities in a collection

By the end of this tutorial you should be able to request a list of entities in the form of a JSON:API collection and filter that list to include only the entities that match a specific set of requirements.

More information

Includes and filters are really powerful features. When combined together you can achieve almost any query your consumer application needs. Fancy filters we mentioned in a previous tutorial allow us to filter a collection based on fields of related entities, in addition to the fields directly under that entity.

In this tutorial we will:

  • Learn about filtering based on data in related resources
  • Filter based on multiple conditions and multi-value fields
  • Demonstrate how to filter a collection of articles based on author or tags

By the end of this tutorial you should be able to use nested filters in conjunction with relationships to further refine the list of content returned in a JSON:API collection.

Categories
Drupal 8, 9, and 10
More information

Sometimes unexpected things happen and Drupal needs to generate an error. The JSON:API specification describes how the server should return those errors. Understanding what to expect allows consumers to plan for errors and react gracefully.

In this tutorial we will:

  • Discuss how HTTP errors are used in conjunction with JSON:API
  • Learn about how JSON:API embeds information about the error encountered into the response object

By the end of this tutorial, you should have a basic understanding of the types of errors you can expect to receive when making JSON:API requests and what you can do to handle them.

More information

When you enable the JSON:API module you're significantly increasing the attack surface of your application. So it's a good idea to make sure that you understand the implications of doing so, and how to mitigate potential security issues. In most cases it doesn't require much work to do, but it's worth taking the time to make sure you've done it right.

In this tutorial we'll learn:

  • What JSON:API already does to keep you secure
  • How to protect against common attacks
  • How to limit access to resources exposed by JSON:API

By the end of this tutorial you should know what to look for when auditing your JSON:API configuration to help prevent against common attacks.

This course appears in the following guides:
Module Development
Use web services modules like JSON:API to build an API for your decoupled Drupal site.

Decoupled Headless Drupal