Sub topic 7
Creating a session
This chapter covers the request that opens a conversation with an Agentforce agent. It assumes you can already get an access token (see the previous chapter), that your agent is activated, and that you have its agent ID. You should also be comfortable reading short Python examples and JSON bodies.
What a session is
A session is the server-side container for one conversation. The agent keeps the conversation history inside it. Every later call, whether sending a message or ending the conversation, uses the session ID that this request returns. Creating a session does not send any user text. It only starts the conversation and returns the agent's first greeting.
The request
Send a POST to the agent's sessions endpoint:
POST https://api.salesforce.com/einstein/ai-agent/v1/agents/{agentId}/sessionsThe request needs two headers:
Authorization: Bearer <access token>Content-Type: application/json
The JSON body has these fields:
| Field | Purpose |
|---|---|
| `externalSessionKey` | A unique value you generate, such as a UUID. It lets you tie the Salesforce session to a record in your own system. |
| `instanceConfig.endpoint` | Your My Domain URL, for example `https://mycompany.my.salesforce.com`. |
| `streamingCapabilities.chunkTypes` | The chunk types you accept when streaming. `["Text"]` is enough for text replies. |
| `bypassUser` | Set to `true` when the agent should run as the client credentials run-as user. Without it, the session expects a signed-in end user. |
A working example
This function reuses the token you obtained earlier and returns the new session ID.
import os
import uuid
import requests
API_BASE = "https://api.salesforce.com/einstein/ai-agent/v1"
MY_DOMAIN = os.environ["SF_MY_DOMAIN"]
AGENT_ID = os.environ["SF_AGENT_ID"]
def create_session(token: str) -> str:
response = requests.post(
f"{API_BASE}/agents/{AGENT_ID}/sessions",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
json={
"externalSessionKey": str(uuid.uuid4()),
"instanceConfig": {"endpoint": MY_DOMAIN},
"streamingCapabilities": {"chunkTypes": ["Text"]},
"bypassUser": True,
},
timeout=30,
)
response.raise_for_status()
return response.json()["sessionId"]The response body contains a sessionId. It also contains a messages array, which usually holds the agent's opening greeting. Store the session ID in a variable you can reach from the code that sends messages and ends the session.
How the pieces fit
Note that the token comes from your My Domain, while the session request goes to api.salesforce.com. The instanceConfig.endpoint field tells the Agent API which org to connect to.
Common errors
- 401 Unauthorized. The token is missing, expired, or was requested for a different org. Request a new token.
- 404 Not Found. The agent ID is wrong, or the agent is not activated. Check both in Agent Builder.
- 400 Bad Request. The body is malformed, often because
instanceConfig.endpointis missing or has a trailing path. - 403 Forbidden. The External Client App lacks the required scopes, or the run-as user cannot access the agent.
Exercise
Your create-session call returns 404 even though your token works. Name two things to check.
Show answer
Check that the agent ID in the URL is correct, and that the agent is activated. A valid token rules out most authentication problems, so the agent itself is the likely cause.