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.
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:
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.
The JSON:API module in Drupal offers limited configuration options by default, focusing primarily on specifying allowed operations.
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.
Once you have enabled and configured the JSON:API module, it's time to test and interact with your newly created API
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.
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
Create a role for API User
*Article*: Create new content
permissionConfigure authorization in postman. User the newly created account
application/vnd.api+json
Update the body:
{
"data": {
"type": "node--article",
"attributes": {
"title": "This page is created trough JSON:API",
"body": {
"value": "some body text",
"format": "plain_text"
}
}
}
}
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.