
Website Security
How Pantheon Shut Down a Trojan Attack on Our GitHub Repositories
A supply chain attack targeted Pantheon's GitHub repos on May 29. Our security policies blocked it from reaching customer sites.
Stop API timeouts and security issues in Drupal integrations. Production-tested patterns for external APIs with auth, retries and monitoring.

Integrating third-party APIs into your Drupal site is a powerful way to enhance functionality – whether it's pulling in data from an external CRM, syncing products to an e-commerce platform or automating tasks between systems. But as useful as these integrations are, they also come with their own set of challenges:
In this article, we’re going to walk through the best practices for integrating APIs with Drupal in a way that’s both secure and scalable. We’ll cover everything from structuring your code to handle API calls properly, to keeping sensitive credentials safe and optimizing for performance with retries and timeouts.
Whether you’re adding a new API connection or fine-tuning an existing one, these tips will help you build integrations that are solid, maintainable and secure.
One of the most important things to get right when integrating APIs is separation of concerns. Directly calling third-party APIs from controllers, forms or even other Drupal components can lead to messy, hard-to-maintain code.
Instead, you should use a service-first structure, where all outbound API calls are encapsulated in a custom service class. This approach offers several key advantages:
By encapsulating all your outbound API calls in a service, you set up your integration for long-term success – keeping your codebase clean organized and testable.
Token refreshes should be centralized and serialized to avoid issues like simultaneous refresh attempts. A few things to keep in mind:
In Drupal, you can use the http_client (which leverages Guzzle) via dependency injection to handle your API calls. This is a more flexible and powerful way of making HTTP requests compared to using raw PHP functions like file_get_contents().
To configure the HTTP client for robust API communication, ensure your client has appropriate connect and read timeouts. This helps avoid hanging requests when the vendor API is unresponsive.
Also, set a sane user-agent string and include any required headers (like authentication or custom headers) globally in your client. This avoids repetition across individual API calls.
Once you receive a response from the API, normalize the vendor's data into value objects or entities that make sense for your Drupal site. This helps decouple your system from changes in the external API and makes the data easier to work with.
Here’s how:
Anything that could potentially exceed the web request timeout limit (typically around 120 seconds) should be moved to background workers. In Drupal, the Queue API or Cron are perfect for this.
Use the Queue API for repeatable tasks like syncing data at regular intervals. This ensures that long-running tasks are offloaded to background processes without blocking user interactions.
For scheduled tasks, use Drupal’s cron system to handle periodic work like data syncing or cleanup jobs. And, if you need to run a one-off job (like backfilling missing data), use the Batch API to chunk the work into manageable pieces. This gives users visibility into the progress and prevents timeouts.
When integrating third-party APIs into your Drupal site, security should always be a top priority. If not handled properly, APIs can introduce vulnerabilities that put both your data and your users at risk.
Here are some best practices to ensure your API integrations remain secure:
One of the fundamental principles of API security is least privilege. This means you should only request the minimum level of access required for your integration.
For example, if your API doesn’t need write access, don’t request it. Similarly, when it comes to authentication, short-lived access tokens are ideal. They have a limited lifespan, so even if they’re compromised, the damage is minimized.
Refresh tokens should also be stored securely – never in your code or configuration files. Use a runtime secrets store to manage them safely.
When dealing with personally identifiable information (PII), encryption is key. TLS (transport layer security) should be used to encrypt all data in transit, ensuring it can’t be intercepted by malicious actors.
For data stored at rest, make sure you’re using strong encryption methods to protect sensitive information. Also, be mindful of what you log—avoid storing tokens, passwords or PII in your logs. If logging is necessary, make sure sensitive data is redacted.
When it comes to handling authentication and API credentials, never store them in your code or config files. Hardcoding credentials or placing them in version-controlled files is a security risk. Instead, use a runtime secrets store to manage credentials securely. This ensures that sensitive data, like API keys or OAuth tokens, is never exposed in your code.
To manage secrets securely, store credentials in environment variables (or parameters in Drupal’s settings). This keeps them out of your code and ensures they are only available at runtime.
Pantheon Secrets, for example, makes it easy to manage these secrets safely, reducing the risk of exposure.
Also, inject these secrets into your service class through Drupal's dependency injection system. This makes it easy to access them where needed without hardcoding them anywhere.
If your API uses OAuth2 for authentication, it’s a good practice to use short-lived access tokens along with refresh tokens. Access tokens should be temporary and renewed via the refresh token mechanism.
Use Drupal’s State API to store ephemeral access tokens (not the Configuration API), as tokens typically expire quickly. Ensure the tokens are time-bound (with TTL) and remove them when no longer valid.
Additionally, ensure your logs never contain access tokens. Always redact or avoid logging sensitive information like authentication tokens.
When integrating Drupal with third-party APIs, timeouts are a common issue in production environments. They occur when requests take longer to complete than the system allows, which usually points back to the way the integration is designed.
A frequent cause is running heavy operations – like bulk data imports – inside a web request. These long tasks should be moved out of the request cycle and into background processes using Drupal’s Queue API or scheduled with cron. This ensures the user’s request completes quickly while the real work happens asynchronously.
Timeouts can also come from making too many API calls in a loop. If your code calls the API once for each record, you’ll hit limits fast. Instead, batch requests or sync data in smaller chunks using pagination or cursor-based methods.
External services themselves can also contribute to timeouts if they respond slowly or experience downtime. To handle this, add retry logic with exponential backoff and jitter, which spaces out retries and prevents overloading the service. And because issues often surface silently, monitoring and alerting are critical – track success rates, queue performance and retry patterns so problems can be caught early.
Tools like Pantheon’s Quicksilver can automate notifications and recovery actions in response to these issues.
To ensure that your API calls are reliable, you should implement retry middleware with exponential backoff and jitter. Here’s what that looks like:
To further improve debugging and observability, attach a correlation ID to each API request. This allows you to track a specific request across different services, making it easier to troubleshoot when something goes wrong.
Testing your external API integrations is crucial to detecting problems before they make it into production. Here are some testing approaches to ensure your integration runs reliably:
Once your API integration is live, observability becomes key for monitoring its health and diagnosing issues. Here’s how to ensure you can track your system’s performance and stability:
Setting Service Level Objectives (SLOs) and creating alerting mechanisms ensure that your integration delivers the expected level of service. Here’s how to set SLOs and manage alerts:
When integrating third-party APIs with your Drupal site, choosing the right platform is just as important as following best practices for security, performance and scalability. The right platform should provide tools that simplify your integrations and help you maintain high standards for reliability and security.
Pantheon provides a platform that is built with these key features in mind, making it the ideal environment for Drupal integrations – here’s how:
You can apply the best practices and patterns outlined throughout the article to any platform, but if you want an out-of-the-box solution designed to reduce operational toil while reinforcing reliable, secure API integrations, Pantheon is the platform for you.
Start with Pantheon today to simplify your integration process and ensure your Drupal site is resilient and secure!

Website Security
A supply chain attack targeted Pantheon's GitHub repos on May 29. Our security policies blocked it from reaching customer sites.

Drupal
Drupal 7 ended January 5, 2025. Compare Drupal 11, WordPress, Backdrop CMS and extended support options to find the best solution for you.

Website Security
Drupal expects professional security workflows; WordPress requires creating them. Learn why architecture, governance and automation outweigh CMS choice.