Sub topic 4
Using Named Credentials for the endpoint
This chapter assumes you can write basic Apex classes, know what an HttpRequest is, and have read the previous chapter's refresher on Http, HttpRequest and HttpResponse. Here you replace a hard-coded URL and secret with a Named Credential.
What a Named Credential gives you
A Named Credential is a Salesforce setup record. It stores the base URL of an external service and the authentication details for it. Your Apex code refers to it by name, so the URL and the secret never appear in the code.
This gives you three benefits:
- No secrets in code. Passwords, tokens and OAuth settings live in setup, not in a class or a trigger.
- Automatic authentication. Salesforce adds the authentication to each request for you.
- No Remote Site Setting. Callouts that use a Named Credential don't need a separate Remote Site Setting entry for that endpoint.
Your code also stays the same across environments. A sandbox and production can use the same name, Billing_API, while each org points it at a different URL.
Using the credential in Apex
Set the endpoint to callout: followed by the Named Credential name, then add the path. Salesforce replaces the callout:Billing_API part with the stored base URL.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Billing_API/accounts');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setTimeout(20000);
req.setBody(JSON.serialize(payload));
HttpResponse res = new Http().send(req);If the Named Credential's base URL is https://billing.example.com/api, this request goes to https://billing.example.com/api/accounts. This is the same endpoint format used by AccountSyncJob and AccountCalloutService in this tutorial.
What to configure in setup
In Setup, a Named Credential is usually built from two parts. The exact screens depend on your org's release, so check the current Salesforce help for the field names.
- An External Credential describes how to authenticate, for example with OAuth or a custom header. A permission set principal controls who can use it.
- A Named Credential holds the URL and points to the External Credential.
Give the user or integration user access through a permission set. Without that access, the callout fails even if your code is correct.
Common errors
| Symptom | Likely cause |
|---|---|
| `Callout:Billing_API` fails with an endpoint error | The name is misspelled or the credential doesn't exist in this org. The name is case-sensitive in practice, so match it exactly. |
| Authentication fails with a 401 or similar | The running user lacks the permission set for the External Credential. Async jobs run as the user who enqueued them, so check that user. |
| Double slash or missing path | The base URL ends in `/` and your path starts with `/`. Keep one slash between them. |
| Test fails with a callout error | Tests can't reach the network. Use `Test.setMock`, covered in a later chapter. |
Exercise
A Queueable class sets the endpoint to 'callout:Billing_API/accounts'. In production the call returns a 401, while the same code works in a sandbox. Name two things to check.
Show answer
First, check that the running user has access to the External Credential through a permission set in production. Second, check that the Billing_API Named Credential in production has the correct URL and authentication settings, since each org stores its own values.