# Why must administrators configure Remote Site Settings before allowing Apex code to perform external HTTP callouts?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Remote Site Settings, Network Security

Read it on the site: https://www.forcechallenges.com/interview-questions/why-must-administrators-configure-remote-site-settings-before-allowing-apex-code-to-perform-external-http

## Answer

**Basics:** Salesforce is multi-tenant, so one org's code cannot freely contact any internet host. Admins explicitly approve each destination. This protects against data exfiltration by malicious or buggy code. The error you see is `System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings.`

**How it works:**
1. Setup > Security > Remote Site Settings > New.
2. Enter a name and the base URL (protocol and domain, for example `https://api.example.com`), keep it Active.
3. Apex `req.setEndpoint('https://api.example.com/v1/data')` now works.
4. Alternative: a Named Credential (`callout:Name`) satisfies the check without a separate Remote Site Setting.
5. Remote Site Settings are metadata and deploy between orgs.

**Advanced / deep dive:** Remote Site Settings only govern Apex (and some server-side) callouts, not browser traffic. Browser-side requests from LWC (for example `fetch`) are controlled by **CSP Trusted Sites** and Lightning Web Security, a separate list. There is an option to disable protocol security that permits http/https mismatches; avoid it. Callouts to non-standard ports are restricted (verify the allowed port range in current docs). Prefer Named Credentials because they combine the allowlist, endpoint, and secrets and remove the need to maintain separate entries and hardcoded URLs. In unit tests no real callouts happen at all; you must supply an `HttpCalloutMock` regardless of the Remote Site Setting. A callout to your own org's REST API also needs a Remote Site or a Named Credential. Audit the list occasionally to remove stale hosts.

**Code / config example:**
```apex
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.example.com/v1/data'); // needs Remote Site Setting
// Preferred: req.setEndpoint('callout:Example_NC/v1/data'); // no Remote Site needed
req.setMethod('GET');
HttpResponse res = new Http().send(req);
```

**Hidden gotchas & must-remember points:**
- The setting must match protocol and domain; `http` and `https` are different entries.
- Forgetting to deploy the Remote Site Setting to production is a classic go-live failure.
- Named Credential callouts do not need it.
- It does not cover browser-side `fetch`; use CSP Trusted Sites.
- Only the domain is checked, not the path.
- Each redirect target host may also need approval.

**Best practice:** Prefer Named Credentials; where Remote Sites are used, list the narrowest host, deploy it with the code, and review periodically.

---
Source: ForceChallenges · last updated 2026-11-02
