API Data Source

Updated on February 24, 2024
Updated on February 24, 2024

The API Data Source lets you programmatically bring external Web and API data that GPTfy sends along with your Salesforce data for AI Processing.

We have designed this so you can add your custom connector Apex class for any API Data source and its related integration, authentication, etc.

In this walkthrough, we will look at a sample connector class that GPTfy uses to send the value of the ‘Website’ field to Explorium.

Explorium retrieves relevant information and Firmographic data for the ‘website’ and returns it to GPTfy.

GPTfy then sends this API-enriched data and additional Salesforce data for AI processing and presents the results.

Process Flow

The process flow for setting up API Data Sources typically involves the following steps:

Let’s review the steps to connect with an API Data Source, using Explorium as an example.

  • The starting point of this guide is the Cockpit.

  • From there, navigate to the ‘Manage Configuration’ section and select the “API Data Source.”
  • Select one of the pre-defined API Data Sources, or Create your own.

All within your familiar Salesforce workspace!

To continue further, you will need the following:

  • Named Credential: Create an external credential and assign a permission set. Don’t forget to select external credential principal access.

  • Connector Class: This is essential for calling the API and authenticating the connection.

  • Record with ‘Website’ Field: Have a record of any object containing the ‘website’ field value.

Now, let’s connect to the API Data Source (Explorium) by following these steps:

Step 1: Add the Named Credential

  • Open your GPTfy dashboard.

  • Navigate to the Named Credential section.

  • Add a new Named Credential named “Explorium.”

  • Select Explorium as the data source.

  • Specify the Class Name (e.g. SampleDataSourceClass). You’ll customize this class based on your specific API data source.

Step 2: Save and Validate

  • Save the changes after adding the connector class.

  • Refresh the page to ensure all settings are updated.

Step 3: Activate API Data Source

  • Click the Activate button to activate the API Data Source (Explorium).

Step 4: Create/Update Context Mapping

  • In this example, we will create a Data Context Mapping for the ‘Account’, and add the activated API Data Source ‘Explorium’.

Step 5: Create Your Prompt

  • Now, create a prompt that utilizes the data context mapping you’ve set up.

  • For this example, we will create a custom prompt so users can ask any question related to this Account.

  • When this prompt is run, GPTfy will get Account information from Explorium, send it to AI along with the user’s question/prompt, and display the answer.

Step 6: Run GPTfy on a Sample record to test the Prompt and API Data Source Integration.

  • Navigate to an Account record and update its website field with a sample website value.

  • Explorium will retrieve website data using the data source, send it to the AI, and then receive the response.

Note: Ensure this is a legitimate business website to see the quality of data returned.

Here are the details of what is happening under the hood

  • The user asks a question for an Account:
    In this example, the Account record’s website field has a value of “www.nyiax.com.”
  • Explorium Fetches Website Data:
    Explorium retrieves information from the specified website when you trigger a request using your configured API Data Source.

  • GPTfy passes Explorium Data to AI: The extracted website description is then passed to the AI along with other Salesforce data (via GPTfy).

Your users can add specific questions or instructions for AI using the custom prompt.

Typical use cases are whitespace analysis, Summarization, Insights, or any other Gen AI capability usage.

  • GPTfy Presents the AI Response to the user (or updates field, etc.):
    User is provided with the response. This can also have an automation that populates record fields, responds to a Flow, or performs other actions.

Sample Connector Class

Note: The sample code here is provided ‘as-is’ for reference and informational purposes only.

You can refer to this to understand how a similar connection can be made with other API Data Sources.

Code

				
					global class SampleDataSourceClass implements ccai.AIDataSourceInterface{
    
    public String getExternalData(ccai__AI_Data_Source__c dataSource, String extractedData){
        Map<String, Object> mainMap = (Map<String, Object>)JSON.deserializeUntyped(extractedData);
        if(mainMap.containsKey(‘Website’) && mainMap.get(‘Website’) != null){ //getting the website from the account extracted data.
             String apiData = SampleDataSourceClass.connectToExplorium(String.valueOf(mainMap.get(‘Website’)), dataSource); //calling the explorium to get the information using the website.
            if(String.isNotBlank(apiData)){
                List<Object> apiDataList = (List<Object>)JSON.deserializeUntyped(apiData); //parsing the data returned from Explorium.
                if(apiDataList != null && !apiDataList.isEmpty()){
                    Map<String, Object> apiDataMap = (Map<String,Object>)apiDataList.get(0);
                    for(String key : apiDataMap.keySet()){
                       mainMap.put(key, String.valueOf(apiDataMap.get(key))); //adding the description returned from Explorium to AI for processing.
                     }
                }
            }
        }
        return JSON.serialize(mainMap);
    }
    
    public static String connectToExplorium(String sourceUrl , ccai__AI_Data_Source__c dataSource){
        HttpRequest request = new HttpRequest();
        request.setEndpoint(‘callout:’+ dataSource.ccai__Named_Credential__c); //create a named credential for the external system you are calling
        request.setMethod(‘POST’);

        request.setHeader(‘accept’, ‘application/json’);
        request.setHeader(‘content-type’, ‘application/json’);
        
        List<requestBody> lstObj = new List<requestBody>();
        lstObj.add(new requestBody(sourceUrl)); //passing the website URL
        String body = JSON.serialize(lstObj);
        
        request.setBody(body);

        Http http = new Http();
        HttpResponse response = http.send(request);
        System.debug(‘body: ‘+response.getBody());
        System.debug(‘status: ‘+response.getStatus());
        System.debug(‘statusCode: ‘+response.getStatusCode());
        return response.getBody();
    }
    
    public class requestBody{
        public String url {get; set;}
        public requestBody(String url){
            this.url = url;
        }
    }
}