Why must administrators configure Remote Site Settings before allowing Apex code to perform external HTTP callouts?
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:
- Setup > Security > Remote Site Settings > New.
- Enter a name and the base URL (protocol and domain, for example
https://api.example.com), keep it Active. - Apex
req.setEndpoint('https://api.example.com/v1/data')now works. - Alternative: a Named Credential (
callout:Name) satisfies the check without a separate Remote Site Setting. - 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:
();
req.setEndpoint();
req.setMethod();
().send(req);