Sub topic 8
Sending a Message and Getting a Reply
This chapter covers the synchronous message call. You send one message to an open session and wait for the full reply. It assumes you can already get an access token and create a session, and that you know basic REST and JSON.
How a synchronous message works
A synchronous call is one HTTP request that stays open until the agent has finished its answer. The agent may run topics, actions, and the language model before it responds. The call can therefore take several seconds, so set a longer timeout than you use for the token call.
The request goes to the session's messages endpoint:
POST https://api.salesforce.com/einstein/ai-agent/v1/sessions/{sessionId}/messagesSend the bearer token in the Authorization header. The body holds one message object.
| Field | Meaning |
|---|---|
| `sequenceId` | A number that orders the messages in the session. Start at 1 and add 1 for each message you send. |
| `type` | The kind of message. Use `Text` for plain user text. |
| `text` | What the user said. |
A working example
This function is the middle step of the full conversation example. It assumes token and session_id already exist.
import requests
API_BASE = "https://api.salesforce.com/einstein/ai-agent/v1"
def send_message(token: str, session_id: str, text: str, sequence_id: int) -> str:
response = requests.post(
f"{API_BASE}/sessions/{session_id}/messages",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
json={
"message": {
"sequenceId": sequence_id,
"type": "Text",
"text": text,
}
},
timeout=60,
)
response.raise_for_status()
return response.json()["messages"][0]["message"]Call it again with sequence_id=2 for a follow-up. The session keeps the earlier turns, so the agent can answer "What about the second one?" without repeating the context.
What comes back
The response body contains a messages array. Each entry has a type and a message field with the agent's text. The example takes the first entry, which is enough for simple replies. The next chapter covers the other message types and how to pick the right entry.
Common errors
- Reusing or skipping a
sequenceId. Keep a counter per session and increase it for every message you send. A repeated value can be rejected or confuse the ordering. - Timeout too short. If the client gives up while the agent is still working, you get an exception even though the agent may have finished. Use 60 seconds or more for this call.
- Sending to an ended or expired session. The call fails with a client error. Create a new session and start again.
- Expired token. A 401 response means the access token is no longer valid. Request a new one and retry.
- Calling
raise_for_statusand discarding the body. The error body usually says what went wrong, so log it.
Exercise
Your app sends a first message with sequenceId 1 and gets a reply. What sequenceId should the next message use, and why does the session still need to be open?
Show answer
Use 2. The sequenceId orders the messages you send in the session, so it increases by 1 each time. The session must still be open because it holds the conversation history. If it has ended or expired, the call fails and you must create a new session.
Exercise
A synchronous message call times out after 30 seconds. Why is a plain retry risky, and what is a safer first step?
Show answer
The agent may have finished the work and run its actions, so a retry can repeat side effects. A safer first step is to raise the timeout for this call, for example to 60 seconds or more. Retry only after you have confirmed the first request did not complete.