Sub topic 3
Creating an External Client App with the right scopes
This chapter assumes you can read short curl and Python examples and know what an OAuth 2.0 access token is. It also assumes you have a Salesforce org with Agentforce enabled and an agent you can activate. By the end you will have an External Client App (ECA) that an outside system can use to request tokens for the Agent API.
Why an External Client App is needed
The Agent API does not accept a username and password. Every call carries a bearer token, and Salesforce issues that token only to a registered application. The ECA is that registration. It holds:
- A consumer key and consumer secret, which identify your external system.
- The OAuth scopes the system may request.
- The flows the system may use. For server-to-server calls this is the client credentials flow.
- Policies, such as which Salesforce user the system acts as.
An External Client App is the newer replacement for the Connected App. Salesforce recommends it for new integrations. Many older tutorials describe Connected Apps, and the fields are similar. If you only find Connected App instructions, check the current Salesforce documentation for your release.
The ECA has two parts. The settings part describes the app and its OAuth configuration. The policies part controls how the org enforces it, including the run-as user. You must configure both. Missing the policies part is the most common reason a new ECA fails.
Step 1: Create the app
- In Setup, enter
External Client App Managerin Quick Find and open it. - Click New External Client App.
- Fill in the basic information. Use a name that tells an admin what the app is for, such as
Order Status Agent Integration. Add a contact email.
Name the app after the integration, not after yourself. Admins will later need to know which system a given key belongs to, especially when they rotate secrets.
Step 2: Enable OAuth and choose scopes
Open the OAuth settings section of the app and enable OAuth. Then fill in the following.
Callback URL. The form requires one even though the client credentials flow never redirects a browser. Use a placeholder you control, such as https://localhost/callback. The value is not used by this flow.
OAuth scopes. Add these three:
| Scope label in Setup | Scope value | What it allows |
|---|---|---|
| Manage user data via APIs | `api` | Calls to Salesforce REST APIs as the run-as user. |
| Access chatbot services | `chatbot_api` | Access to the agent and chatbot services. |
| Access the Salesforce API Platform | `sfap_api` | Access to `api.salesforce.com`, where the Agent API is hosted. |
Many guides also list Perform requests at any time (refresh_token, offline_access). It is harmless, but the client credentials flow does not return a refresh token. You request a new token when the old one expires, which is covered in the error handling chapter.
Request only the scopes you need. Each extra scope widens what a leaked token can do. For example, do not add full for convenience.
Step 3: Enable the client credentials flow
In the same OAuth settings section, find the flow options and select Enable Client Credentials Flow. Salesforce shows a warning that this flow runs as a specific user. Read it. It describes the central security decision of this setup, which the next chapter covers in detail.
Leave the flows you do not use disabled. If your integration is purely server-to-server, you do not need the web server flow, the JWT bearer flow, or the device flow.
Save the app. Salesforce may take a few minutes to make a new app usable. If a token request fails right after creation with an invalid_client_id error, wait and retry before changing anything.
Step 4: Set the run-as user in policies
The client credentials flow has no human logging in, so Salesforce needs to know which user the token represents.
- Open the app in External Client App Manager and go to the Policies tab.
- Edit the OAuth Policies.
- Under the client credentials flow settings, enable the flow and choose a Run As user.
- Save.
Choose the run-as user with care:
- Create a dedicated integration user instead of reusing an admin account. A dedicated user makes audit logs readable and lets you limit access without affecting people.
- Give that user only the permissions the agent needs. The token carries the user's access, so a powerful run-as user means a powerful token.
- Make sure the user is active. If an admin deactivates the user, every token request for the app fails.
- Do not use a user whose password policy forces expiry in a way that locks the account. API-only integration users are better suited to this.
Step 5: Copy the consumer key and secret
Open the app's OAuth settings and choose to reveal the consumer details. Salesforce may ask you to verify your identity. Copy the Consumer Key and Consumer Secret. They map to the client_id and client_secret form fields in the token request.
Store them as environment variables, not in source code. The Python and curl examples throughout this tutorial read them like this:
export SF_MY_DOMAIN="https://mycompany.my.salesforce.com"
export SF_CLIENT_ID="3MVG9...your-consumer-key"
export SF_CLIENT_SECRET="your-consumer-secret"Use your own My Domain URL. The token endpoint is on your org's My Domain, not on login.salesforce.com. A token requested from the wrong host will not work for the client credentials flow.
Step 6: Check the app before writing code
Test the app with curl before building anything on top of it. This isolates configuration problems from code problems.
curl -s -X POST "$SF_MY_DOMAIN/services/oauth2/token" \
-d "grant_type=client_credentials" \
-d "client_id=$SF_CLIENT_ID" \
-d "client_secret=$SF_CLIENT_SECRET"A working app returns a JSON body that contains an access_token, a token_type of Bearer, and a scope field. Read the scope value. It should list the three scopes you selected. If one is missing, the app configuration is the problem, not your client code.
Common errors and what they mean
| Error returned | Likely cause | Fix |
|---|---|---|
| `invalid_client_id` | Wrong consumer key, app not yet propagated, or wrong My Domain. | Recopy the key, wait a few minutes, and confirm the host. |
| `invalid_client` | Wrong consumer secret. | Recopy or regenerate the secret. |
| `unsupported_grant_type` | Client credentials flow is not enabled on the app. | Enable it in the OAuth settings and policies. |
| `invalid_grant` with a run-as message | No run-as user set, or the user is inactive. | Set an active user in OAuth Policies. |
| `invalid_scope` or later 401/403 on `api.salesforce.com` | A required scope is missing. | Add `api`, `chatbot_api`, and `sfap_api`, then request a new token. |
Remember that scopes are fixed into a token when it is issued. If you change the scopes on the app, tokens you already hold keep the old scopes. Request a fresh token after any change.
Edge cases to plan for
- Several environments. A sandbox and production org each need their own ECA, consumer key, and secret. Use separate environment variables per environment, and never reuse a production secret in a test setup.
- Packaging. If you deploy the ECA between orgs, the consumer secret does not travel with the metadata. Regenerate it in each target org.
- Secret rotation. Plan how your system picks up a new secret without downtime. Reading the value from environment variables or a secrets manager at startup makes rotation a restart, not a code change.
- Naming and ownership. Record who owns the app and the run-as user. Orphaned integrations are hard to retire safely.
Summary
An External Client App for the Agent API needs OAuth enabled, the api, chatbot_api, and sfap_api scopes, the client credentials flow enabled, and an active, least-privilege run-as user set in the OAuth policies. A curl check against your My Domain token endpoint confirms the setup before you write any integration code. The next chapter explains what the client credentials flow does with these settings.
Exercise
Your curl test against the My Domain token endpoint returns an access token, but session creation on api.salesforce.com later fails with an authorization error. The scope field in the token response shows only api chatbot_api. What is wrong, and what must you do to fix it?
Show answer
The app is missing the sfap_api scope, which grants access to the Salesforce API Platform host that serves the Agent API. Add sfap_api to the External Client App's OAuth scopes, save, and request a new access token. The old token keeps its original scopes, so it must be replaced.
Exercise
A teammate sets the run-as user of the app to their own administrator account because it is quick. Give two reasons to use a dedicated integration user instead.
Show answer
First, the token carries the run-as user's permissions, so an admin account makes a leaked token far more dangerous than a user with only the access the agent needs. Second, if the teammate leaves or the account is deactivated, every token request for the app fails, and audit logs mix human activity with integration activity.