Cloudbelive

REST API Integration using Named Credentials:

A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. Salesforce manages all authentication for Apex callouts that specify a named credential as the callout endpoint so that your code doesn’t have to. You can also skip remote site settings, which are otherwise required for callouts to external sites, for the site defined in the named credential.
To integrate a REST API with named credentials in Salesforce, you can follow these steps:

1. Create a Named Credential:

2. Define Remote Site Settings:

3. Use the Named Credential in Apex:

HttpRequest req = new HttpRequest();
req.setEndpoint(‘callout:My_Named_Credential/path/to/api’);
req.setMethod(‘GET’);
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
// Process the response
String responseBody = res.getBody();
// …
} else {
// Handle error
String errorMessage = ‘Error: ‘ + res.getStatusCode() + ‘ – ‘ + res.getStatus();
// …
}