Handle Authentication
By the end of this lesson, you'll be able to:
- Switch the hardcoded endpoint to a Named Credential
- Explain why this keeps the API key out of Apex code entirely
- Apply Module 36's Named Credential lesson to this real integration
Prerequisites: "Build the Callout Service"
Switching to a Named Credential
public Decimal convert(Decimal amountInZar, String targetCurrency) {
HttpRequest request = new HttpRequest();
request.setEndpoint(
'callout:Currency_API/convert?from=ZAR&to=' + targetCurrency + '&amount=' + amountInZar
);
request.setMethod('GET');
HttpResponse response = new Http().send(request);
return null;
}
callout:Currency_API replaces the raw https://api.example.com URL entirely — the actual endpoint and authentication (an API key, in this integration's case) are configured once in Setup as Currency_API, exactly Module 36's Named Credential lesson.
Why this matters specifically for this project
A currency exchange API almost certainly requires an API key for authentication — without a Named Credential, that key would need to live somewhere in Apex, visible to anyone reading the code and requiring a full deployment to rotate if it were ever compromised. This is exactly the real-world scenario Module 36's lesson was preparing for, not a hypothetical one.
Exercise
As a comment, explain what would need to change in Apex code if the Currency_API's API key needed to be rotated, using a Named Credential vs a hardcoded header.
Show hint
Compare "nothing in Apex changes" vs "a code change and deployment are required."
Handle Authentication Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson replaces the hardcoded endpoint from Lesson 2 with a Named Credential — Module 36's "keep secrets out of code" lesson, applied for real.