Live Agent API calls to initiate a Chat- A small demo using Postman tool


  • You might have a requirement to implement an API mechanism to initiate a chat and also to end the chat with API only
  • As soon as and you ended the chat with API, you may have a requirement to get the details of attached records(nothing but caseId, ContactId) in the subsequent call '/chat/rest/System/Messages' to show in post chat
  • To achieve this please follow the below steps
  • Create a post-chat page and add the page as your chat button post-chat page
  • To get attachedRecords after you closed the chat with chatEnd API, you need to make another API call: /chat/rest/System/Messages

  • Request type is GET
  • By using the API call you can generate live agent session that can be used in subsequent calls to initiate chat, send messages, pull messages and chat End
Headers you need to set:
X-LIVEAGENT-API-VERSION: 45
X-LIVEAGENT-AFFINITY: null

Response: In the response you will see Key and affinityToken.

image.png

  • Request type is POST
  • To initiate the chat via API

Headers you need to set:
  1. X-LIVEAGENT-API-VERSION: 45
  2. X-LIVEAGENT-SESSION-KEY: “Key from Previous response”
  3. X-LIVEAGENT-AFFINITY: “affinityToken from previous Response”
  4. X-LIVEAGENT-SEQUENCE: 1

Body- Raw: If you have any requirement to create case and contact via pre-chat form use the below raw body where it contains pre-chat details. If you don’t wants to create contact then make the prechatDetails as blank ( "prechatDetails": [], "prechatEntities": []).



Response: The response will be ‘OK’

image.png

  • Request type is POST
  • By using the API mentioned above you can send message to agent

Headers you need to set:
  1. X-LIVEAGENT-API-VERSION: 45
  2. X-LIVEAGENT-SESSION-KEY: “Key from Previous response - step-1”
  3. X-LIVEAGENT-AFFINITY: “affinityToken from previous Response - - step-1”

Body Raw: {"text" : "Hi, I need help with installing product."}

Response: The response will be ‘OK’

image.png

  • Request type is GET
  • By using this API call you will see if the chat is successful or not, messages and Agent info
  • When Agent/API close the chat and the response you will see is all the attached records

Headers you need to set:
  1. X-LIVEAGENT-API-VERSION: 45
  2. X-LIVEAGENT-SESSION-KEY: “Key from Previous response - step-1”
  3. X-LIVEAGENT-AFFINITY: “affinityToken from previous Response - - step-1”
Response: Below screen shot was captured after agent ends the chat, so here you can see case details in response
  1. While chat is active you can see chat messages when you execute this request
  2. If you execute this API call after agent closed the chat, you will see attached records like caseID, contactId as mentioned in below screenshot
image.png

3. If you execute this request after the following catEnd request: /chat/rest/Chasitor/ChatEnd, you will not see any attachedRecords in the response. But to get the attached record after chatEnd call, your chat button should have associated post chat page


  • Request type is POST
  • To end the active chat and chat sessions

Headers you need to set:
  1. X-LIVEAGENT-API-VERSION: 45
  2. X-LIVEAGENT-SESSION-KEY: “Key from Previous response - step-1”
  3. X-LIVEAGENT-AFFINITY: “affinityToken from previous Response - - step-1”
  4. X-LIVEAGENT-SEQUENCE: 2
Body Raw: {reason: “client”}

Response: The response is just ‘OK’

image.png

Source: Salesforce provided KB articles
  1. https://help.salesforce.com/articleView?id=000331168&type=1&mode=1
  2. https://help.salesforce.com/articleView?id=000340657&type=1&mode=1
  3. https://developer.salesforce.com/docs/atlas.en-us.live_agent_rest.meta/live_agent_rest/live_agent_rest_ChatEnd.htm
image.pngimage.png
  • Here you can see the attachedRecords details.
                                        Thanks for Blogging!!!

Post a Comment

5 Comments

  1. Calling an API involves sending a request to a server and receiving a response with data that can be processed by an application. APIs can enable integration with third-party services, access to data, and automation of tasks, making them essential in modern software development.

    ReplyDelete
  2. Calling an API involves sending a request to a server and receiving a response with data that can be processed by an application. APIs can enable integration with third-party services, access to data, and automation of tasks, making them essential in modern software development.

    ReplyDelete
  3. very informative blog... API calls are the lifeblood of modern software systems, enabling seamless communication between different applications and services. They facilitate data exchange, functionality integration, and automation, powering countless digital experiences. With APIs, developers can unlock the power of collaboration and create innovative solutions that drive technological progress and enhance user experiences.

    ReplyDelete
  4. API calls are the backbone of modern software development, enabling applications to communicate with external services and retrieve data or trigger specific actions. They facilitate seamless integration, allowing developers to leverage the functionality and resources of APIs, unlocking a world of possibilities and enhancing the capabilities of their own applications.

    ReplyDelete
  5. This blog provides a concise yet comprehensive explanation of what an API call is. It breaks down the concept in a beginner-friendly manner, making it easy to grasp even for non-technical readers. The real-world examples and analogies further enhance the understanding of API calls. A great read for anyone curious about APIs!

    ReplyDelete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)