Welcome to the world of intelligent conversational agents! If you are looking to build smarter customer interactions using Salesforce, you are in the right place. The Salesforce Einstein Agent API is your toolkit for creating those seamless, smart experiences.
Think of this guide as your roadmap. We are going to take you from "zero" to "first API call," ensuring you have everything set up correctly along the way. Let's dive in!
Phase 1: Getting Your Gear Ready (Prerequisites)
Before we start building, we need to make sure you have the keys to the workshop. You strictly need two things:
- Salesforce Developer Account: You cannot build without a sandbox. If you don’t have one, go Sign up for a free Salesforce Developer account.
- Basic Knowledge: It will be much easier if you are already comfortable with Salesforce concepts, REST APIs, and JSON.
Phase 2: Flipping the Switch
First, we need to make sure the features are actually active in your environment.
- Log In to your Salesforce Developer account.
- Navigate to Setup and search for Einstein.
- Verify Availability: Ensure that Einstein features are available in your organization. You might need specific licenses or editions, so check this first!
Phase 3: The Gateway (Creating a Connected App)
To let your code talk to Salesforce, you need a "Connected App." Think of this as creating a secure ID card for your application.
- Navigate to Setup: Click the gear icon in the top right and select Setup.
- Access App Manager: In the Quick Find box, type App Manager and click it.
- Create New Connected App: Click on the button labeled New Connected App.
- Fill in Details:
- Connected App Name: Pick a descriptive name.
- API Name: Let this auto-populate.
- Contact Email: Enter your email.
- Enable OAuth Settings: This is crucial. Check the box for Enable OAuth Settings.
- Set the Callback URL: For testing purposes, you can use the local host URL below. Note: You must replace this with your actual redirect URL later for production.
https://localhost - OAuth Scopes: You must add these specific scopes:
- Full access (full)
- Perform requests on your behalf at any time (refresh_token, offline_access)
- Save: Click Save.
IMPORTANT: Immediately note down your Consumer Key and Consumer Secret. You will not be able to authenticate without them.
Phase 4: Getting the Secret Handshake (Access Token)
Now we need to get an access token. This token is what authorizes your specific API calls. You can use a tool like Postman or curl for this.
1. The Request
Make an HTTP POST request to this specific endpoint:
POST https://login.salesforce.com/services/oauth2/token
2. The Body
Set your content type to application/x-www-form-urlencoded and include these exact parameters:
- grant_type: password
- client_id: (Your Consumer Key)
- client_secret: (Your Consumer Secret)
- username: (Your Salesforce username)
- password: (Your Salesforce password + your security token)
3. The Result
If successful, you will receive a JSON response containing an access_token. Keep this safe; you will use it for every future call.
Phase 5: Making Contact (Your First API Call)
Now for the fun part. Let's create an Agent Session.
1. Craft the HTTP Request
Format your POST request like this:
POST https://<your_instance>.salesforce.com/services/data/vXX.X/sobjects/AgentSession
- Replace
<your_instance>with your specific Salesforce instance. - Replace
vXX.Xwith your API version.
2. Set Request Headers
You must include these headers:
- Authorization: Bearer <your_access_token>
- Content-Type: application/json
3. Define the Request Body
Copy this JSON structure exactly for your body:
{
"agentId": "yourAgentId",
"sessionId": "someSessionId",
"userId": "someUserId",
"input": "User's input message"
}
4. Send It!
Execute the request. If you get a 200 OK or 201 Created status, congratulations! You have just successfully used the Einstein Agent API.
Phase 6: Next Steps
- Handle Responses: Always check the JSON response for data and implement error handling for failed requests.
- Iterate: Test your application flows and refine the logic.
- Explore: Look into managing session flows or integrating other CRM data.
Check the Salesforce Developer Documentation regularly for updates, and don't hesitate to visit the Salesforce Developer Forums if you get stuck!