Sub topic 2
Preparing the Agent: Activate It and Find Its ID
This chapter covers the first practical step of any Agent API integration: making sure the agent can accept calls, and getting the identifier you will put in every session request. It assumes you know basic REST and HTTP, can read short Python or curl examples, and have a Salesforce org with Agentforce enabled and at least one agent built. You do not need an access token yet. That comes in later chapters.
Why preparation comes first
The Agent API does not create or configure agents. It starts conversations with an agent that already exists in your org. If that agent is not ready, every later step fails, and the errors can look like authentication or payload problems even though the cause is the agent itself.
Two things must be true before you write any integration code:
- The agent is activated.
- You know its agent ID, the value that goes in the session URL.
Activation
An agent has a status. While it is inactive, you can edit its topics and actions, and you can test it in the builder. In that state it is a work in progress. Only an activated agent is a published version that other clients can reach.
To activate an agent:
- In Setup, enter Agents in Quick Find and open the Agents page. The page may be labeled Agentforce Agents in your org.
- Open the agent you want to call.
- Check that the agent's topics, actions, and instructions are the ones you want to expose. Callers will see the behavior of the version you activate.
- Select Activate.
If the agent was already active and you edit it, the change is saved in a new version. You must activate that new version before external callers see the change. This surprises many teams. A developer fixes an instruction in the builder, tests it in the builder, and then the external system still shows the old behavior because the new version was never activated.
Activation and versions
Think of the agent as having a stable identity with versions behind it. The table below separates what stays the same from what changes.
| Item | Stays the same across edits | Changes with a new version |
|---|---|---|
| Agent ID used in the API URL | Yes | No |
| Topics, actions, instructions | No | Yes |
| Which version external callers reach | No | Yes, after you activate it |
Because the ID is stable, you do not need to redeploy your integration when the agent's behavior changes. You only activate the new version. If you ever deactivate the agent, the same ID stops accepting sessions until it is activated again.
Finding the agent ID
The Agent API addresses an agent by its ID. In the session creation call, the ID is part of the path:
POST https://api.salesforce.com/einstein/ai-agent/v1/agents/{agentId}/sessionsAn agent ID is an 18-character Salesforce record ID that begins with 0Xx. You can get it in either of two ways.
From the browser URL
- Open the agent in the Agentforce Builder.
- Look at the address bar. The record ID of the agent appears in the URL path.
- Copy the 18-character value that starts with
0Xx.
The exact URL layout differs between releases, so look for the value with that prefix, not a fixed position.
From a query
You can also read the ID from the org's data. Agents are stored as records of the BotDefinition object, and the agent ID is the record ID. In the Developer Console Query Editor, or in any SOQL tool, run:
SELECT Id, DeveloperName, MasterLabel
FROM BotDefinition
WHERE DeveloperName = 'Order_Support_Agent'Replace Order_Support_Agent with your agent's API name. The Id column is the value you need. Querying is more reliable than copying from a URL when you script setup across several orgs, such as a sandbox and production.
Keeping the ID in configuration
The examples in this tutorial read the agent ID and your My Domain URL from environment variables. This keeps environment-specific values out of code and lets the same program run against a sandbox and production.
export SF_MY_DOMAIN="https://mycompany.my.salesforce.com"
export SF_AGENT_ID="0XxXXXXXXXXXXXXXXX"The agent ID is an identifier, not a secret. Even so, keep it in configuration so you can change it without editing code. The client ID and client secret you create in a later chapter are different. Those are secrets and need stricter handling.
In Python, read the values once at startup and fail early if one is missing:
import os
REQUIRED = ("SF_MY_DOMAIN", "SF_AGENT_ID")
missing = [name for name in REQUIRED if not os.environ.get(name)]
if missing:
raise SystemExit(f"Missing environment variables: {', '.join(missing)}")
MY_DOMAIN = os.environ["SF_MY_DOMAIN"]
AGENT_ID = os.environ["SF_AGENT_ID"]
print(f"Agent {AGENT_ID} on {MY_DOMAIN}")Failing at startup gives a clear message. Without the check, a missing value appears later as a confusing HTTP error, such as a 404 on a URL that contains None.
A quick sanity check on the ID
Before you spend time on authentication, check the ID's shape. This catches common copy mistakes, such as a trailing space or a record ID from a different object.
import re
def looks_like_agent_id(value: str) -> bool:
return re.fullmatch(r"0Xx[A-Za-z0-9]{15}", value) is not None
print(looks_like_agent_id("0Xx000000000001AAA")) # True
print(looks_like_agent_id("001000000000001AAA")) # False, an Account ID prefixThis checks the format only. It does not prove the agent exists or is active. You can only confirm that when you create a session in a later chapter.
What the Agent API needs from the org
Activation alone does not complete the setup. Keep these points in mind, because later chapters build on them:
- The agent must be reachable by the user the integration runs as. That user is the run-as user, covered in the chapter on the client credentials flow.
- The org must have Agentforce enabled, and the user must have the permissions the agent's actions require. An agent can accept a session and still fail when an action touches data the run-as user cannot read.
- Your My Domain URL is the base for the token request, and the agent is addressed through the
api.salesforce.comhost. Keep both values in configuration.
Common mistakes
- Using an inactive agent. The session request is rejected. Check the agent's status first.
- Forgetting to activate a new version. The API keeps serving the older version. Compare the version number in Setup with the behavior you see.
- Copying the wrong ID. The ID of a topic, an action, or another record can look similar. Confirm the
0Xxprefix, or use the SOQL query. - Carrying an ID between orgs. A sandbox ID will not work in production.
- Putting a trailing newline or space in the value. This often happens when you paste into a shell or a secrets tool. Strip the value when you read it.
Checklist before moving on
- The agent is active, and you activated the version you want callers to reach.
- You have the 18-character agent ID that begins with
0Xx. SF_MY_DOMAINandSF_AGENT_IDare set in the environment for the org you are targeting.- You have reviewed the agent's topics and actions with external callers in mind.
Exercise
A teammate edits an agent's instructions in the builder and confirms the new behavior in the test panel. Your integration, which calls the agent through the Agent API, still shows the old behavior. What is the most likely cause, and what do you do?
Show answer
The edit was saved as a new version that has not been activated. The Agent API serves the active version, so callers still reach the old one. Activate the new version in Setup. The agent ID stays the same, so the integration needs no code change.
Exercise
You develop against a sandbox and deploy to production. How should your code handle the agent ID, and how could you find the ID in each org with a query?
Show answer
Read the ID from configuration, such as the SFAGENTID environment variable, because it differs between orgs. In each org, run SELECT Id, DeveloperName, MasterLabel FROM BotDefinition WHERE DeveloperName = '<your agent API name>' and use the Id value.