Invoke Google Gemini API from Salesforce Agentforce

Invoke Google Gemini API from Salesforce Agentforce

In this Blog Post, I have used Agentforce Apex Agent Action to invoke the Google Gemini API for general searches.

Sample Apex Class:

public class GeminiAPIController {
    
    @InvocableMethod(
        label = 'Invoke Gemini API' 
        description = 'Executing Goolge Gemini API for general search'
    )
    public static List < String > invokePrompt( List < String > listMessages ) {
        
        String strMessage = listMessages.get( 0 );
        // Setting default value to return
        List < String > listResponses = new List < String > { 'Some issue with the API. Please try again later.' };
        // End Point of the API
        String strEndpoint = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=<YOUR API KEY>';
        
        HTTP h = new HTTP();
        HTTPRequest req = new HTTPRequest();
        req.setEndPoint( 
            strEndpoint 
        );
        req.setMethod( 
            'POST' 
        );
        req.setHeader( 
            'Content-Type', 
            'application/json' 
        );       
        req.setBody( 
            '{ "contents": [ { "parts":[ {"text": "' + 
            strMessage + '" } ] } ] }'
        );
        HTTPResponse response = h.send(
            req
        );
        System.debug( 
            'Response is ' + 
            response.getBody() 
        );
        
        if ( response.getStatusCode() == 200 ) {
            
            GeminiAPIWrapper objWrap = ( GeminiAPIWrapper )System.JSON.deserialize( 
                response.getBody(), 
                GeminiAPIWrapper.class 
            );
            
            if ( objWrap.candidates.size() > 0 ) {
                
                for ( GeminiAPICandidateWrapper objCaWrap : objWrap.candidates ) {
                    
                    GeminiAPIContentWrapper objCoWrap = objCaWrap.content;
                    
                    if ( objCoWrap != null ) {
                        
                        if ( objCoWrap.parts.size() > 0 ) {
                            
                            for ( GeminiAPIPartWrapper objPWrap : objCoWrap.parts ) {
                                
                                System.debug( 'Text: ' + objPWrap.text );
                                // Retrieving the response for the Prompt
                                listResponses = new List < String > { objPWrap.text };
                                    
                            }
                            
                        }
                        
                    }
                    
                }
                
            }
            
        } else {
             listResponses = new List < String > { response.getBody() };
        }
        
        return listResponses;
        
    }
    
    // Wrapper Classes to handle the API Response
    public class GeminiAPIWrapper {
        List < GeminiAPICandidateWrapper > candidates;
    }
    
    public class GeminiAPICandidateWrapper {        
        GeminiAPIContentWrapper content;
    }
    
    public class GeminiAPIContentWrapper {        
        List < GeminiAPIPartWrapper > parts;
    }
    
    public class GeminiAPIPartWrapper {
        String text;
    }
    
}

Agentforce Agent Topic:

Agentforce Apex Agent Action:

Agentforce Agent Topics Configuration:

Please add the Gemini API Endpoint URL in the Salesforce Remote Site Settings so that the HTTP Request to it will succeed. Make sure the Remote Site Setting is Active.

Output:

Leave a Reply