Using Salesforce Chat REST API

Ayub Ansari
2 min readMar 9, 2021

Original Article:


We may choose to build a custom live chat app using Salesforce Live Chat REST API. There may be below given reasons or any specific reasons:

  • Custom Experience:- Header, Footer, Navigation, Offline Form, Pre-chat Form
  • Security Reason:- Salesforce generated embedded service snippet code does not require authentication and works publicly. Also, the script may contain some dangerous Javascript functions like (eval(), create()) which may be against our company privacy policy.
  • Building any embedded web app which is tightly coupled with Salesforce Service Cloud.

Here are Salesforce REST API link:

Also, here are some useful content regarding same problem/requirement


  • If you try to hit Salesforce Live Chat Server endpoint (something like using any web app, you will get the CORS error because Salesforce Live Chat Server will not serve any request by the client (browser i.e javascript) as the client domain is not added as an allowed domain. So the question is can we add our domain at Live Chat Server, answer is NO, we can not whitelist.


  • As a workaround, we will have to create a proxy server that will get requests from the web app and pass the request to the Live Chat Server. Likewise, the proxy server will return the response to the web app.
  • We can orchestrate the request and response at this proxy server.
  • We can add the layers of security as well as this proxy server.


  • This server must have a very high throughput rate & low latency.
  • Must be able to cope with tons (yes, I mean it — huge numbers of HTTP request trips would be made) requests.
  • You can develop this in any technology, just for example, here are two ways-
  • Java: Here is a working repo that can be helpful:
  • Salesforce Apex REST Endpoint: Not an ideal approach keeping in mind the performance prerequisites of the server but still can work if there is not much traffic on the chat channel for you or you just want to play with it. This is explained further in this article.

Salesforce Apex REST Endpoint as Proxy Server

  • Develop an Apex class as below to make enabled for APEX REST


global with sharing class LiveChatService{


global static String doPost(String type, String payload) {

if(type == ‘availibity’){

responseBody = checkAgentAvailability();



public static String checkAgentAvailability(){

String endPoint = ‘'+BUTTONID+'&deployment_id='+DEPLOYMENTID+'&org_id='+ORGANIZATIONID;

HttpResponse response = makeHttpRequest(null, ‘GET’, endPoint);

return response.getBody();



  • Add this apex class to a site by using Guest User Profile.
  • After that, we can use this service as below in our client side code:

let CHATSERVICEENDPOINT = ‘https://<site URL>/services/apexrest/livechat/service’;

const resp = await fetch(CHATSERVICEENDPOINT, {

method: ‘POST’,

headers: {

‘Content-Type’: ‘application/json’


redirect: ‘follow’,

body: ‘{“type”:”availibity”, “payload”:””}’


const data = await resp.json();