When we want to send data from Salesforce to an external system like a Google API or a payment system, we need to provide authentication details (username, password, or access token). If we write these details directly in Apex code, it is not secure and difficult to maintain.
To solve this, we create a Named Credential in Salesforce. It stores the external system URL and authentication details securely. Then, whenever Apex, Flow, or Integration needs to connect with that system, it just uses the Named Credential.
By using Named Credential, you store these details safely in the Salesforce setup. If anything changes, you only need to update the Named Credential; there is no need to edit the Apex code.
In this article, we will learn how to create a named credential in Salesforce, allowing us to securely connect Salesforce with external systems without providing sensitive information in Apex code.
Named Credential in Salesforce
A named credential in Salesforce is a secure way to store the login details, such as URL, username, password, or access token of an external system.
Instead of writing these details in Apex code, we save them in Salesforce setup as a named credential. Then, whenever we need to connect Salesforce with that external system (using Apex, Flow, or integration), we only need to call the named credential.
For example, when a new lead is created, we want to verify if the email provided is real and active before assigning it to the sales team. Instead of using Apex code, I used an HTTP callout in the flow to send the email address to an external email validation API.
Create External Credential in Salesforce
Before creating the named credential, we first need to create the external credential to define the authentication method, such as API key, username-password, or OAuth, that Salesforce will use to connect with the external system.
To create the credentials in Salesforce, click on the Setup. -> Home Page. -> In the Quick Find Box, Search for Named Credentials. You will see an option under Security. -> Click on it.
There, you need to click the “External Credential” option and then click the “New” button to create external credentials.

When creating an external credential, first enter a Label, such as an email validation credential. The Name field will be automatically populated based on the label you provide.
After that, select the Authentication Protocol, which defines the method Salesforce will use to connect with the external system (for example, api key, username-password, OAuth, or custom).
Then click the Save button.

Then we need to create the principal in the external credential to define who is making the callout to the external system. Here, I provided the parameter name as Email Validation Principle.
A Principal defines who or what can use the External Credential. It links the external credential to a user, permission set, or integration, so Salesforce knows which user or process can access the external system.
