Salesforce Agent API
7K views
Feb 17, 2025
Salesforce Agent API can be used to invoke Salesforce Agentforce Agents using APIs. Blog Post: https://www.infallibletechie.com/2025/02/salesforce-agent-api.html
View Video Transcript
0:06
hello everyone in this video we are
0:08
going to see Salesforce agent
0:12
API in order to access Salesforce agent
0:16
force agents we can make use of
0:18
Salesforce agent
0:21
API the first thing uh in order to
0:24
access Salesforce agent apas we can make
0:27
use of client credentials connected app
0:31
go to sales for setup create a Connected
0:35
app enable enable o settings so this
0:39
should be
0:40
selected in the call back URL use
0:43
https
0:46
colon login.
0:48
salesforce.com in the selected o o
0:52
Scopes select access chatbot Services
0:56
access the Salesforce API platform
0:58
manage user data via apis and also
1:02
perform requests at any time in order to
1:05
get the refresh token and offline access
1:07
so make sure the selected o Scopes
1:11
include all these next
1:14
deselect
1:16
require proof key for code exchange
1:20
extension for support authorization
1:22
flows require secret for web server
1:25
flows and also the require secret for
1:29
refresh token flow so make sure all
1:31
these are
1:32
deselected next enable enable client
1:36
credentials flow and issue Json web
1:40
token JWT based access tokens for name
1:44
users make sure these are all enabled in
1:46
your Connected
1:48
app in order to select the run as user
1:53
the user should have APA only permission
1:56
once the Connected app is created click
1:58
the manage button
2:00
click edit policies and select run as
2:03
users in the client credential flows
2:06
section when you do this make sure the
2:08
user you are selecting have user you are
2:11
selecting have APA only permission in
2:14
their profile if this is not there your
2:18
Connected app won't work and we cannot
2:21
get generate the access token so make
2:24
sure the user have AP only permission if
2:27
you create an uh app app with user who
2:32
is not having AP only permission and
2:35
then if you try to edit the app and then
2:36
if you try to select the user it won't
2:39
work you have to create the Connected
2:41
app from the scratch again so that you
2:43
can select the right user with ap only
2:46
permission on the
2:47
profile next in Salesforce setup we have
2:50
to go to agents we have to select the
2:53
agent once you select the agent you can
2:56
click the add button under connections
2:59
stab here you have to select API here
3:02
you have to select the Connected app
3:04
which you created and here you have to
3:06
give the integration name so this is
3:08
like a uh label you can give any name
3:11
for your reference once it is done it
3:15
will link your uh Connected app to your
3:19
agent force
3:20
agent in order to test it make sure your
3:23
agent force agent is
3:26
active so now the configuration is done
3:30
now we have to first generate the access
3:34
token next we have to create a session
3:40
ID next we can send a message to our
3:44
agent force
3:47
agent and finally once the conversation
3:50
is done you can end the session by doing
3:53
a delete
3:55
request let's see this in
3:57
action the first step is you have to do
4:00
a post request to your Salesforce my
4:03
domain URL /s Services /2 SL token here
4:09
in the body you have to pass the client
4:11
ID client secret which will be your
4:15
consumer key and consumer secret from
4:18
your client credentials Connected app in
4:22
in the grand type we have to pass client
4:26
credentials okay in the header you have
4:28
to pass content type type as application
4:32
SLX iph www iph form I URL encoded so
4:38
this should be your header and this
4:40
should be your body you have to do a
4:42
post
4:45
request so here you will get the access
4:47
token and also you will get the API
4:50
instance
4:51
URL using a simple
4:53
script I am storing the access token and
4:56
APA
4:58
URL inside these two Global
5:02
variables next step is we have to create
5:06
a session once you create a session you
5:08
will get
5:10
the session ID so that you can send a
5:13
message and also finally you will be
5:15
able to end the
5:17
session in order to create a session you
5:21
have to make use of the
5:24
instance URL slash Einstein slash
5:30
AA agent AA iph agent slv1 SL agents
5:35
slash the 18 digigit ID of your agent
5:38
force agent force agent SL
5:41
sessions once you open your agent force
5:44
agent you will be able to see the 18
5:48
digigit ID here so you have to make use
5:50
of this 18 digigit
5:58
ID in this in the external session key
6:01
you have to pass a uu ID in order to
6:04
generate The UU ID you can make use
6:08
of this sample code in Salesforce we
6:12
have uu ID class in this uu ID class you
6:17
can make use of random uu ID method so
6:19
that you will be able to
6:22
generate uh gener uu ID value using
6:26
which you can pass it to the external
6:29
session key let's run this code so that
6:32
we will
6:33
get the uu
6:41
ID so this will be my uu
6:52
ID so I'm passing the body as external
6:56
session key in the instance
6:57
configuration I'm passing my my domain
7:00
URL streaming capabilities chunk types
7:03
I'm using text in the header I am
7:06
passing the content type as application
7:08
or
7:09
Json authorization as barer space the
7:13
access token which we got it in the
7:15
first uh post
7:18
request so I'm going
7:20
to send the request I'm able to get the
7:24
session ID successfully in order to
7:26
store this session ID in a global
7:28
variable I'm using using this script
7:30
which will uh store the session ID in
7:34
agent force session ID
7:36
variable okay so session is also done
7:39
now I have to send a message to the
7:42
agent force agent for that you have to
7:45
make a post request to the instant URL
7:49
SL Einstein SL AI space agent slv1 slash
7:55
sessions slash the session ID which you
7:57
got it from the previous uh post request
8:01
slash messages if you send a post
8:04
request to this it will send a
8:08
request uh to agent force agent and then
8:12
it will get the response back so I was
8:15
able to send a message opportunity
8:19
booking I'm using the sequence IDs 2
8:23
um it is saying sure I can help with
8:27
that let's start by finding your
8:29
contract record could you please provide
8:31
your email address for that in order to
8:35
send a message back to agent force agent
8:38
increase the sequence
8:41
ID and I'm going to
8:43
say my email
8:47
address is test contact it is a dummy
8:51
value but uh already there is a contact
8:54
with this email ID so it should say I
8:57
found your contact let's see
9:04
yeah it is processing it in uh agent
9:07
force agent API okay so now I got the
9:09
response back great I found your contact
9:11
now could you please provide your start
9:13
date for your products you are
9:15
interested in so so this is how we can
9:18
uh have the conversation synchronously
9:20
with our agent force agent so we created
9:23
an access token we created a session we
9:25
are able to send a message now I'm going
9:27
to end the session um assume that all
9:30
the conversation uh back and forth
9:33
between agent force agent and the user
9:35
are done now I have to close the session
9:38
so that it will end the session to me in
9:40
order to end the session you have to do
9:42
a post a delete request to your instance
9:45
Ur API instance URL SL Einstein SL AA
9:50
iph agent slv1 SL sessions slash the
9:55
agent force session ID which we got it
9:57
from create session post request
10:00
again we are making use of the API
10:02
instance URL in order to create a
10:05
session and also in order to send a
10:07
message and also in order to end the
10:10
session in the header pass x i session I
10:14
end iph reason as user request because
10:17
as an user I wanted to end the session
10:20
authorization I'm sending it as bar
10:22
space the access token which we got it
10:24
from
10:25
the first uh uh access token request so
10:29
so now if I click if I send
10:32
it okay by su by I was able to
10:36
successfully end the
10:38
session if you wanted to see the
10:41
conversation back and forth what you had
10:43
in the
10:44
Salesforce you have to go to the agent
10:47
Studio
10:49
Builder go to all sessions in the event
10:56
locks this is the latest one so you
10:59
should be able to see all the
11:02
conversation so I sent opportunity
11:05
booking and also I sent this is my email
11:08
address so back and forth whatever
11:10
conversation I had with the agent force
11:12
agent I should be able to see it in the
11:13
event locks in order to see the event
11:16
locks make sure you are enabling enrich
11:19
event locks with the conversation
11:25
data please check the video description
11:27
in the video description I have given my
11:29
blog post from the blog post you should
11:31
be able to get the sample payload which
11:34
I um we were discussing it on the video
11:38
and also if you want to generate The UU
11:40
ID you can make use of the sample Apex
11:43
code um even for this particular uu ID I
11:47
have given the sample code and also if
11:49
you wanted to learn more about it you
11:51
can use this particular blog post and
11:54
then you can learn more about Salesforce
11:56
Apex U ID class
12:04
I hope it was
12:12
helpful thank you for watching
#Customer Relationship Management (CRM)
#Data Management
#Intelligent Personal Assistants
#Internet Software
#Software