Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

RESTful APIs for Headless Drupal

Introduction to Headless Drupal

Headless Drupal refers to a setup where Drupal serves as a content management system (CMS) but does not handle the front-end presentation layer. Instead, it exposes content through APIs, allowing developers to use modern JavaScript frameworks like React, Vue, or Angular to create dynamic user interfaces. This architecture promotes flexibility, scalability, and improved performance.

What are RESTful APIs?

REST (Representational State Transfer) is an architectural style that uses standard HTTP methods (GET, POST, PUT, DELETE) to interact with web services. RESTful APIs allow for seamless communication between clients and servers. In the context of Headless Drupal, RESTful APIs enable external applications to retrieve, create, update, or delete content stored in Drupal.

Setting Up RESTful APIs in Drupal

To enable RESTful APIs in Drupal, follow these steps:

  1. Log in to your Drupal admin interface.
  2. Go to Extend and enable the RESTful Web Services module.
  3. Enable the Serialization module, which allows data to be serialized into JSON or XML format.

Once these modules are enabled, you can begin to configure RESTful endpoints.

Configuring RESTful Endpoints

To configure RESTful endpoints, navigate to Configuration > Web services > REST. Here you can define different resources such as nodes, users, and taxonomy terms. For instance, you can create a RESTful endpoint for the "Article" content type:

1. Select Content and then choose Article from the list.

2. Enable the HTTP methods (GET, POST, PATCH, DELETE) that you want to allow.

3. Set permissions for different user roles.

Making API Calls

After setting up your endpoints, you can start making API calls. Below are examples of how to interact with your Drupal API using JavaScript's Fetch API.

GET Request Example

fetch('https://your-drupal-site.com/api/node/article/1')

This request retrieves the article with ID 1. You can use the following code to handle the response:

fetch('https://your-drupal-site.com/api/node/article/1')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error('Error:', error));
                

POST Request Example

fetch('https://your-drupal-site.com/api/node/article', { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' } })

This request creates a new article. Below is an example of the request body:

{
    "type": [
        {
            "target_id": "article"
        }
    ],
    "title": [
        {
            "value": "New Article Title"
        }
    ],
    "body": [
        {
            "value": "Content of the new article.",
            "format": "basic_html"
        }
    ]
}
                

Authentication

To secure your RESTful API, you can implement authentication methods such as OAuth2 or Basic Auth. For Basic Auth, you need to ensure that the user has the necessary permissions to access or modify resources. You can set this up in People > Permissions.

Conclusion

With RESTful APIs, Drupal can serve as a powerful backend for your applications, allowing you to leverage its content management features while using modern front-end technologies. By following the steps outlined in this tutorial, you can set up and interact with RESTful APIs in your Headless Drupal setup efficiently.