Sub topic 10
Custom headers and credential formulas
This chapter assumes you can write basic Apex, know how HTTP headers work, and can navigate Setup and permission sets. It builds on the External Credential and Named Credential setup from earlier chapters.
Why headers need a formula
Many APIs expect a secret in a header, such as X-Api-Key: abc123. If you put that value in Apex, the secret ends up in source control and in logs. Salesforce avoids this with a custom header on the Named Credential. The header value is a formula. Salesforce evaluates the formula at callout time and reads the secret from the External Credential.
The Apex code never sees the secret. It only sets the endpoint.
Where the pieces live
The secret is stored as an authentication parameter on a principal of the External Credential. The Named Credential holds the header definition and refers to the parameter by name. A permission set grants access to the principal.
External Credential: Weather_EC
Authentication Protocol: Custom
Principal: Weather_Principal (Named Principal)
Authentication Parameter: ApiKey = <secret entered in Setup>
Named Credential: Weather_API
URL: https://api.example.com/v1
External Credential: Weather_EC
Custom Header:
Name: X-Api-Key
Value: {!$Credential.Weather_EC.ApiKey}
Permission Set: Weather_Callout_Access
External Credential Principal Access: Weather_EC - Weather_PrincipalThe formula {!$Credential.Weather_EC.ApiKey} has three parts:
| Part | Meaning |
|---|---|
| `$Credential` | The merge field namespace for credential values |
| `Weather_EC` | The API name of the External Credential |
| `ApiKey` | The name of the authentication parameter on the principal |
The Apex side
The Apex class sets only the callout: endpoint. It does not set X-Api-Key.
public with sharing class WeatherService {
public static String getForecast(String city) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Weather_API/forecast?city=' + EncodingUtil.urlEncode(city, 'UTF-8'));
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
HttpResponse res = new Http().send(req);
if (res.getStatusCode() != 200) {
throw new CalloutException('Weather API returned status ' + res.getStatusCode());
}
return res.getBody();
}
}When send runs, Salesforce adds X-Api-Key to the outgoing request. Your code can still set ordinary headers such as Accept.
Common errors
- Wrong formula name. A typo in the External Credential name or parameter name leaves the header empty or blank, and the API usually returns 401 or 403. Check that the names match exactly.
- Missing principal access. If the running user's permission set does not include the principal, the callout fails before the request is sent. The header is never resolved.
- Secret set in Apex too. Setting the same header in Apex can conflict with the one Salesforce adds. Leave authentication headers to the Named Credential.
- Parameter not saved. Secret values are write-only in Setup. If you are unsure the value was saved, enter it again.
Exercise
A Named Credential has the custom header X-Api-Key with the value {!$Credential.Weather_EC.ApiKey}. Callouts return 401, and the Apex code is correct. Name two things in Setup you should check.
Show answer
First, check that the formula matches the External Credential API name and the authentication parameter name exactly, including case and spelling. Second, check that the principal holds the correct secret value and that the user's permission set grants access to that principal. A wrong or missing value in either place causes the API to reject the request.