Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Views Bootstrap 5.5.4 — Minor update available for module views_bootstrap (5.5.4). Release: ChatGPT Ads 1.0.3 — Minor update available for module chatgpt_ads (1.0.3). Release: Opensolr Search 5.1.3 — Minor update available for module opensolr_search (5.1.3). Release: ServiceM8 Webform Integration 1.2.1 — Minor update available for module servicem8_webform (1.2.1). Release: PostfixAdmin 1.0.0 — Initial release available for module postfix_admin (1.0.0)! Release: MCP Sentinel 2.28.2 — Minor update available for module mcp_sentinel (2.28.2). Release: Rightup theme 1.0.2 — Minor update available for theme vartheme_bs5_rightup (1.0.2). Release: ip_analyser 1.0.3 — Minor update available for module ip_analyser (1.0.3). Security Coverage: Microsoft Azure AI — Module ai_provider_azure now has official Drupal security advisory coverage. Module Revived: Liquid Voting 2.0.1 — Module liquid_voting updated after 6 months of inactivity (2.0.1).

What does it do?

Do you have a consumer (for instance a JavaScript application) that uses a 3rd party API?

  • Does that API have all the CORS headers you need?
  • Does that API have sufficient caching?
  • Does it require an authentication key that you don't want to put in the clear in your JS app?
  • Do you wish it returned something different?

This module will help with all those problems. Putting Drupal between your consumer and the 3rd party API will allow you to modify the requests to the 3rd party API and the responses from it.

Usage

You will need to:

  • Create the proxy plugin.
  • Configure the server plugin.
  • Profit.

Create the proxy plugin

In order to connect to a new API you will need to write a new plugin. Take for instance:

namespace Drupal\my_module\Plugin\api_proxy;

use Drupal\api_proxy\Plugin\api_proxy\HttpApiCommonConfigs;
use Drupal\api_proxy\Plugin\HttpApiPluginBase;
use Drupal\Core\Form\SubformStateInterface;
use Symfony\Component\HttpFoundation\Response;

/**
 * The Example API.
 *
 * @HttpApi(
 *   id = "api-slug",
 *   label = @Translation("Example API"),
 *   description = @Translation("Proxies requests to the Example API."),
 *   serviceUrl = "https://api.example.org/v1",
 * )
 */
final class Example extends HttpApiPluginBase {

  use HttpApiCommonConfigs;

  /**
   * {@inheritdoc}
   */
  public function addMoreConfigurationFormElements(array $form, SubformStateInterface $form_state): array {
    $form['auth_token'] = $this->authTokenConfigForm($this->configuration);
    $form['more_stuff'] = ['#type' => 'textfield', '#title' => $this->t('Extra config')];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function calculateHeaders(array $headers): array {
    $new_headers = parent::calculateHeaders($headers);
    // Modify & add new headers. Here you can add the auth token. Refer to your
    // APIs documentation for expected auth format.
    return $new_headers;
  }

  /**
   * {@inheritdoc}
   */
  public function postprocessOutgoing(Response $response): Response {
    // Modify the response from the API.
    // A common problem is to remove the Transfer-Encoding header.
    // $response->headers->remove('transfer-encoding');
    return $response;
  }

}

Configure the server plugin

Head to /admin/config/services/api-proxy/settings and you should see a configuration form for your proxy. Note that there will be some common configuration options plus the ones specific for this plugin you added in addMoreConfigurationFormElements.

Profit

In your consumer (JS application) instead of making requests to:

https://api.example.org/v1/the-resource/123?setting[foo]=bar&lorem=10

Make them against:

https://your-drupal-install.com/api-proxy/api-slug?_api_proxy_uri=the-resource%2F123%3Fsetting%5Bfoo%5D%3Dbar%26lorem%3D10

Note that you will need to URL encode the the-resource/123?setting[foo]=bar&lorem=10. This results into the-resource%2F123%3Fsetting%5Bfoo%5D%3Dbar%26lorem%3D10.

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Jun 2023
Latest release
2 years ago
Releases (12 mo)
0
Maintenance
Dormant

Releases

Version Type Core Release date
2.0.3 Stable Nov 27, 2023
2.0.2 Stable Jun 22, 2023