# Why are Named Credentials considered an essential security best practice for managing Apex HTTP callouts?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Named Credentials, Security Best Practices

Read it on the site: https://www.forcechallenges.com/interview-questions/why-are-named-credentials-considered-an-essential-security-best-practice-for-managing-apex-http-callouts

## Answer

**Basics:** Hardcoding a URL and a password or token in Apex is a security and maintenance problem: secrets leak through source control, differ per sandbox, and expire. A Named Credential moves that to configuration. The code only references a name.

**How it works:**
1. Create the credential (Setup > Named Credentials). In the current model, it is split: an **External Credential** (authentication protocol and principals) plus a **Named Credential** (URL) that points to it.
2. Grant users access through a permission set mapped to the external credential principal.
3. In Apex use `req.setEndpoint('callout:ERP_NC/orders')`.
4. Salesforce resolves the base URL, adds authentication (for example OAuth bearer token, basic auth, AWS signature) and sends the request.
5. For OAuth it obtains and refreshes the token automatically.

**Advanced / deep dive:** Benefits: no Remote Site Setting required for the host, secrets are encrypted and not visible to developers, environment-specific values differ per org without code change, and the same credential works from Apex, Flow HTTP Callout, External Services and Salesforce Connect. Authentication can be **named principal** (one identity for all) or **per user** (each user authenticates, preserving identity at the target). Custom headers and body formulas can use merge fields such as `{!$Credential.Username}` (legacy) or the external credential principal parameters (new model). Callouts count against the standard limits (100 per transaction, 120 seconds cumulative timeout), and callouts must still precede uncommitted DML in the transaction. Legacy-style Named Credentials still work but Salesforce directs new work to the External Credential model.

**Code / config example:**
```apex
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ERP_NC/v1/orders/' + EncodingUtil.urlEncode(extId, 'UTF-8'));
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setTimeout(30000);
HttpResponse res = new Http().send(req);
```

**Hidden gotchas & must-remember points:**
- Secrets are not deployed through metadata; they must be re-entered in each org.
- With the new model, users without the permission set mapping get a callout error even if Apex runs fine.
- Named credential endpoint calls bypass Remote Site Settings.
- A callout to a path beyond the base URL is fine; the host cannot be changed.
- Unit tests cannot make real callouts; use `HttpCalloutMock`.
- Per-user principals need each user to authenticate first.

**Best practice:** Never put URLs or secrets in code, custom settings or custom labels; use a Named Credential with least-privilege permission set access.

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