This project demonstrates how to integrate OpenAI's ChatKit with Salesforce using Lightning Web Components (LWC), Apex, and Named Credentials.
This Salesforce DX project provides a proof-of-concept for embedding OpenAI ChatKit into Salesforce. It uses a custom LWC (agentBuilderLWC) and Apex controller (AgentBuilderController.cls) to securely create chat sessions and interact with OpenAI via Named Credentials.
- Apex Controller:
AgentBuilderController.clshandles session creation. - LWC:
agentBuilderLWCloads the ChatKit widget and connects it to the backend via Apex. - Static Resource:
chatkit.jsis the ChatKit widget loaded in the LWC. - Named Credential:
openAI.namedCredential-meta.xmlsecurely stores API credentials for outbound calls. - Custom Object:
Agent_Settings__cstores configuration such as workflow IDs.
You can automatically create a Salesforce scratch org and deploy all source code using the included script:
-
Clone the repository
-
Authorize your Dev Hub org
sf org login web --set-default-dev-hub --alias MyHub -
Create and deploy to a scratch org
npm run scratch:deployThis will:
- Create a scratch org using the definition in
config/project-scratch-def.json - Deploy all source code
- Assign the required permission set
- Open the org in your browser
- Create a scratch org using the definition in
-
Configure Named Credential
- Update
openAI.namedCredential-meta.xmlwith your OpenAI API credentials (see below).
- Update
-
Configure Custom Setting
- Copy the Workflow ID and version into the Agent Settings custom setting.
- Add Domain Whitelist on OpenAI Profile Security
- Add the scratch org domain to your OpenAI security domain whitelist
- Assign permissions
- Ensure users have access to the custom object and Apex class.
- Add the LWC to a Lightning page
- Drag
agentBuilderLWConto your desired Lightning page.
- Drag
Once deployed, users can interact with the ChatKit widget directly in Salesforce. The widget uses the Apex controller to create secure sessions and communicate with OpenAI.
force-app/main/default/classes/AgentBuilderController.cls: Apex backend logicforce-app/main/default/lwc/agentBuilderLWC/: LWC frontendforce-app/main/default/staticresources/chatkit.js: ChatKit widgetforce-app/main/default/namedCredentials/openAI.namedCredential-meta.xml: API credentialsforce-app/main/default/objects/Agent_Settings__c/: Custom setting for configuration
- Run unit tests with:
npm run test:unit - Lint and format code:
npm run lint npm run prettier
For questions or contributions, please open an issue or pull request.
- Make sure you have an OpenAI Account and have deployed an Agent Builder.
- Deploy an Agent Builder and copy the workflow ID. Fill it in on the Agent Builder custom setting (
Agent_Settings__c). - Generate an OpenAI API key on your OpenAI profile page. Copy the key and fill it in on the Named Credential under password using this format:
Bearer {APIKEY}- Do not use brackets around the API Key.
- "Bearer" must be written exactly with a capital B and a space between Bearer and the APIKEY.
- Add your scratch org or dev org domain as a whitelisted domain on your OpenAI profile page.
- Make sure OpenAI API key is entered on Named Credential
- Make sure Agent Builder custom setting has your Workflow ID and that the workflow is Deployed
- Make sure you have your org whitelisted on openAI or you will get a verification error. It can take 15 minutes for this to propogate
- Make sure the CSP for the openAPI cdn is enabled, or you cannot use the iframe.
- Make sure the Session settings in setup has
Turn on Trusted Mode for third-party static resourcesandUse Lightning Web Security for Lightning web components and Aura components
