Consume An API With Credentials in Drupal 10

By Xandermar LLC, September 21, 2023

To consume an API with credentials in Drupal 10, you can use the Guzzle HTTP client library that Drupal provides by default. Here's a step-by-step guide on how to do this:

1. Install Guzzle HTTP Client:

  • Drupal 10 includes Guzzle as a dependency, so you don't need to install it separately.

2. Create a Custom Module:

  • If you don't already have a custom module for your project, create one in the modules/custom directory. You can use Drupal Console or Drush to generate a module scaffold.

3. Define API Credentials:

  • In your custom module, define the API credentials. This could be in a configuration form, settings file, or through the Drupal configuration system. Make sure to securely store sensitive information like API keys.

4. Create a Service to Make API Requests:

  • Create a service within your custom module that handles API requests. You can use the Dependency Injection Container for this purpose.

5. Use Guzzle to Make API Requests:

  • In your service, use Guzzle to make HTTP requests to the API. Here's an example of how to do this:
   // Your custom service.
   namespace Drupal\my_custom_module;

   use GuzzleHttp\ClientInterface;

   class MyApiService {

     /**
      * The HTTP client.
      *
      * @var \GuzzleHttp\ClientInterface
      */
     protected $httpClient;

     /**
      * Constructor.
      *
      * @param \GuzzleHttp\ClientInterface $http_client
      *   The HTTP client.
      */
     public function __construct(ClientInterface $http_client) {
       $this->httpClient = $http_client;
     }

     /**
      * Make an API request.
      *
      * @return string
      *   The API response.
      */
     public function makeApiRequest() {
       $api_url = 'https://api.example.com/endpoint'; // Replace with your API URL.

       // You can include credentials in the request headers or query parameters.
       $response = $this->httpClient->request('GET', $api_url, [
         'headers' => [
           'Authorization' => 'Bearer YOUR_API_TOKEN', // Replace with your authentication method.
         ],
         // Other request options.
       ]);

       return $response->getBody()->getContents();
     }
   }

6. Use the Custom Service:

  • You can now use your custom service in controllers, forms, or other parts of your module to make API requests and handle the responses.

7. Test and Secure Your Implementation:

  • Thoroughly test your code to ensure that it correctly handles API responses and errors.
  • Make sure to handle exceptions and errors gracefully, and consider implementing caching if appropriate.
  • Securely manage API credentials, and never hardcode them in your codebase or expose them publicly.

8. Enable and Configure Your Module:

  • After creating your custom module, enable it through the Drupal admin interface and configure any settings related to API endpoints and credentials.

By following these steps, you can consume an API with credentials in Drupal 10 using Guzzle. Make sure to adapt the code and authentication method according to the specific requirements of the API you are working with.

Tags

Comments