Callout means calling an external web Service or Sending an HTTP request from Apex Code and then receiving the response.
Why We Use Named Credentials
Named credentials to make callouts easier to maintain. For example: If an endpoint URL changes, you update only the named credential. All callouts that reference the named credential simply continue to work.
Authentication in Salesforce
Salesforce provides several methods to authenticate users. Some methods are automatically enabled, and some require that you enable and configure them.
It supports two types of authentication protocols for now: Basic Authentication (Password authentication) or OAuth Basic Authentication: Where you have to share your Password with People who need to access your User Account.
OAuth : OAuth doesn’t Share Passwords. Instead, OAuth uses authorization tokens to verify an identity between consumers and service providers.
Benefits of Named Credential
You can skip remote site settings, which are otherwise required for callouts to external sites, for the site defined in the named credential.
- For example callout: My_Named_Credential/some_path.
Architecture Diagram of Named Credential

Following steps to Connect to an external System using “Named Credential”:
- Create a Connected App.
- Create an Authorization Provider.
- Define Named Credential.
- Use Apex to connect in 5 lines of code.
1. Create a Connected App:-


2. Create Authorization Provider:


- Label – A user-friendly name for the named credential displayed in Salesforce.
- Name – A unique identifier that refers to this named credential from callout definitions
- URL – The URL or root URL of the callout endpoint.
- Identity Type – Determines whether you are using one or multiple sets of credentials to access the external system.
- Anonymous : No identity and therefore no authentication.
- Per User : Use separate credentials for each user who accesses the external system via callouts.
- Named Principal : Use the same set of credentials for all users who access the external system from your org.

3. Use Apex code to connect:


