Being-Creative-with-AI/C3/Creating-and-using-Gemini-API-Key/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Title Slide Welcome to this Spoken Tutorial on Creating and using a Gemini API key
Learning Objectives Slide In this tutorial, we will learn to -
  • Use Google AI Studio
  • Create a Gemini API key
  • Store the key securely
  • Use the key in a simple Python example
Disclaimer Content Slide As Al tools constantly evolve, if you are unable to locate any icon or encounter difficulty at any step, you may use any conversational Al Chatbot for guidance.
System Requirement Slide To record this tutorial, I am using:
  • A laptop with internet connection
  • A web browser such as Chrome
  • A Google account
  • Google Colab for running Python code
Pre-requisite Slide Basic computer knowledge is enough to follow this tutorial.

No programming experience is required.

Pre-requisite

For the Pre-requisites of this tutorial, visit the website shown on your screen

http://EduPyramids.org

For the Pre-requisites of this tutorial, visit the website shown on your screen.
Code Files

The following code file is required to practice this tutorial Gemini-API-key.txt

This file is provided in the code files link of this tutorial page.

Please download and extract the file

The following code file is required to practice this tutorial.

This file is provided in the Code Files link of this tutorial page.

Please download and extract the file.

Show slide: Why Gemini API? The Gemini API helps us use Google’s AI models in applications.

We will use Google AI Studio to create an API key.

Show browser address bar Open any web browser and log into your Google account. Now in the address bar type, aistudio dot google dot com.
First-time login screen Accept the terms and click Continue if prompted.
Highlight “Get API key” Click on Get API key in the bottom left corner.
Highlight “Create API key” On the top right, click on Create API key.
Rename field A pop window named Create a new key opens.

In the Name your key field, I will type Sample API

Note that a default name is already provided in this field.

Project selection Next field is Choose an imported project.

Click on the dropdown and select plus Create project.

Project field Another pop up window named Create a new project opens.

In Name your project field, I will again type Sample API

Click on Create project.

Click Create Key Again a pop up window opens. Create Key.
Show generated key


Click Copy key

Now the API key is generated.

In the pop up window you can see the details.

Click on the Copy key tab towards the bottom right corner.

Doing so will copy the API key safely.

Close the pop up window by clicking on the cross mark.

Show the copy icon You can also copy the API key by clicking on the copy icon here.
Open Google Colab In a new browser tab, type colab dot research dot google dot com
Click + New Notebook Click on plus New notebook button on the left corner.
Click Key icon

Add new secret

Click on the Key icon on the left panel.

Here, click on plus Add new secret.

Enter Name and Value I’m entering the Name as Spoken.

Paste the API key in the Value field.

Click and drag the Notebook Access option to use the key.

The key is now stored securely.

Point to the key Do not share your API key publicly. Others can misuse your data.

Let us close this Secrets window now.

Show slide: Using the API key Next, let us use this API key to create an AI application.
Show code

!pip install -q -U google-generativeai

First, let us install the library.

In a new code cell type:

exclamation pip space install space hyphen q space hyphen capital U space google hyphen generative ai

Press shift and enter keys to run it.

Highlight import statements

import google.generativeai as genai

from google.colab import userdata

Now we need to import the required modules.

Now pause the video and type this command in a new code cell.

This command should be in 2 lines as shown here.

Always, Make sure the cursor is on the same line at the end of the command.

Press shift and enter.

Show code

GOOGLE_API_KEY = userdata.get('Spoken') genai.configure(api_key=GOOGLE_API_KEY)


Next we need to retrieve the API key from secrets using this code.

Now pause the video and type this command in a new code cell.

This command should be in 2 lines as shown here.

Remember Spoken is the name given for the api key stored in the secret manager

Press shift and enter.

Create model

model = genai.GenerativeModel('gemini-2.5-flash')

Now we will create a Gemini model with this code.

Type model equal to genai dot GenerativeModel open parenthesis single quote gemini-2.5-flash close single quote parenthesis

Press shift and enter.

Generate content

response = model.generate_content("Write a slogan for a smart coffee mug")

Let us now generate content using the model with this code.

Now pause the video and type this command in a new code cell.

Press shift and enter.

print(response.text)

We will now print the response with this print open parenthesis response dot text close parenthesis

Press shift and enter.

The model generates creative slogans.

Summary Slide Let us summarise. We learnt the following.
  • Created a Gemini API key
  • Stored it securely in Colab
  • Used it in a Python example
Assignment Slide
  • Create your own API key
  • Open Google Colab and store the key
  • Create a Gemini model as mentioned in the tutorial
  • Modify the prompt to create 5-questions for a science quiz.
  • Run the code and observe the output.
Here is an assignment for you.
Acknowledgement slide Thank you for joining.
This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited, SINE, IIT Bombay

Contributors and Content Editors

Madhulika, Misbah