VertexAI has not be defined Error-

Hi,

New here, I am not sure if this is the right area, please advise if it is not.

I am using Google Apps Scripts and working on a Google Project that requires VertexAI. The project runs but returns the error VertexAI is not defined. Under Services on the left column I can’t specify VertexAI as a reference. Anyone experience this and could advise? Thanks in advance.

I think accessing vertex AI via Google App Script is via REST API . I am not sure if there is any offcial library or module defined. Folks can correct me if I am wrong

REST API :

function generateVertexAIContent() {
const PROJECT_ID = "YOUR_GCP_PROJECT_ID";
const LOCATION = "us-central1"; // e.g., us-central1
const MODEL = "gemini-1.5-flash"; // or your desired model

const url = https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL}:generateContent;

const payload = {
contents: [
{
role: "user",
parts: [{ text: "Explain quantum computing in one sentence." }]
}
]
};

const options = {
method: "post",
contentType: "application/json",
headers: {
Authorization: Bearer ${ScriptApp.getOAuthToken()}
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
};

const response = UrlFetchApp.fetch(url, options);
const json = JSON.parse(response.getContentText());

Logger.log(JSON.stringify(json, null, 2));
}