Sub topic 3
The Named Credential: where the service lives
This chapter assumes you can write basic Apex classes, send an HTTP request with Http, HttpRequest and HttpResponse, and find your way around Setup. It covers the Named Credential. The External Credential, which handles authentication, comes in the next chapter.
What a Named Credential holds
A Named Credential is a Setup record that stores the location of an external service and the settings for calling it. It does not store the secret. It points to an External Credential, and that record defines how you authenticate.
Splitting the two means you can change a base URL without touching authentication. You can also reuse one authentication setup across several endpoints.
| Field | Purpose |
|---|---|
| Label and Name | The Name is what Apex uses after `callout:`. It must be unique. |
| URL | The base URL of the service, such as `https://api.example.com/v1`. |
| External Credential | The authentication definition this endpoint uses. |
| Enabled for Callouts | Turns the Named Credential on. Without it, callouts fail. |
| Generate Authorization Header | Lets Salesforce build the standard `Authorization` header from the External Credential. |
| Allow Formulas in HTTP Header or Body | Allows credential formulas in custom headers or in the request body. |
| Custom Headers | Fixed headers added to every request through this credential. |
How a callout is resolved
Apex never sees the base URL or the secret. At run time, Salesforce replaces the callout: prefix with the stored URL and then adds authentication.
In this example, the Named Credential Weather_API has the URL https://api.example.com/v1. This Apex line:
req.setEndpoint('callout:Weather_API/forecast?city=Oslo');is sent to https://api.example.com/v1/forecast?city=Oslo. Anything after the Named Credential name is appended to the stored URL as a path and query string.
Creating one in Setup
- Open Setup and search for Named Credentials.
- Select the New Credential option on the Named Credentials tab.
- Enter a label and a name such as
Weather_API. - Enter the URL, with no trailing path that your Apex code will repeat.
- Choose the External Credential, for example
Weather_EC. - Select Enabled for Callouts. Select Generate Authorization Header only if the service expects a standard
Authorizationheader. - Save.
The Named Credential alone does not authorize anyone. Users also need access to the External Credential principal through a permission set, and a later chapter covers that.
Common mistakes
- Forgetting to select Enabled for Callouts, which causes a failure even when the URL is correct.
- Repeating part of the base path in Apex, which produces URLs such as
/v1/v1/forecast. - Adding a secret to a custom header as plain text. Use a credential formula instead, covered in a later chapter.
- Treating the Named Credential as the place to store the API key. The key belongs to the External Credential principal.
Exercise
A Named Credential named InventoryAPI has the URL https://stock.example.com/api. Your Apex sets the endpoint to 'callout:InventoryAPI/items/42'. Which URL receives the request, and where should you change it if the service moves to a new host?
Show answer
The request goes to https://stock.example.com/api/items/42. If the host changes, edit the URL on the Inventory_API Named Credential in Setup. The Apex code stays the same.