Prerequisites
Before using this example, make sure you:
- Set up an OpenAI API key.
- Set up and configure a Salesforce instance with an External Client App.
- Configure a Salesforce OIDC connection in Auth0 with the following scopes:
openid
api
refresh_token
offline_access
- Complete the User authentication quickstart to create a Next.js app integrated with Auth0.
- Set the
SALESFORCE_INSTANCE_URL
in your.env
file.
.env
1. Define the Vercel AI tool and backend API route
Create an AI tool that gets a list of opportunities from Salesforce and a backend route that uses Auth0 to get a Salesforce access token:- When the tool calls
auth0.getAccessTokenForConnection()
, passsfdc
as the connection name to get a Salesforce access token.
app/api/chat/route.js
2. Call from the frontend Chat UI
Use the@ai-sdk/react
hook to wire up the chat component:src/components/chat.tsx
3. Example UI
Navigate tohttps://localhost:3000
to see the chat UI:
Get me 2 opportunities from Salesforce
, GPT-4 interprets the request and invokes the listOpportunities
tool, which securely calls the Salesforce API using access tokens obtained via Auth0 and returns the results.