Gathering detailed insights and metrics for salesforce-agent-api-client
Gathering detailed insights and metrics for salesforce-agent-api-client
Gathering detailed insights and metrics for salesforce-agent-api-client
Gathering detailed insights and metrics for salesforce-agent-api-client
npm install salesforce-agent-api-client
Typescript
Module System
Node Version
NPM Version
JavaScript (97.66%)
TypeScript (2.34%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
CC0-1.0 License
8 Stars
9 Commits
6 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Jun 25, 2025
Latest Version
1.0.1
Package Id
salesforce-agent-api-client@1.0.1
Unpacked Size
40.34 kB
Size
9.59 kB
File Count
7
NPM Version
10.9.2
Node Version
22.14.0
Published on
Mar 04, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
See the API documentation and the Postman collection for more information on the Salesforce Agent API.
Here's an example that will get you started quickly with streaming events.
Install the client library and dotenv
:
1npm install salesforce-agent-api-client dotenv
Create a .env
file at the root of your project and configure it with the the following template:
1instanceUrl= 2clientId= 3clientSecret= 4agentId=
Create an index.js
file with the following code:
1import * as dotenv from 'dotenv'; 2import AgentApiClient from 'salesforce-agent-api-client'; 3 4// Hard-coded prompt used for a single demo run 5const SAMPLE_PROMPT = 'What does "AI" stand for?'; 6 7// Load config from .env file 8dotenv.config(); 9const config = { 10 instanceUrl: process.env.instanceUrl, 11 clientId: process.env.clientId, 12 clientSecret: process.env.clientSecret, 13 agentId: process.env.agentId 14}; 15 16// Configure Agent API client 17const client = new AgentApiClient(config); 18 19// Authenticate 20await client.authenticate(); 21 22// Prepare SSE stream event handler 23function streamEventHandler({ data, event }) { 24 const eventData = JSON.parse(data); 25 console.log('Event: %s', event); 26 console.log(JSON.stringify(eventData, null, 2)); 27 // TODO: add custom logic to process events 28} 29 30// Prepare SSE stream disconnect handler 31async function streamDisconnectHandler() { 32 // On disconnect, close the session 33 await client.closeSession(sessionId); 34} 35 36// Create a new session 37const sessionId = await client.createSession(); 38const variables = []; 39try { 40 // Sends an streaming message 41 const eventSource = client.sendStreamingMessage( 42 sessionId, 43 SAMPLE_PROMPT, 44 variables, 45 streamEventHandler, 46 streamDisconnectHandler 47 ); 48} catch (error) { 49 console.log(error); 50 await client.closeSession(sessionId); 51}
Run the code with node index.js
.
If everything goes well, the output should look like this:
Agent API: authenticated on https://coralcloudresorts19-dev-ed.develop.my.salesforce.com (API endpoint: https://api.salesforce.com)
Agent API: created session a4923398-0d60-4529-9f7a-91f021409875
Agent API: sending async message 1740068546539 with text: What does AI stand for?
Event: INFORM
{
"timestamp": 1740068551742,
"originEventId": "1740068546968-REQ",
"traceId": "66b8d7d8f3aac7bb404730970c88659d",
"offset": 0,
"message": {
"type": "Inform",
"id": "f5d3d83f-3bc7-4d81-9f8f-4b7e75522aa3",
"feedbackId": "12636229-b716-4fcd-ba0b-6a498e27caab",
"planId": "12636229-b716-4fcd-ba0b-6a498e27caab",
"isContentSafe": true,
"message": "How can I assist you with any customer support issues today?",
"result": [],
"citedReferences": []
}
}
Event: END_OF_TURN
{
"timestamp": 1740068551746,
"originEventId": "1740068546968-REQ",
"traceId": "66b8d7d8f3aac7bb404730970c88659d",
"offset": 0,
"message": {
"type": "EndOfTurn",
"id": "696fd6f5-53bc-4366-b37c-fbf0a74ce992"
}
}
SSE disconnected. Preventing auto reconnect.
Agent API: closed session a4923398-0d60-4529-9f7a-91f021409875
Object that describes the client configuration:
Name | Type | Description |
---|---|---|
instanceUrl | string | Your Salesforce Org domain. |
clientId | string | Connected app client ID. |
clientSecret | string | Connected app client secret. |
agentId | string | Agent ID. |
The client uses debug level messages so you can lower the default logging level if you need more information.
The documentation examples use the default client logger (the console). The console is fine for a test environment but you'll want to switch to a custom logger with asynchronous logging for increased performance.
You can pass a logger like pino in the client constructor:
1import pino from 'pino'; 2 3const config = { 4 /* your config goes here */ 5}; 6const logger = pino(); 7const client = new PubSubApiClient(config, logger);
Client for the Salesforce Agent API
AgentApiClient(configuration, [logger])
Builds a new Agent API client.
Name | Type | Description |
---|---|---|
configuration | Configuration | The client configuration (authentication...). |
logger | Logger | An optional custom logger. The client uses the console if no value is supplied. |
async authenticate() → {Promise.<void>}
Authenticates with Salesforce.
Returns: Promise that resolves once the client is authenticated.
async createSession() → {Promise.<string>}
Creates an agent session.
Returns: Promise that holds the session ID.
async sendSyncMessage(sessionId, text, variables = []) → {Promise.<any>}
Sends a synchronous prompt to the agent.
Name | Type | Description |
---|---|---|
sessionId | string | An agent session ID. |
text | string | The prompt sent to the agent. |
variables | Object[] | Optional context variables. |
Returns: Promise that holds the agent's response.
async sendStreamingMessage(sessionId, text, variables = [], onMessage, onDisconnect) → EventSource
Sends an asynchronous prompt to the agent.
Name | Type | Description |
---|---|---|
sessionId | string | An agent session ID. |
text | string | The prompt sent to the agent. |
variables | Object[] | Context variables. |
onMessage | MessageCallback | Message callback function. |
onDisconnect | function() | Optional disconnect callback function. |
Returns: a SSE event source. See EventSource for implementation details.
MessageCallback(Object: message)
is a callback function from EventSource
. Notable message
properties are as follow:
Name | Type | Description |
---|---|---|
event | string | Event type. One of INFORM , ERROR or END_OF_TURN . |
data | string | The event data. Can be empty. |
async closeSession(sessionId) → {Promise.<void>}
Closes the agent session.
Name | Type | Description |
---|---|---|
sessionId | string | An agent session ID. |
Returns: Promise that resolves once the session is closed.
async submitFeedback(sessionId, feedbackId, feedback, feedbackText) → {Promise.<void>}
Submits feedback to the agent.
Name | Type | Description |
---|---|---|
sessionId | string | An agent session ID. |
feedbackId | string | Feedback ID. |
feedback | string | feedback type (GOOD or BAD ) |
feedbackText | string | Optional feedback text |
Returns: Promise that resolves once the feedback is saved.
No vulnerabilities found.
No security vulnerabilities found.