Sub topic 2
Preparing the agent: activate it and find its ID
This chapter assumes you know basic REST and JSON, have a Salesforce org with Agentforce enabled, and have built at least one agent. Before any code can call your agent, the agent must be active and you must know its ID. Both steps are quick, and skipping either one is a common cause of failed session requests later.
Why the agent must be ready
The Agent API talks to a specific agent version that is live in your org. If the agent is still a draft, or if you built it but never activated it, the session request in a later chapter fails. The API does not read your agent builder's working copy. It runs the activated version.
The agent ID is the value you put in the URL when you create a session. The URL looks like this:
https://api.salesforce.com/einstein/ai-agent/v1/agents/{AGENT_ID}/sessionsA wrong ID returns an error, and so does an ID from a different org. Sandbox and production agents have different IDs, so keep one value per environment.
Activate the agent
- In Setup, enter Agents in Quick Find and open Agents.
- Click the name of your agent to open it in Agentforce Builder.
- Test it in the builder first. Confirm that it answers a typical question and that its topics and actions behave as you expect.
- Click Activate. If the agent has unsaved changes, commit or save them first.
- Confirm that the status now shows Active.
Each change to the agent produces a new version. If you edit the agent later, activate the new version, or the API keeps serving the old one.
Find the agent ID
You can get the ID in two ways.
From the browser URL. Open the agent in Agentforce Builder. The URL contains a Salesforce record ID that begins with 0Xx. Copy that value.
From a query. The agent is stored as a BotDefinition record, so you can query it. In the Developer Console or with the Salesforce CLI, run:
sf data query --query "SELECT Id, DeveloperName, MasterLabel FROM BotDefinition" --target-org my-orgFind the row whose MasterLabel or DeveloperName matches your agent and copy its Id.
Both methods give an 18-character ID in the form the API expects. Store it as an environment variable so your code never contains it directly:
export SF_AGENT_ID="0XxXXXXXXXXXXXXXXX"The Python example in a later chapter reads it with os.environ["SF_AGENT_ID"].
How the pieces connect
The diagram shows what must be true before a session call can succeed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Session request returns a not-found or invalid-agent error | Wrong ID, or an ID from another org | Re-copy the ID from the target org |
| Session starts but the agent behaves like an old version | A newer version exists but was not activated | Activate the latest version |
| Works in the sandbox, fails in production | The agent was never deployed or activated in production | Deploy it, activate it, and use the production ID |
Exercise
Exercise
Your integration works against your sandbox, but the same code returns an error in production with the agent ID you copied from the sandbox. What is the most likely cause, and what do you check first?
Show answer
Agent IDs are specific to each org, so the sandbox ID is not valid in production. Look up the production agent's ID, for example with a BotDefinition query against the production org, confirm that agent is activated there, and store the ID as a separate environment variable for production.