Quickstart: Generate text using Agent Platform

This page explains how to use Google Apps Script's Vertex AI advanced service (which connects to the Agent Platform API) to prompt the Gemini 2.5 Flash model to generate text.

To learn more about the Vertex AI advanced service, see the reference documentation.

Objectives

  • Set up your environment.
  • Create an Apps Script project that uses the Vertex AI advanced service to connect to the Agent Platform.
  • Run the script to generate text.

Prerequisites

Set up your environment

This section explains how to configure and set up your environment in the Google Cloud console and Apps Script.

Enable the Agent Platform API in your Cloud project

  1. In the Google Cloud console, open your Google Cloud project and enable the Agent Platform API (formerly Vertex AI API):

    Enable the API

  2. Confirm that you're enabling the API in the correct Cloud project, then click Next.

  3. Confirm that you're enabling the correct API, then click Enable.

Create and set up your Apps Script project

To create and set up your Apps Script project, complete the following steps:

  1. Go to script.google.com.
  2. Click New project to create an Apps Script project.
  3. At the top left, click Untitled project.
  4. Name your script Agent Platform quickstart and click Rename.

Set up the Vertex AI advanced service

To enable the Vertex AI advanced service (which connects to the Agent Platform API) and set up the code, do the following:

  1. In the script editor, go to Services and click Add a service The icon to add a service.
  2. In the drop-down menu, select Vertex AI API and click Add.
  3. Open the Code.gs file and replace its contents with the following code:

    /**
     * Main entry point to test the Agent Platform integration.
     */
    function main() {
      const prompt = 'What is Apps Script in one sentence?';
    
      try {
        const response = callVertexAI(prompt);
        console.log(`Response: ${response}`);
      } catch (error) {
        console.error(`Failed to call Agent Platform: ${error.message}`);
      }
    }
    
    /**
     * Calls the Gemini model on Agent Platform.
     *
     * @param {string} prompt - The user's input prompt.
     * @return {string} The text generated by the model.
     */
    function callVertexAI(prompt) {
      // Configuration
      const projectId = 'GOOGLE_CLOUD_PROJECT_ID';
      const region = 'us-central1';
      const modelName = 'gemini-2.5-flash';
    
      const model = `projects/${projectId}/locations/${region}/publishers/google/models/${modelName}`;
    
      const payload = {
        contents: [{
          role: 'user',
          parts: [{
            text: prompt
          }]
        }],
        generationConfig: {
          temperature: 0.1,
          maxOutputTokens: 2048
        }
      };
    
      // Execute the request using the Vertex AI Advanced Service (which wraps the Agent Platform API)
      const response = VertexAI.Endpoints.generateContent(payload, model);
    
      // Use optional chaining for safe property access
      return response?.candidates?.[0]?.content?.parts?.[0]?.text || 'No response generated.';
    }
    

    Replace GOOGLE_CLOUD_PROJECT_ID with the project ID of your Cloud project.

  4. Click Save The icon to save the project.

Test the script

  1. In the script editor, click Run to run the main function.
  2. If prompted, authorize the script.
  3. Click Execution log to view the response.

The advanced service calls the Gemini model on Agent Platform and returns a response to the prompt, What is Apps Script in one sentence?.

AI-generated text from Apps Script's Vertex AI advanced service.
The Agent Platform response in the Apps Script execution log.

For example, the execution log returns a response such as the following:

Response: Google Apps Script is a cloud-based, JavaScript platform that lets you
automate, integrate, and extend Google Workspace applications like Sheets, Docs,
and Gmail.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, we recommend that you delete the Cloud project.

  1. In the Google Cloud console, go to the Manage resources page. Click Menu > IAM & Admin > Manage Resources.

    Go to Resource Manager

  2. In the project list, select the project you want to delete and then click Delete .
  3. In the dialog, type the project ID and then click Shut down to delete the project.

To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, we recommend that you delete the Cloud project.