Sub topic 1
What the Agent API is and when to use it
Prerequisites
This chapter assumes you already have the following:
- A working knowledge of REST APIs and HTTP methods such as GET, POST and DELETE.
- Comfort reading JSON request and response bodies.
- A basic idea of what an OAuth 2.0 access token is.
- A Salesforce org with Agentforce enabled and at least one agent built.
- Enough Python or curl to read short examples.
What the Agent API does
The Agent API is a set of HTTP endpoints that lets an external application hold a conversation with an Agentforce agent. Your code sends text to the agent and gets the agent's reply back. The agent runs inside Salesforce, so it uses the same topics, actions and instructions you configured in Agent Builder.
You don't build a second copy of the agent for your integration. You call the one that already exists.
Agent API versus a Salesforce channel
Agentforce agents are normally reached through a Salesforce channel, such as an Experience Cloud site, a messaging channel or Slack. The channel supplies the chat window and handles the connection to the agent.
With the Agent API, your own system takes the place of that channel. You own the user interface, the authentication and the calling logic.
| Aspect | Salesforce channel | Agent API |
|---|---|---|
| User interface | Provided by the channel | You build it |
| Who calls the agent | The channel, inside Salesforce | Your external system, over HTTP |
| Authentication | Handled by the channel | Your code gets an OAuth access token |
| Best for | Standard chat experiences | Custom apps, backends and other systems |
When to use it
The Agent API fits when the standard channels do not reach where your users are. Typical cases:
- A mobile app or custom web portal that needs an in-app assistant.
- A backend service that asks the agent a question as one step in a workflow.
- A voice or messaging platform that Salesforce does not integrate with directly.
- Automated tests that send scripted messages to an agent and check the replies.
If a standard channel already covers your need, use it. The API gives you control, but you take on the work of building and securing the client.
How a conversation works
Every conversation follows the same pattern. You authenticate, open a session, exchange messages, and close the session.
A session is one conversation with the agent. The agent keeps the context of earlier messages inside a session, so a follow-up such as "and the second one?" makes sense to it. A new session starts with no memory of earlier ones.
Messages can be returned in two ways. A synchronous call waits and returns the full reply at once. A streaming call sends the reply in pieces as it is generated, using server-sent events.
What you need before calling
The remaining chapters build these pieces in order:
- An activated agent and its agent ID.
- An External Client App with the right OAuth scopes.
- An access token from your My Domain, using the client credentials flow.
- A session, followed by messages, followed by ending the session.
Exercise
Your team wants to put an Agentforce agent inside a custom mobile app that has its own login and chat screen. Should you use a Salesforce channel or the Agent API, and which part of the work becomes your responsibility?
Show answer
Use the Agent API. The mobile app replaces the channel, so your team builds the chat interface, obtains an OAuth access token, creates and ends sessions, and sends and displays messages. The agent's behavior stays configured in Salesforce.
Exercise
Name the four stages of an Agent API conversation in order.
Show answer
Authenticate to get an access token, create a session, send messages and read replies, then end the session.