Sub topic 3
Authorizing endpoints: Remote Site Settings
This chapter assumes you can read basic Apex, know what an HTTP request is, and can open Setup as an administrator. It explains why Salesforce blocks callouts by default and how a Remote Site Setting unblocks one.
Why the platform blocks callouts
An Apex callout sends data from your org to a server you don't control. Salesforce does not trust that destination by default. Before any callout runs, the platform checks that the target URL has been approved by an administrator. This keeps a piece of code from quietly sending org data to an arbitrary host.
If the check fails, Http.send() throws a System.CalloutException:
System.CalloutException: Unauthorized endpoint, please check
Setup->Security->Remote site settings.Seeing this message means the code is probably fine. The endpoint has not been authorized.
Two ways to authorize an endpoint
| Approach | Where you configure it | Use it when |
|---|---|---|
| Remote Site Setting | Setup, Remote Site Settings | You call a URL directly and handle authentication yourself, or the service needs none. |
| Named Credential | Setup, Named Credentials | You want the endpoint and its authentication stored together. The next chapter covers this. |
A Named Credential authorizes its own URL, so you don't need a separate Remote Site Setting for it. A Remote Site Setting only authorizes the endpoint. It stores no credentials.
Creating a Remote Site Setting
- In Setup, enter Remote Site Settings in Quick Find and open the page.
- Click New Remote Site.
- Enter a Remote Site Name. Use letters, numbers and underscores, for example
Todo_API_Site. - Enter the Remote Site URL, for example
https://jsonplaceholder.typicode.com. - Leave Disable Protocol Security cleared. Checking it allows the callout to go from HTTPS to HTTP, which weakens security.
- Make sure Active is checked, then save.
The URL identifies a server by scheme, host and port. Enter the origin, not a full path to one resource. One setting covers every path on that server, such as /todos and /todos/1.
Matching rules to watch
- The scheme must match. A setting for
https://example.comdoes not authorizehttp://example.com. - The host must match.
api.example.comandexample.comare different hosts, so each needs its own setting. - A non-default port must appear in the setting.
- An inactive setting authorizes nothing. If a callout that worked yesterday now fails, check the Active flag first.
Calling an authorized endpoint
Once the setting exists, you can use the full URL in Apex:
HttpRequest req = new HttpRequest();
req.setEndpoint('https://jsonplaceholder.typicode.com/todos/1');
req.setMethod('GET');
HttpResponse res = new Http().send(req);
System.debug(res.getStatusCode());Without the setting, send throws before any network traffic occurs.
Deploying the setting as metadata
Remote Site Settings are metadata, so you can keep them in source control and deploy them. Save this file as force-app/main/default/remoteSiteSettings/Todo_API_Site.remoteSite-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<RemoteSiteSetting xmlns="http://soap.sforce.com/2006/04/metadata">
<disableProtocolSecurity>false</disableProtocolSecurity>
<isActive>true</isActive>
<url>https://jsonplaceholder.typicode.com</url>
</RemoteSiteSetting>Deploying it keeps sandboxes and production in step. Callouts that work in a sandbox often fail in production because someone created the setting by hand in only one org.
Common mistakes
- Adding a setting in one org and forgetting to deploy it to the others.
- Using a different host in code than in the setting, such as
www.on one side only. - Turning off protocol security to get past an error instead of fixing the URL scheme.
- Adding a Remote Site Setting for a Named Credential endpoint. It does nothing useful.
Exercise
A callout to https://api.example.com/v1/orders fails with "Unauthorized endpoint". The org has an active Remote Site Setting for https://example.com. Why does it fail, and what is the fix?
Show answer
Remote Site Settings match the host exactly. api.example.com is a different host from example.com. Add a Remote Site Setting for https://api.example.com, or use a Named Credential that points to that URL.