Sub topic 1
What an Apex callout is and when to use one
Prerequisites
This chapter assumes you already have:
- Basic Apex syntax: classes, methods, collections and exceptions.
- Familiarity with HTTP methods, status codes and headers.
- A basic understanding of JSON.
- Awareness of Salesforce governor limits.
- The ability to create metadata in Setup, such as Named Credentials.
What a callout is
An Apex callout is a request that your Apex code sends to a service outside Salesforce. Salesforce acts as the client. The external system acts as the server and sends back a response that your code reads.
Apex supports two kinds of callout:
- HTTP callouts, which send requests to REST or other web endpoints with the
Http,HttpRequestandHttpResponseclasses. This tutorial covers this kind. - SOAP callouts, which call web services through Apex classes generated from a WSDL file.
A callout is not the same as an inbound integration. If an outside system calls Salesforce, for example through an Apex REST class, that is an inbound call. A callout always starts inside Salesforce.
A first look at a callout
The following class makes a GET request and returns the response body. The endpoint starts with callout:, which refers to a Named Credential called Todo_API. Later chapters explain Named Credentials, so for now treat the prefix as a pointer to an authorized endpoint.
public with sharing class TodoService {
public class TodoException extends Exception {}
public static String fetchTodo(Integer todoId) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Todo_API/todos/' + todoId);
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setTimeout(20000);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() == 200) {
return res.getBody();
}
throw new TodoException('Unexpected status ' + res.getStatusCode() + ': ' + res.getStatus());
}
}The code does four things:
- It builds a request object and sets the endpoint, method, header and timeout.
- It sends the request with
Http.send. Apex waits here until the response arrives or the timeout expires. - It checks the status code before it uses the body.
- It throws an exception for any status it did not expect.
Why the platform restricts callouts
Salesforce runs your code on shared infrastructure. It therefore controls where that code can send data.
- Every endpoint must be authorized before Apex can reach it, either through a Remote Site Setting or a Named Credential. An unauthorized endpoint makes the callout fail at runtime.
- Callouts are subject to governor limits. A transaction can make at most 100 callouts, and the default timeout for each one is 10 seconds.
- Callouts have rules about uncommitted database work and about where in a transaction they can run. These rules shape how you design the code.
Later chapters cover each of these topics in turn.
When to use a callout
Use an Apex callout when your logic needs to talk to an external HTTP service and a declarative option cannot do the job. Typical cases are:
- Fetching data you need to process in code, such as exchange rates or address validation.
- Sending Salesforce data to another system after a record changes.
- Calling an API that needs custom headers, signing or payload shaping.
Check these alternatives first, because they need less code to maintain:
| Need | Consider instead |
|---|---|
| Show external records without copying them | Salesforce Connect and external objects |
| Call an API from a flow | Flow HTTP Callout action or External Services |
| Notify another system of changes | Platform Events or Change Data Capture |
Exercise
A Salesforce developer wants an external billing system to create Account records in Salesforce by sending HTTP requests. Is this an Apex callout? Explain why or why not.
Show answer
No. A callout is a request sent from Apex to an external service, with Salesforce as the client. Here the external system starts the request and Salesforce receives it, so this is an inbound integration, for example an Apex REST class or the standard REST API.