[Video](https://www.youtube.com/watch?v=SlJGPx_bRC4)

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](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versions#latest-stable)
to generate text.

To learn more about the Vertex AI advanced service, see the
[reference documentation](https://developers.google.com/apps-script/advanced/vertex-ai).

## 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

- A Google Cloud project with billing enabled. To check that an existing project has billing enabled, see [Verify the billing status of your
  projects](https://cloud.google.com/billing/docs/how-to/verify-billing-enabled). To create a project and set up billing, see [Create a
  Google Cloud project](https://developers.google.com/workspace/guides/create-project).

## 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](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com)
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](https://script.google.com/home).
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](https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/add/default/24px.svg).
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](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects)
   of your Cloud project.
4. Click **Save**
   ![The icon to save the project](https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/save/default/24px.svg).

## 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.](https://developers.google.com/static/apps-script/images/vertex-ai-service-response.png) 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.

> [!CAUTION]
> **Caution:** Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for this tutorial, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as a URL on appspot.com, delete the selected resources inside the project instead of deleting the whole project.
>
> If you plan to explore multiple tutorials and quickstarts, reusing projects can help you avoid exceeding project quota limits.

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


   [Go to Resource Manager](https://console.cloud.google.com/cloud-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.

## Related topics

- [Vertex AI advanced service documentation](https://developers.google.com/apps-script/advanced/vertex-ai)
- [Agent Platform documentation](https://developers.google.com/gemini-enterprise-agent-platform/overview)
- View the [Google Workspace AI samples gallery](https://developers.google.com/workspace/guides/ai-overview)