Quickstart: Generate text using Vertex AI

This page explains how to use Apps Script's Vertex AI advanced service 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.
  • 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 Vertex AI API in your Cloud project

  1. In the Google Cloud console, open your Google Cloud project and enable the 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 projectto create an Apps Script project.
  3. At the top left, click Untitled project.
  4. Name your script Vertex AI quickstartand click Rename.

Set up the Vertex AI advanced service

To enable the Vertex AI advanced service and set up the code, do the following:

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

      /** 
     * Main entry point to test the Vertex AI 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 Vertex AI: 
     ${ 
     error 
     . 
     message 
     } 
     ` 
     ); 
      
     } 
     } 
     /** 
     * Calls the Vertex AI Gemini model. 
     * 
     * @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 
      
     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 SaveSave icon.

Test the script

  1. In the script editor, click Runto run the main function.
  2. If prompted, authorize the script.
  3. Click Execution logto view the response from Vertex AI.

The Vertex AI service 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 Vertex AI service 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.

Create a Mobile Website
View Site in Mobile | Classic
Share by: