Sub topic 3
Creating an External Client App with the right scopes
This chapter assumes you are a Salesforce admin or have one nearby. It also assumes you know what an OAuth 2.0 access token is, and that your org has Agentforce enabled with an activated agent. Here you set up the identity your external system uses to call the Agent API.
What an External Client App is
An External Client App (ECA) is the Salesforce record that represents your external system. It holds the credentials your code presents and the permissions the resulting token carries. It replaces the older Connected App for new integrations. The Agent API needs one because every call must carry a bearer token, and a token can only be issued to a registered client.
Create the app
- In Setup, search for External Client App Manager and select New External Client App.
- Enter an app name, an API name, and a contact email. Leave the distribution state as Local, because only your org uses this app.
- Expand API (Enable OAuth Settings) and select Enable OAuth.
- Enter a callback URL. The client credentials flow never redirects, but the field is required. A placeholder such as
https://localhost/callbackworks. - Add the OAuth scopes listed in the next section.
- Select Enable Client Credentials Flow and accept the warning.
- Save the app.
Choose the scopes
Scopes limit what a token can do. Select only these for the Agent API:
| Scope label | Value | Why you need it |
|---|---|---|
| Manage user data via APIs | `api` | Lets the token call Salesforce REST APIs |
| Perform requests at any time | `refresh_token`, `offline_access` | Allows the token to be renewed without user interaction |
| Access chatbot services | `chatbot_api` | Allows access to agent chat services |
| Access the Salesforce API Platform | `sfap_api` | Allows calls to `api.salesforce.com`, where the Agent API runs |
A missing sfap_api scope is a common cause of authorization failures when you call the session endpoints. The token request itself may still succeed.
Set the run-as user
The client credentials flow has no login screen, so Salesforce needs to know which user the token represents.
- Open the app and go to the Policies tab.
- Under OAuth Policies, edit the plugin policies.
- Make sure Enable Client Credentials Flow is selected.
- In Run As (Username), choose a dedicated integration user.
Every call made with this app runs with that user's permissions. Create a user just for this integration, and give it only the access the agent needs. The next chapter covers how this works in more detail.
Copy the credentials
Open the Settings tab, expand OAuth Settings, and select Consumer Key and Secret. Salesforce may ask you to verify your identity. The consumer key is your client ID and the consumer secret is your client secret. Store both in environment variables or a secrets manager. The code in this tutorial reads them as SF_CLIENT_ID and SF_CLIENT_SECRET.
Common errors
invalid_client_credentials: The client credentials flow is not enabled, or no run-as user is set in the Policies tab.invalid_scopeor a 401 on session calls: A required scope is missing. Add it and request a new token. Existing tokens do not gain scopes.- Changes seem ignored: New apps can take a few minutes to become usable. Wait and retry before you change settings again.
Exercise
Your token request succeeds, but creating a session returns an authorization error. Which scope do you check first, and why?
Show answer
Check sfap_api. The Agent API is served from api.salesforce.com, and that scope authorizes calls to the Salesforce API Platform. After you add it, request a new token, because tokens you already hold do not gain scopes.