Introduction

The Client Credentials Flow is an OAuth 2.0 authorization flow used when a system (server-to-server) needs to access resources without user interaction. In Salesforce, this flow allows external applications to authenticate and interact with Salesforce APIs using only client credentials (client ID and client secret), without requiring a user login and user context.

Learning Objectives

By the end of this article, you will:

What is the Client Credentials Flow?

The Client Credentials Flow is used when:

When to Use This Flow?

Where is This Flow Used?

How to Implement the Client Credentials Flow in Salesforce?

Step 1: Create a Connected App
  1. Go to SetupApp Manager.
  2. Click New Connected App.
  3. Enter App Name and Email.
  4. Enable OAuth Settings and select:
    • OAuth Scopes: “Full Access” (or required scopes like API access).
    • Enable Client Credentials Flow.
  5. Save and copy the Consumer Key and Consumer Secret.
Step 2: Obtain Access Token

Use Postman or any API tool to request an access token.

Endpoint:

https://login.salesforce.com/services/oauth2/token

Request (POST, x-www-form-urlencoded):

grant_type=client_credentials
&client_id=YOUR_CLIENT_ID
&client_secret=YOUR_CLIENT_SECRET

Response:

{
"access_token": "00Dxxxxxxxxx!xxxxxxxx",
"instance_url": "https://your-instance.salesforce.com",
"token_type": "Bearer"
}
Step 3: Call Salesforce API Using the Access Token

Once you have the access token, use it in API requests.

Example API Call (Get User Info):

GET https://your-instance.salesforce.com/services/data/v59.0/sobjects/User/
Authorization: Bearer 00Dxxxxxxxxx!xxxxxxxx
Step 4: Implement in Apex

You can also make API calls from Apex.

public class ClientCredentialsAuth {
public static void callSalesforceAPI() {
String tokenUrl = 'https://login.salesforce.com/services/oauth2/token';
HttpRequest req = new HttpRequest();
req.setEndpoint(tokenUrl);
req.setMethod('POST');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setBody('grant_type=client_credentials'
+ '&client_id=YOUR_CLIENT_ID'
+ '&client_secret=YOUR_CLIENT_SECRET');

Http http = new Http();
HttpResponse res = http.send(req);

if (res.getStatusCode() == 200) {
System.debug('Access Token: ' + res.getBody());
} else {
System.debug('Error: ' + res.getStatusCode());
}
}
}

Advantages and disadvantages

advantages

disadvantages

Common Mistakes and Tips


Summary

Quiz or Self-Assessment

  1. When should you use the Client Credentials Flow?
    a) When a user logs in manually
    b) When an external system interacts with Salesforce without a user
    c) When using SSO
  2. What are the required parameters to get an access token?
    a) Client ID and Secret
    b) Username and Password
    c) Authorization Code
  3. Which endpoint is used for getting the access token?
    a) /services/oauth2/token
    b) /services/oauth2/auth
    c) /services/oauth2/logout

Conclusion

The Client Credentials Flow is an essential part of Salesforce integrations when a backend service needs to access data securely without user intervention. Implementing this correctly ensures secure and scalable integrations. By following the steps outlined, you can efficiently use this flow in your Salesforce applications.

Additional Resources

https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_web_server_flow.htm&type=5