Creating a Custom API in Drupal 10: A Step-by-Step Guide
Listen to the article podcast:
Creating a Custom API in Drupal 10: A Step-by-Step Guide
Drupal, as a powerful content management framework, offers a wide range of modules for API integrations. But sometimes, you need to craft a tailor-made API for specific needs. In this guide, we'll walk you through creating a custom API in Drupal 10.
Prerequisites:
A Drupal 10 installation.
Familiarity with YAML and basic PHP.
Step 1: Setting Up Your Custom Module
Navigate to your Drupal installation's modules/custom directory. If the custom directory doesn't exist, create it.
Inside custom, create a new directory for your module, e.g.,custom_api.
Step 2: Defining Your Module
Inside the custom_api directory, create a file named custom_api.info.yml.
Add the following content to custom_api.info.yml:
name: "Custom API"
type: module
description: "Provides a custom API."
core_version_requirement: ^10
package: Custom
dependencies:
- drupal:node
version: "1.0"
project: "custom_api"
Step 3: Routing Your API Endpoint
In the custom_api directory, create a file named custom_api.routing.yml.
Define your custom endpoint in custom_api.routing.yml:
custom_api.content:
path: "/custom-api"
defaults:
_controller: '\Drupal\custom_api\Controller\CustomApiController::content'
_title: "Custom API Endpoint"
requirements:
_permission: "access content"
Step 4: Implementing the API Logic
Inside the custom_api directory, create a new folder structure: src/Controller.
In the Controller folder, create a file named CustomApiController.php.
Add the following content to CustomApiController.php :
<?php
namespace Drupal\custom_api\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Provides route responses for the Custom API module.
*/
class CustomApiController extends ControllerBase {
/**
* Returns a simple JSON response.
*/
public function content() {
return new JsonResponse([
'data' => 'This is some custom API data.',
'method' => 'GET',
]);
}
}
Step 5: Enabling Your Custom Module
Navigate to the Extend page on your Drupal site (/admin/modules) or use Drush:
drush en custom_api -y
Enable the Custom API module.
Step 6: Testing the Custom API
In your web browser, go to
http://your-drupal-site.com/api/custom
You should see a JSON response:
{"data":"Hello from the custom API!","method":"GET"}
Conclusion:
Creating custom APIs in Drupal 10 is straightforward. With the foundational understanding provided in this guide, you can expand your API, add authentication, and further customize it according to your needs. Whether you're integrating with third-party platforms or building custom front-end applications, Drupal's flexibility allows you to tailor your API endpoints precisely.
I hope this guide serves as a helpful resource for your article on creating custom APIs in Drupal 10. If you need any further information or additional sections, please let me know!


Kiran Chaulagain
kkchaulagain@gmail.com
Software engineer and DevOps practitioner with 6+ years of experience turning ideas into scalable web applications and reliable infrastructure. Passionate about clean code, automation, and bridging the gap between development and operations.