The Agent Platform SDK for Node.js lets you use the Gemini API to build AI-powered features and applications. Both TypeScript and JavaScript are supported.
For the latest list of available Gemini models on Agent Platform, see the Model information page in Agent Platform documentation.
-
Make sure your node.js version is 22 or above.
-
Create local authentication credentials for your user account:
gcloud auth application-default login
A list of accepted authentication options are listed in GoogleAuthOptions interface of google-auth-library-node.js GitHub repo.
Install the Agent Platform SDK for Node.js by running the following command:
npm install @google-cloud/agentplatformFirst, import the Client class:
import { Client } from '@google-cloud/agentplatform';Then instantiate a client:
const client: Client = new Client({
project: 'my-cloud-project',
location: 'my-cloud-location',
});First define your prompt as a JS object or Prompt object. Then call createVersion.
const prompt = {
promptData: {
contents: [{ parts: [{ text: 'Hello, {name}! How are you?' }] }],
systemInstruction: { parts: [{ text: 'Please answer in a short sentence.' }] },
variables: [
{ name: { text: 'Alice' } },
],
model: 'gemini-2.5-flash',
},
};
const promptResource = await client.prompts.createVersion({
prompt: prompt,
});Note that you can also use typed objects to define your prompt. Some of the types used to do this (Content, Part) are from the Gen AI SDK (@google/genai).
import { Prompt } from '@google-cloud/agentplatform';
import { Part, Content } from '@google/genai';
const prompt: Prompt = {
promptData: {
contents: [{ parts: [{ text: 'Hello, {name}! How are you?' } as Part] } as Content],
systemInstruction: { parts: [{ text: 'Please answer in a short sentence.' } as Part] } as Content,
variables: [
{ name: { text: 'Alice' } as Part },
],
model: 'gemini-2.5-flash',
},
};
const promptResource = await client.prompts.createVersion({
prompt: prompt,
});Retrieve a prompt by calling get() with the promptId.
const dataset = (promptResource as any)._dataset;
const promptId = dataset?.name?.split('/').pop() || 'YOUR_PROMPT_ID';
const retrievedPrompt = await client.prompts.get({
promptId: promptId,
});After creating or retrieving a prompt, you can call models.generateContent() with that prompt using the Gen AI SDK (@google/genai).
import { GoogleGenAI } from '@google/genai';
// Create a Client in the Gen AI SDK
const genaiClient = new GoogleGenAI({ vertexai: true, project: 'your-project', location: 'your-location' });
// Call generateContent() with the prompt
if (retrievedPrompt.promptData?.contents) {
const response = await genaiClient.models.generateContent({
model: retrievedPrompt.promptData.model || 'gemini-2.5-flash',
contents: retrievedPrompt.promptData.contents,
config: {
systemInstruction: retrievedPrompt.promptData.systemInstruction,
...(retrievedPrompt.promptData.generationConfig || {}),
},
});
console.log(response.text);
}The contents of this repository are licensed under the Apache License, version 2.0.