Sub topic 14
Protecting secrets and limiting access
This chapter covers how to keep the credentials and access of your Agent API integration small and safe. It assumes you can already request an access token with the client credentials flow and create a session, as covered in earlier chapters.
What is at risk
The client ID and client secret of your External Client App act like a username and password for a service. Anyone who holds them can request tokens. Those tokens run as the run-as user, so the caller can talk to your agent and reach whatever data and actions the agent can reach.
Two things limit the damage: how well you protect the secret, and how little access the app and its run-as user have.
Keep secrets out of code
Read credentials from the environment, or from a secrets manager, at runtime. The earlier examples already do this with os.environ. The pattern below fails early with a clear message if a value is missing, and it avoids printing the secret.
import os
REQUIRED = ["SF_MY_DOMAIN", "SF_CLIENT_ID", "SF_CLIENT_SECRET", "SF_AGENT_ID"]
def load_config() -> dict:
missing = [name for name in REQUIRED if not os.environ.get(name)]
if missing:
# Report names only, never values
raise RuntimeError(f"Missing environment variables: {', '.join(missing)}")
return {name: os.environ[name] for name in REQUIRED}Good habits:
- Store secrets in a vault or your platform's secret store, not in
.envfiles that get committed. - Add local secret files to
.gitignore. - Never log request headers. The
Authorization: Bearer ...header contains a live token. - Keep tokens in memory only. Do not write them to disk or a database.
- Use separate apps and secrets for development, test, and production.
Limit what the app can do
Apply least privilege at each layer.
| Layer | What to restrict |
|---|---|
| OAuth scopes | Grant only the scopes the Agent API needs. Leave out broader scopes such as full access. |
| Run-as user | Use a dedicated integration user, not a named employee or an administrator. |
| Permission sets | Give that user only the object and field access the agent's actions require. |
| Agent design | Expose only the topics and actions the external system should use. |
| Network | Where your setup allows it, restrict the app to known IP ranges. |
The run-as user matters most. The agent acts with that user's permissions, so a broad profile turns a leaked secret into broad data access. A dedicated user also makes audit trails clear, because activity is attributed to the integration and not to a person.
Rotate and monitor
- Rotate the client secret on a schedule, and immediately after any suspected exposure. Deploy the new secret before you remove the old one if your app supports having both, so you avoid downtime.
- Review the app's usage and the integration user's login history for unexpected activity.
- Remove External Client Apps and users that are no longer used.
- Cache a token until shortly before it expires instead of requesting a new one for every call. This reduces how often the secret is sent.
Protect the conversation data
Messages sent to the agent may contain personal data. Send only what the agent needs. Do not log full message bodies in production unless your policy allows it. Always use HTTPS, which the Agent API requires. Always end sessions, as shown in the earlier chapter, so conversation state does not linger.
Common mistakes
- Using an administrator as the run-as user for convenience.
- Granting every OAuth scope "to make it work".
- Printing the response of a failed token request with the request body included, which can expose the secret.
- Sharing one secret across many services, so you cannot revoke one without breaking the rest.
Exercise
A teammate proposes setting the run-as user to a System Administrator so the agent "never hits permission errors". Give two reasons this is risky and name a better setup.
Show answer
The agent acts with the run-as user's permissions, so a leaked secret or a prompt that triggers an unintended action could read or change far more data than the integration needs. Audit records would also point to a powerful shared account, which makes activity harder to trace. A better setup is a dedicated integration user with a permission set that grants only the objects, fields, and actions the agent's topics require.
Exercise
Why is it a mistake to log the full headers of every request in your Agent API client?
Show answer
The Authorization header holds a live bearer token. Anyone who can read the logs can reuse the token until it expires and call the agent as the run-as user.