Drupal

Using REST in Drupal 11: Complete Guide to API Setup & Authentication

Using REST in Drupal 11: A Complete Practical Guide

Using REST in Drupal 11 is actually pretty powerful—but the setup isn’t “automatic.”

You need to enable the right modules, configure endpoints, and (optionally) secure it with authentication.

Let me walk you through it in a clean, practical way 👇

🚀 1. Enable Required Modules

Go to:

/admin/modules

Enable these modules:

  • RESTful Web Services
  • Serialization
  • HAL
  • HTTP Basic Authentication (or OAuth if needed)

👉 Or via CLI:

drush en rest serialization hal basic_auth -y

⚙️ 2. Configure REST Resources

Go to:

/admin/config/services/rest

You’ll see a list like:

  • Content (node)
  • User
  • Comment

Example: Enable REST for Content (Node)

  • Enable GET / POST / PATCH / DELETE
  • Select formats: json
  • Authentication: basic_auth (or cookie)

🔗 3. Test REST API Endpoint

Example endpoint:

GET https://your-site.com/node/1?_format=json

Response:

{
  "nid": [{"value": "1"}],
  "title": [{"value": "My First Node"}]
}

🔐 4. Authentication (Important)

Option 1: Basic Auth

curl -u username:password \
https://your-site.com/node/1?_format=json

Option 2: Cookie (for logged-in users)

Use session cookies from browser.

📝 5. POST (Create Content)

curl -X POST \
  -H "Content-Type: application/json" \
  -u username:password \
  -d '{
    "type":[{"target_id":"article"}],
    "title":[{"value":"New Article"}]
  }' \
  https://your-site.com/entity/node?_format=json

🔄 6. PATCH (Update Content)

curl -X PATCH \
  -H "Content-Type: application/json" \
  -u username:password \
  -d '{
    "title":[{"value":"Updated Title"}]
  }' \
  https://your-site.com/node/1?_format=json

❌ 7. Common Issues (Very Important)

🔴 403 Forbidden

Check permissions:

/admin/people/permissions

Enable:

  • Access content
  • REST permissions

🔴 415 Unsupported Media Type

Missing header:

Content-Type: application/json

🔴 404 Not Found

Resource not enabled in REST config

⚡ 8. Best Practice (Real-world use)

Instead of default REST, many devs prefer:

👉 JSON:API (Recommended)

Enable:

drush en jsonapi -y

Then use:

/jsonapi/node/article

Why better?

  • No config needed
  • Cleaner responses
  • Standardized structure

🧠 9. Example (Frontend Integration)

If you’re using:

  • React / Next.js
  • Vue
  • Angular

You can fetch like:

fetch("https://your-site.com/jsonapi/node/article")

🏁 Summary

Feature REST Module JSON:API
Setup Manual Automatic
Structure Custom Standard
Recommended

Leave a Reply

Your email address will not be published. Required fields are marked *