Getting Started with Gemini AI API via Google Cloud Code Application Templates

Romin Irani
Google Cloud - Community
5 min readDec 20, 2023

The Gemini Model is available to all developers and I wanted to share with you one way via which you can test out the API via an Application Template right within your favourite editor : VS Code.

The Pre-requisites for this tutorial include:

  1. You have a Google Cloud Account and a Google Cloud Project + billing associated with it.
  2. You are familiar with Visual Studio Code.

New Gemini Application Template in Cloud Code

Visit the Google Cloud console and login with your account. Select your project and launch Cloud Shell and then the Editor. You should be at the following point, as shown below:

If you have any other Folder/Application open in Cloud Shell IDE, do close it down.

Next step, is to launch the VS Code command palette as shown below:

In the Command Palette window, type out:

Cloud Code: New Application

This will bring up a list of Application Templates, go for gemini as shown below:

Two sample application templates are available ( Node.js and Python). Pick your choice from the list below:

I will go with Gemini API Python. This will bring up the folder path, go with one that you have on your environment. This will then go ahead and download the template and the files for you. My file list is shown below and you should have a similar list of files:

The README.md file is informative and gives you enough instructions to get the example running.

Get the Gemini AI API Key

Before we delve into running the application, do keep in mind that these application templates are designed to work with the Gemini AI API that you get to use in Google AI Studio. Go ahead and visit Google AI Studio (formerly known as MakerSuite) to get an API Key.

I created an API Key in my existing Google Cloud Project. It creates a long API Key string that you should save securely somewhere.

Run the Gemini Application

Now that we have the API Key, we need to follow the steps given below to run the Python application.

Launch a Terminal from the Cloud Shell IDE and follow along (assuming you are in the terminal and the folder in which the main.py , requirements.txt files are present.

First up set the following environment variable since that is expected by main.py application. This is the Application Key that you just created. Use the value that you safely stored somewhere.

export GOOGLE_API_KEY="Your Gemini API Key"

Next step is to install the Python dependencies. A good practice will be to create your own Python virtual environment but I leave that to you. Just ensure that we install the required package google-generativeai that is there in the requirements.txt file.

pip install -r requirements.txt

Great, now all we need to do is run the application:

python main.py

The default prompt in the main.py is the following one:

How do I bake a cake?

Feel free to modify that before running the main.py file.

A sample run is shown below:

$ python main.py 

models/gemini-pro
models/gemini-pro-vision
1. **Gather the ingredients.**

* 1 cup (2 sticks) unsalted butter, softened
* 1 3/4 cups granulated sugar
* 3 large eggs
* 2 1/4 cups all-purpose flour
* 3 1/2 teaspoons baking powder
* 1 teaspoon salt
* 1 cup milk
* 1 teaspoon vanilla extract

2. **Preheat the oven.**

* Preheat the oven to 350 degrees F (175 degrees C).

3. **Grease and flour the baking pans.**

* Grease and flour two 9-inch round baking pans.

4. **Cream the butter and sugar together.**

* In a large bowl, cream the butter and sugar together until light and fluffy.

5. **Beat in the eggs one at a time.**

* Beat in the eggs one at a time, until well blended.

6. **Sift together the flour, baking powder, and salt.**

* In a separate bowl, sift together the flour, baking powder, and salt.

7. **Add the dry ingredients to the wet ingredients alternately with the milk.**

* Add the dry ingredients to the wet ingredients alternately with the milk, beginning and ending with the dry ingredients.

8. **Stir in the vanilla extract.**

* Stir in the vanilla extract.

9. **Pour the batter into the prepared baking pans.**

* Pour the batter into the prepared baking pans.

10. **Bake for 30-35 minutes, or until a toothpick inserted into the center comes out clean.**

* Bake for 30-35 minutes, or until a toothpick inserted into the center comes out clean.

11. **Let the cakes cool for 10 minutes in the pans, then turn them out onto wire racks to cool completely.**

* Let the cakes cool for 10 minutes in the pans, then turn them out onto wire racks to cool completely.

12. **Frost the cakes.**

* Frost the cakes with your favorite frosting.

13. **Serve.**

* Serve the cakes immediately or store them in an airtight container at room temperature for up to 3 days.

Hope this helps you try Gemini AI API and let me know what you build.

--

--