How to Integrate ChatGPT with Salesforce

May 12, 2025


 

 

Introduction:

Today, businesses want to work faster and give customers more personal, helpful experiences. Salesforce is a popular tool that helps companies manage customer data and run their day-to-day work. But what if you could make Salesforce even smarter?

That’s where ChatGPT comes in — an AI tool from OpenAI that can talk like a human, write emails, answer questions, and more. By connecting ChatGPT to Salesforce, you can save time, automate tasks, and make your system more intelligent.

In this blog, we’ll show you how to connect Salesforce with ChatGPT using the OpenAI API and Apex.


How to Generate ChatGPT API Key:

  1. Go to the OpenAI Website
  2. Open Your Profile
    • Click your profile icon in the top-right corner, then select Your Profile.
  3. Find the API Keys Section
    • On the left sidebar, click API keys under the Organization section.
  4. Create a New API Key
    • Click the Create new secret key button.
  5. Enter Key Details
    • Give your key a name (e.g., Salesforce GPT Integration)
    • Select the project you want the key to be used for
  6. Copy the Key
    After the key is generated, copy it immediately. You won’t be able to see it again.

 

Create a Custom Label in Salesforce

Now that you have your ChatGPT API key, let’s store it securely in Salesforce using a Custom Label. This allows you to manage the key without hardcoding it in Apex.

  1. Open Setup in Salesforce
    • Click the gear icon in the top-right corner and choose Setup.
  2. Search for Custom Labels
    • In the Quick Find box (top left), type Custom Labels and select it from the results.
  3. Create a New Label
    • Click the New Custom Label button.
  4. Enter Label Details
    • Short Description: ChatGPT Key
    • Value: Paste your ChatGPT API key
  5. Save the label.

 

Create Remote site setting in salesforce

  1. Open Setup in Salesforce
    • Click the gear icon in the top-right corner and choose Setup.
  2. Search for Remote Site Settings
    • In the Quick Find box (top left), type Remote Site Settings and select it from the results.
  3. Create a New Remote Site
    • Click the New Remote Site button.
  4. Enter Remote Site Setting Details
    • Remote Site Name: ChatGPT
    • Remote Site URL: https://api.openai.com
  5.  Save the Remote Site Settings.

 

Create an Apex Class for ChatGPT Integration

let’s create an Apex class that sends a request to ChatGPT and gets a response using the API key stored in your custom label.

For this, Create an apex class called ChatGPTIntegration and paste the below code into it.

ChatGPTIntegration.cls
public class ChatGPTIntegration {
    public static void getResponse(String input) {
        String key = Label.ChatGPT_Key;
        String body = '{"model": "gpt-4.1", "input": "'+ input +'"}';

        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('https://api.openai.com/v1/responses');
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Authorization', 'Bearer '+ key);
        req.setBody(body);

        Http http = new Http();
        HttpResponse res = http.send(req);
        Map<String, Object> response = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
        List<Object> outputs = (List<Object>) response.get('output');
       
        List<Map<String, Object>> outputsMap = new List<Map<String, Object>>();
        for(Object obj : outputs) {
            Map<String, Object> content = (Map<String, Object>) obj;
            outputsMap.add(content);
        }
       
        List<Object> contents = (List<Object>) outputsMap[0].get('content');
       
        List<Map<String, Object>> contentsMap = new List<Map<String, Object>>();
        for(Object obj : contents) {
            Map<String, Object> content = (Map<String, Object>) obj;
            contentsMap.add(content);
        }
       
        String text = (String) contentsMap[0].get('text');
        System.debug('Output => '+text);
    }
}
 
and Save it.
Now to run this code open the Anonymous Window and paste the below code.
String input = 'Write a one-sentence bedtime story about a unicorn.';
ChatGPTIntegration.getResponse(input);

Press the Execute button.