Make Your Apex Talk! Trigger Einstein Bot Using REST API in Salesforce


NSIQ Icon
November 7, 2025

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

  1. Go to Setup → Einstein Bots → Enable Einstein Bots.
  2. Create a new bot, for example: ConvoBot.
  3. Choose your Service Cloud license and preferred language (e.g., English).
  4. Click Save and Activate your bot.

Step 2: Configure Dialogs and Bot Setup

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

Configure Dialogs and Bot Setup

Step 3: Create a Connected App

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

Create a Connected App

Step 4: Create Auth Provider

  1. Go to Setup → Auth. Providers → New.
  2. Select Salesforce as the provider type.
  3. 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
  4. Save and copy the Callback URL for reference.

Step 5: Create Named Credential

  1. Go to Setup → Named Credentials → New.
  2. Enter name: WhatsaapNameCredApp.
  3. URL: https://<your-domain>.my.salesforce.com
  4. Set Authentication Protocol to the Auth Provider you just created
    (e.g., EinsteinBotAuth).
  5. Identity Type: Named Principal.
  6. Save your Named Credential.

Step 6: Connect Einstein Bot with Connected App

  1. Open your Einstein Bot.
  2. Open your Bot Version (Draft).
  3. Scroll to Connections and click Add.
  4. Select API as the connection type.
  5. Under Deployment, choose your Connected App (e.g.,
    WhatsaapNewConnectedApp).
  6. Save.

Connect Einstein Bot with Connected App

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.