This quickstart shows you how to install the Google Gen AI SDK for your language of choice and then make your first API request. It covers the following topics:
generateContent
method to send a text prompt and receive a response.Choose your authentication method:
Before you begin ConceptsConfigure application default credentials if you haven't yet.
The following diagram summarizes the overall workflow:
Install the SDK and set up your environmentOn your local machine, click one of the following tabs to install the SDK for your programming language.
Gen AI SDK for PythonInstall and update the Gen AI SDK for Python by running this command.
pip install --upgrade google-genai
Set environment variables:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=globalGen AI SDK for Go
export GOOGLE_GENAI_USE_VERTEXAI=True
Install and update the Gen AI SDK for Go by running this command.
go get google.golang.org/genai
Set environment variables:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=globalGen AI SDK for Node.js
export GOOGLE_GENAI_USE_VERTEXAI=True
Install and update the Gen AI SDK for Node.js by running this command.
npm install @google/genai
Set environment variables:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=globalGen AI SDK for Java
export GOOGLE_GENAI_USE_VERTEXAI=True
To install the Gen AI SDK for Java, add the following dependency to your Maven pom.xml
file:
<dependencies> <dependency> <groupId>com.google.genai</groupId> <artifactId>google-genai</artifactId> <version>0.7.0</version> </dependency> </dependencies>
Set environment variables:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=globalREST
export GOOGLE_GENAI_USE_VERTEXAI=True
Set environment variables:
GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT GOOGLE_CLOUD_LOCATION=global API_ENDPOINT=YOUR_API_ENDPOINT MODEL_ID="gemini-2.5-flash" GENERATE_CONTENT_API="generateContent"Make your first request
Use the generateContent
method to send a request to the Gemini API in Vertex AI:
To send this prompt request, run the curl command from the command line or include the REST call in your application.
curl \ -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://${API_ENDPOINT}/v1/projects/${GOOGLE_CLOUD_PROJECT}/locations/${GOOGLE_CLOUD_LOCATION}/publishers/google/models/${MODEL_ID}:${GENERATE_CONTENT_API}" -d \ $'{ "contents": { "role": "user", "parts": { "text": "Explain how AI works in a few words" } } }'
The model returns a response. Note that the response is generated in sections with each section separately evaluated for safety.
Generate imagesGemini can generate and process images conversationally. You can prompt Gemini with text, images, or a combination of both to achieve various image-related tasks, such as image generation and editing. The following code demonstrates how to generate an image based on a descriptive prompt:
You must include responseModalities: ["TEXT", "IMAGE"]
in your configuration. Image-only output is not supported with these models.
Gemini can understand images as well. The following code uses the image generated in the previous section and uses a different model to infer information about the image:
Code executionThe Gemini API in Vertex AI code execution feature enables the model to generate and run Python code and learn iteratively from the results until it arrives at a final output. Vertex AI provides code execution as a tool, similar to function calling. You can use this code execution capability to build applications that benefit from code-based reasoning and that produce text output. For example:
For more examples of code execution, check out the code execution documentation.
What's nextNow that you made your first API request, you might want to explore the following guides that show how to set up more advanced Vertex AI features for production code:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4