Sub topic 1
What the Agent API is and when to use it
The Agent API is an HTTP interface that lets software outside Salesforce hold a conversation with an Agentforce agent. Your application authenticates, opens a session, exchanges messages, and closes the session. The agent runs its topics, actions, and instructions as it would in any other channel.
This chapter explains what the API is, how it differs from the channels built into Salesforce, and how to decide whether it fits your project. It also gives a map of the calls you will build in the rest of the tutorial.
Prerequisites
This tutorial assumes you already have the following:
- Basic knowledge of REST APIs and HTTP methods (GET, POST, DELETE) and of JSON request and response bodies.
- A rough idea of what an OAuth 2.0 access token is: a short-lived string you send in an
Authorization: Bearerheader. - A Salesforce org with Agentforce enabled and at least one agent built. It does not need to be a polished agent. A simple one with a single topic works for testing.
- Enough Python or curl to read short examples. The code later in the tutorial uses the
requestslibrary and shell commands.
You do not need prior experience with Salesforce Connected Apps, External Client Apps, or streaming protocols. Later chapters introduce them.
What the Agent API does
An Agentforce agent normally lives behind a channel. Examples are the Salesforce Experience Cloud site, Enhanced Chat, Slack, or the Lightning chat panel. Each channel handles authentication, session handling, and rendering for you.
The Agent API removes the channel. You call the agent directly, and your code takes over those three responsibilities:
- Authentication: your application gets an OAuth access token and sends it with every request.
- Session handling: your application creates a session, keeps its ID, and ends it when the conversation is over.
- Rendering: your application decides what to do with the reply. It might print it in a terminal, show it in a custom web UI, read it aloud, or pass it to another system.
The agent itself does not change. It uses the same topics, instructions, and actions, and it runs under the same Einstein Trust Layer protections as it does in a channel. What changes is who sits in front of it.
The API exposes the agent through a small set of endpoints under https://api.salesforce.com/einstein/ai-agent/v1. The token request goes to a different host, your org's My Domain.
The shape of a conversation
Every integration follows the same pattern. The numbered steps below match the diagram.
- Request an access token from your My Domain, using the OAuth client credentials flow.
- Create a session for a specific agent. The response contains a
sessionId. - Send one or more messages to that session. Each reply comes back either as one complete response or as a stream of chunks.
- End the session.
The table summarizes each call. Later chapters cover each one in detail.
| Phase | Method and path | Host | Result |
|---|---|---|---|
| Get a token | `POST /services/oauth2/token` | Your My Domain | Access token |
| Create a session | `POST /agents/{agentId}/sessions` | `api.salesforce.com` | `sessionId` |
| Send a message | `POST /sessions/{sessionId}/messages` | `api.salesforce.com` | Complete reply |
| Stream a message | `POST /sessions/{sessionId}/messages/stream` | `api.salesforce.com` | Server-sent events |
| End a session | `DELETE /sessions/{sessionId}` | `api.salesforce.com` | Session closed |
The session is the central idea. The agent remembers earlier turns of a conversation only within one session. If you create a new session for every message, the agent treats each message as the start of a new conversation. If you never end sessions, they stay open and use resources until they time out.
When the Agent API is the right choice
Use the Agent API when the conversation must happen in a place Salesforce channels do not reach, or when you need control over the experience. Common cases:
- A custom front end. A mobile app, a customer portal on a different platform, or a kiosk where you own the interface.
- Another system as the caller. A call-center platform, an ERP, or a messaging gateway forwards user messages to the agent and relays the replies.
- Voice and device integrations. A speech-to-text service sends text to the agent, and a text-to-speech service reads the answer.
- Automation and testing. A script sends a fixed set of questions to an agent after every change and compares the answers. This is a practical way to build regression tests.
- Backend workflows. A service asks the agent to summarize or classify something as one step in a larger pipeline, with no human involved.
When another option is better
The Agent API is not always the best fit. Before you build against it, check these alternatives:
| Situation | Better option | Reason |
|---|---|---|
| Users are already in Slack, Experience Cloud, or the Salesforce UI | The built-in channel | Authentication, sessions, and rendering are already done. |
| You only need to run one piece of Salesforce logic, such as an Apex action | Invoke the action or flow directly | You avoid a language model call and its latency and cost. |
| You need the agent to call out to an external system | An agent action that calls the external system | The direction is reversed. The agent calls out, and your system does not call in. |
| You need a guaranteed, deterministic answer | Ordinary APIs | Agent replies are generated and can vary between runs. |
The last two rows are common sources of confusion. The Agent API sends conversations into an agent. It does not let an agent reach an outside system. Calls from an agent to outside systems are configured as agent actions inside the agent, not through this API.
Trade-offs to plan for
Choosing the Agent API gives you control, and the extra work falls to you.
- You own security. Your application stores the client secret and handles tokens. A leaked secret lets someone talk to your agent as the run-as user. The security chapter returns to this.
- You own reliability. Tokens expire, networks fail, and sessions can time out. Your code needs retry and refresh logic.
- Replies are not instant. The agent may run several actions before it answers. A synchronous call can take many seconds, so set timeouts deliberately. Streaming reduces the perceived delay for interactive users.
- Usage is metered. Agent conversations consume Salesforce usage, so automated tests and loops should be bounded.
- Behavior depends on the agent. If the agent is not activated, or the user it runs as lacks access to the data its actions need, the API call may succeed while the answer is poor or empty. The cause is configuration, not your HTTP code.
Common mistakes at this stage
These mistakes appear often in first attempts, and the chapters that follow address each one:
- Calling the token endpoint on
api.salesforce.cominstead of your My Domain. - Using an agent that is still in draft or deactivated, or its name instead of its ID.
- Creating a new session for every message and then wondering why the agent forgets the conversation.
- Skipping the end-session call, which leaves sessions open.
- Hard-coding the client secret in source code rather than reading it from the environment.
What comes next
The following chapters follow the order of a real build. You will prepare the agent and find its ID. You will create an External Client App with the right scopes and learn how the client credentials flow and its run-as user work. Then you will request a token, create a session, send messages, parse replies, and end the session. After that come streaming, context variables, error handling, security, and troubleshooting.
Exercise
A team wants customers who already use Slack to chat with their Agentforce agent. Another team wants a mobile app they built themselves to chat with the same agent. Which team needs the Agent API, and why?
Show answer
The mobile app team needs the Agent API. Slack is a built-in channel that already handles authentication, sessions, and rendering. A custom mobile app has no channel, so it must authenticate with OAuth, create and end sessions, and display replies itself, and the Agent API provides the HTTP interface for that.
Exercise
Your app sends every user message to POST /agents/{agentId}/sessions as a new session, and the agent keeps asking the user to repeat information given earlier. What is wrong, and how do you fix it?
Show answer
Each call creates a new session, and the agent remembers earlier turns only within one session. Create the session once at the start of the conversation, keep the sessionId, send every later message to /sessions/{sessionId}/messages, and end the session when the conversation finishes.