Make Your Apex Talk! Trigger Einstein Bot Using REST API in Salesforce
This guide explains how to integrate Einstein Bot with Apex using REST API within the same Salesforce org.
Follow these simple steps to set up your bot, authentication, and make a callout from Apex to your Einstein Bot.
Step 1: Enable Einstein Bots
- Go to Setup → Einstein Bots → Enable Einstein Bots.
- Create a new bot, for example: ConvoBot.
- Choose your Service Cloud license and preferred language (e.g., English).
- Click Save and Activate your bot.

Step 2: Configure Dialogs and Bot Setup
- Open your bot in Einstein Bot Builder.
- Add dialogs such as Greeting, Help, or Case Info to
define how your bot responds. - Add Prompts (messages the bot will send) and Variables (to collect
input from users). - Under Connections, select API — this allows the bot to communicate externally through REST.

Step 3: Create a Connected App
- Go to Setup → App Manager → New Connected App.
- Enter name: WhatsaapNewConnectedApp.
- Check Enable OAuth Settings.
- Set Callback URL to: https://login.salesforce.com/services/oauth2/success
- Select these OAuth Scopes:
- Full access (full)
- Perform requests on your behalf (refresh_token, offline_access)
- Click Save and wait 10 minutes for the app to activate.

Step 4: Create Auth Provider
- Go to Setup → Auth. Providers → New.
- Select Salesforce as the provider type.
- Fill in details:
- Consumer Key and Consumer Secret → from your Connected App.
- Authorize Endpoint URL:
https://<your-domain>.my.salesforce.com/services/oauth2/authorize - Token Endpoint URL:
https://<your-domain>.my.salesforce.com/services/oauth2/token
- Save and copy the Callback URL for reference.
Step 5: Create Named Credential
- Go to Setup → Named Credentials → New.
- Enter name: WhatsaapNameCredApp.
- URL: https://<your-domain>.my.salesforce.com
- Set Authentication Protocol to the Auth Provider you just created
(e.g., EinsteinBotAuth). - Identity Type: Named Principal.
- Save your Named Credential.
Step 6: Connect Einstein Bot with Connected App
- Open your Einstein Bot.
- Open your Bot Version (Draft).
- Scroll to Connections and click Add.
- Select API as the connection type.
- Under Deployment, choose your Connected App (e.g.,
WhatsaapNewConnectedApp). - Save.

Step 7:Save your Named Credential.
Use this Apex class to call your Einstein Bot through REST API:
public class EinsteinBotCallout {
public static void sendMessageToBot() {
try {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:WhatsaapNameCredApp/v5.1.0/bots/0XxNS000002I6nN0AS/sessions');
req.setMethod('POST');
req.setHeader('X-Org-Id', '00DNS00000Yc2y8');
req.setHeader('X-Request-ID', '36a73651-a46d-4d16-9a8a-fd436ed62e1a');
req.setHeader('Content-Type', 'application/json');
Map<String, Object> body = new Map<String, Object>();
body.put('externalSessionKey', '57904eb6-5352-4d5e-adf6-5f100572cf5d');
body.put('message', new Map<String, String>{ 'text' => 'Hi' });
body.put('forceConfig', new Map<String, String>{
'endpoint' => 'https://.my.salesforce.com'
});
Map<String,Object> responseOptions = new Map<String,Object>();
responseOptions.put('variables', new Map<String,Object>{
'include' => false,
'onlyChanged' => true
});
responseOptions.put('metrics', false);
responseOptions.put('intents', false);
responseOptions.put('entities', false);
body.put('responseOptions', responseOptions);
body.put('tz', 'America/Los_Angeles');
body.put('referrers', new List<Map<String,String>>{
new Map<String,String>{ 'type' => 'Salesforce:Core:Bot:Id', 'value' => 'string' }
});
req.setBody(JSON.serialize(body));
Http http = new Http();
HttpResponse res = http.send(req);
System.debug('HTTP Status: ' + res.getStatusCode());
System.debug('Response Body: ' + res.getBody());
} catch (Exception e) {
System.debug('Error: ' + e.getMessage());
}
}
}
Step 8: Test the Integration
Run this code in Anonymous Apex:
EinsteinBotCallout.sendMessageToBot();
Then open Debug Logs and check your logs for the bot’s JSON response.
If successful, your Apex just talked to your Einstein Bot!
Final Thoughts
You’ve now learned how to:
- Enable Einstein Bot
- Build Dialogs and Prompts
- Create a Connected App, Auth Provider, and Named Credential
- Link the Connected App with the Bot
- Trigger Einstein Bot directly from Apex
This setup lets you make your Apex “talk” to Einstein Bot — perfect for automating conversations, integrating with workflows, or building AI-powered user interactions inside Salesforce.
related blog
