Get the Agent ID
Session Lifecycle
Examples
Variables
Considerations
Troubleshooting
Export Agentforce Session Tracing Data
Bring the power of generative AI to your business with Agentforce. Build intelligent, trusted, and customizable AI agents and empower your users to get more done with Salesforce. Use the Agent API to communicate with AI agents directly from a REST API. Start sessions, send messages to the AI agent, receive messages, and end sessions using the API.
Agent API isn’t supported for agents of type “Agentforce (Default)”.
Note
To access the Agent API, you must set up an external client app that supports the client credential flow. These instructions show you how to get your environment set up for use with the API and demonstrate how to make your first call to the API.
You must have Agentforce enabled with at least one agent activated. See Set Up Your Agent in Salesforce Help.
To use the Agent API, you must create an external client app (ECA). To learn how to create an ECA, see Create an External Client App in Salesforce Help.
These instructions help you create a basic app with the client credentials flow, although you can use any flow that provides a JWT-based access token.
From Setup, in the Quick Find box, enter External Client App, and then select External Client Apps Manager.
Select New External Client App.
Specify a name and contact email.
Select Enable OAuth and use these OAuth scopes.

Select these additional OAuth settings.

Deselect:
Create the app.
From your app settings, click the Policy tab.
Click Edit.
From the OAuth Flows and External Client App Enhancements section, check Enable Client Credentials Flow.
Set the Run As (Username) field to the email address of a user that has at least API Only access.
Save the changes.
To create a token, you need the consumer key and consumer secret from your external client app.
All calls to the Agent API require a token. Create a token by using the consumer key, consumer secret, and your domain name.
1curl https://{MY_DOMAIN_URL}/services/oauth2/token \
2--header 'Content-Type: application/x-www-form-urlencoded' \
3--data-urlencode 'grant_type=client_credentials' \
4--data-urlencode 'client_id={CONSUMER_KEY}' \
5--data-urlencode 'client_secret={CONSUMER_SECRET}'MY_DOMAIN_URL: You can get the domain from Setup. Search for My Domain. Copy the value shown in the Current My Domain URL field.CONSUMER_KEY, CONSUMER_SECRET: You can get the consumer key and secret by following the instructions in Obtain Credentials.The previous command returns a JSON payload similar to this response.
1{
2 "access_token": "eyJ0bmsiOiJjb3JlL3Byb2QvM…(shortened)",
3 "signature": "HBb7Zf4aaOUlI1V…(shortened)",
4 "token_format": "jwt",
5 "scope": "sfap_api chatbot_api api",
6 "instance_url": "https://sample-org-eaa32a127e4a6b.my.salesforce.com",
7 "id": "https://login.salesforce.com/id/00DW…Wpg5MAC/005W…nF9IAI",
8 "token_type": "Bearer",
9 "issued_at": "1736530186928",
10 "api_instance_url": "https://api.salesforce.com"
11}Copy the access token specified in the access_token property. This token is required when making requests to the API.
After you set up your app and created a token, you’re ready to call the API. Before making the call, gather this information.
AGENT_ID: The ID of the agent that you want to interact with. The method for obtaining this ID depends on which builder you used to create your agent. See Get the Agent ID for an Agent for detailed instructions.ACCESS_TOKEN: The token that you created in Create a Token.RANDOM_UUID: A random UUID value that you provide to represent the session key. You can use this parameter to trace the conversation in your agent’s event logs.MY_DOMAIN_URL: From Setup, search for My Domain. Copy the value shown in the Current My Domain URL field.Be sure to use the right URLs for your org.
some_domain.my.salesforce.com) and not simply the domain name displayed in your browser (some_domain.lightning.force.com).api.salesforce.com with api.gov.salesforce.com in all Agent API endpoints.Important
This curl command creates a new agent session with the Agent API.
1curl --location -X POST https://api.salesforce.com/einstein/ai-agent/v1/agents/{AGENT_ID}/sessions \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer {ACCESS_TOKEN}' \
4--data '{
5 "externalSessionKey": "{RANDOM_UUID}",
6 "instanceConfig": {
7 "endpoint": "https://{MY_DOMAIN_URL}"
8 },
9 "streamingCapabilities": {
10 "chunkTypes": ["Text"]
11 },
12 "bypassUser": true
13}'The bypassUser parameter indicates whether to use the agent-assigned user instead of the logged in user. If set to true, the API uses the user associated with the agent. If set to false, the API uses the user associated with the token. For this client credentials flow scenario, we set the value to true. However, you may need to change this value depending on your use case.
Note
When this call succeeds, you receive a response with a session ID and additional info. Use the session ID to continue the conversation with the agent.
1{
2 "sessionId": "8e715939-a121-40ec-80e3-a8d1ac89da33",
3 "_links": {
4 "self": null,
5 "messages": {
6 "href": "https://api.salesforce.com/einstein/ai-agent/v1/sessions/8e715939-a121-40ec-80e3-a8d1ac89da33/messages/stream"
7 },
8 "session": {
9 "href": "https://api.salesforce.com/einstein/ai-agent/v1/agents/0XxQZ0000000Ty50AE/sessions"
10 },
11 "end": {
12 "href": "https://api.salesforce.com/einstein/ai-agent/v1/sessions/8e715939-a121-40ec-80e3-a8d1ac89da33"
13 }
14 },
15 "messages": [
16 {
17 "type": "Inform",
18 "id": "8e7cafae-0eb5-44b1-9195-21f1cd6e1f4b",
19 "feedbackId": "",
20 "planId": "",
21 "isContentSafe": true,
22 "message": "Hi, I'm an AI service assistant. How can I help you?",
23 "result": [],
24 "citedReferences": []
25 }
26 ]
27}Congratulations, you’ve successfully started using the Agent API! To continue using the API, see Agent API Examples and the Agent API Postman Collection.