Sub topic 2
Why credentials should not live in Apex code
This chapter assumes you can read basic Apex, know how HTTP requests, headers and status codes work, can find your way around Salesforce Setup, and have a rough idea of what API keys, Basic authentication and OAuth 2.0 are. It explains the problem that Named Credentials and External Credentials solve. Later chapters show how to configure them.
The problem with hardcoded endpoints and secrets
A first callout often looks like this:
public with sharing class WeatherServiceNaive {
public static String getForecast(String city) {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.example.com/v1/forecast?city=' + EncodingUtil.urlEncode(city, 'UTF-8'));
req.setMethod('GET');
req.setHeader('X-Api-Key', 'sk_live_51H8exampleSecret');
HttpResponse res = new Http().send(req);
return res.getBody();
}
}This code works, and it causes several problems.
- Secret exposure. The API key sits in source code. Anyone who can read the class, the repository or a debug log can read the key.
- Environment coupling. The sandbox and production orgs need different URLs and keys. Hardcoding forces you to edit code for each org, which risks shipping a test key to production or a live key to a sandbox.
- Painful rotation. Changing a key means editing and deploying code.
- Missing Remote Site Setting. A hardcoded URL must also be allowed in a Remote Site Setting, or the callout fails.
- No per-user identity. The code cannot easily call the service as the current user. Every call carries the same shared secret.
You may think of moving the key into a custom setting or custom metadata. That hides it from the class body, but admins and developers can still read the value, and your code still has to build the authentication header. Authentication logic stays in Apex.
What Salesforce does instead
Salesforce splits the concern into separate pieces that you configure in Setup, not in code:
| Piece | Answers the question | Holds |
|---|---|---|
| Named Credential | Where is the service? | Base URL and callout settings |
| External Credential | How do I authenticate? | Protocol, principals, secrets |
| Permission set | Who may use it? | Access to an external credential principal |
Apex only refers to the Named Credential by name. At run time, Salesforce looks up the URL, adds the authentication and sends the request. The secret is never part of your code.
What you gain
- No secrets in code or version control. The key is stored encrypted in Setup.
- No Remote Site Setting. Callouts through a Named Credential do not need one for that endpoint.
- Per-org configuration. Each org holds its own URL and secret while the Apex stays identical.
- Rotation without deployment. You change the secret in Setup and the code stays untouched.
- Access control. Only users whose permission sets grant access to the principal can make the callout.
Exercise
A teammate stores an API key in a custom metadata record and reads it in Apex to set the X-Api-Key header. Name two problems that a Named Credential with an External Credential would solve.
Show answer
The key remains readable to admins and developers who can view the metadata record, and the Apex code still has to build the authentication header. With an External Credential, the secret is stored encrypted in Setup, Salesforce injects it at callout time, and a permission set controls who can use it. It also removes the need for a Remote Site Setting and lets you rotate the key without a code deployment.