Endpoint

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

Overview

Endpoint module is a framework which allows Drupal acts as a light endpoint.

The module was designed specifically for REST API. It contains a router that allows to follow REST style in your URLs. Here is an example:

GET /dogs
POST /dogs/create
DELETE /dogs/1
GET /dog/1
PUT /dog/1

Endpoint is really light, fast and flexible, that makes it a good solution for projects where Drupal role is mobile backend and single-page app backend.

Endpoint does not do anything itself but instead provides an API to other developers.

Prerequisites

Apache:
Add to your .htaccess file in the document root the following content:

# Rewrite API callback URLs of the form api.php?q=x.
RewriteCond %{REQUEST_URI} ^\/([a-z]{2}\/)?api\/.*
RewriteRule ^(.*)$ api.php?q=$1 [L,QSA]
RewriteCond %{QUERY_STRING} (^|&)q=(\/)?(\/)?api\/.*
RewriteRule .* api.php [L]

Nginx:
You may start from adding the following block to Nginx config:

# Rewrite API callback URLs of the form api.php?q=x.
location ^~ /api/ {
  location ~* ^/api/ {
    rewrite ^/(.*)$ /api.php?q=$1 last;
  }
}

Note: this code doesn't take into account API versions.

Development

Create api.php file with the following content in your site document root and modify it as you need:

/**
 * @file
 */

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/sites/all/modules/contrib/endpoint/includes/router.inc';

$routes = array(
  'api/v1/foos' => array(
    'GET' => array(
      'callback' => 'my_module_foo_list',
      'anonymous'  => TRUE,
    ),
    'POST' => array(
      'callback' => 'my_module_foo_create',
    ),
  ),
  'api/v1/foo/(\w+)' => array(
    'GET' => array(
      'callback' => 'my_module_foo_get',
    ),
    'POST' => array(
      'include' => DRUPAL_ROOT . '/sites/all/modules/custom/another_module/includes/some_callback.inc',
      'callback' => 'my_module_foo_update',
    ),
  ),
);

endpoint_route(array(
  'debug' => TRUE,
  'routes' => $routes,
//  'authorize callback' => 'my_module_callback_authorize',
//  'error callback' => 'my_module_callback_error',
));

function my_module_foo_list() {
  // ...
  return array('foos' => $foo_collection);
}

function my_module_foo_create() {
  $data = endpoint_request_data();
  // ...
  return array('foo' => $foo, 'bar' => $bar);
}

function my_module_foo_get($id) {
  // ...
  return array('foo' => $foo, 'bar' => $bar);
}

function my_module_foo_update() {
  $data = endpoint_request_data();
  // ...
}

Similar modules

Help improve this page

Page status: Not set

You can: