Loading

Salesforce Apex Callout Timeout Error to Third-Party REST API — How to Handle and Increase Timeout

Date de publication: May 30, 2026
Description

When making an HTTP callout from Salesforce Apex to a third-party REST API, the callout may fail with a System.CalloutException timeout error. This article explains how to catch the timeout exception in Apex, how to configure a custom timeout value on the HttpRequest object, and the default and maximum timeout values supported by Salesforce.

Résolution

Catching the Callout Exception

To handle timeout errors gracefully in Salesforce Apex, wrap the HTTP request execution in a try-catch block that catches System.CalloutException. When a timeout occurs, the system throws a System.CalloutException. Inside the catch block, add your error handling logic, such as retrying the callout, logging the error, or notifying the user.


The pattern is:

  1. Create the HttpRequest object and set the endpoint and method.
  2. Create an Http object and call the send() method, passing the HttpRequest object.
  3. Wrap the send() call in a try block.
  4. In the catch block, catch System.CalloutException.
  5. Inside the catch block, implement retry logic, error logging, or any other appropriate error handling.

You can also log helpful debug information such as the response status code and status message to assist with troubleshooting.

How to Set a Custom Timeout

The callout timeout is configurable in Apex using the setTimeout() method on the HttpRequest object. The timeout value is specified in milliseconds.
To set a custom timeout:

  1. Create an HttpRequest object.
  2. Call the setTimeout() method on the HttpRequest object, passing the desired timeout value in milliseconds.
  3. Use the configured HttpRequest object when executing the callout.

Timeout limits:

  • Default timeout: 10,000 milliseconds (10 seconds)
  • Minimum timeout: 1 millisecond
  • Maximum timeout: 120,000 milliseconds (120 seconds)

For example, to set a 30-second timeout, call req.setTimeout(30000) before executing the callout. If your integration requires more time to respond than the default 10 seconds, increase the timeout value up to the maximum of 120 seconds.
If the third-party API consistently exceeds the maximum 120-second timeout, you should consider an asynchronous callout pattern using Salesforce Queueable Apex or a scheduled job.


Numéro d’article de la base de connaissances

000387845

 
Chargement
Salesforce Help | Article