Open In App

How to Access and Use Google Gemini API Key (with Examples)

Last Updated : 18 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In today's world artificial intelligence (AI) is changing the way technology is used. The Google Gemini API is a widely used tool that can help create smart applications like chatbots, content generators and many more. In this guide we will help you to how to access Google Gemini API key, making it easy to make AI projects.

How to Get Your Google Gemini API Key

Step 1: Open Google AI Studio

  • Visit Google AI Studio in your browser.
  • Log in using your Google Account credentials.
google_ai_studio
Visit the home page

Step 2: Click on "Get API Key"

  • On the top-right corner, click on “Get API Key”.
google_ai_studio

Step 3: Click on "Create API Key"

  • After clicking "Get API Key" you'll see a button "Create API Key".
  • Click on "Create API Key" to proceed.
create_api_key
Create API Key

Step 4: Select Google Cloud Project

  • Select an existing Google Cloud project or create a new one when prompted.
p
Choose a Project

Step 5: Generate and Copy API Key

  • Once you select or create the project, your Gemini API Key will be generated.
  • Copy the API key and store it securely for your usage.
f
Copy the API

How to Use the Gemini API Key in Python

Now as we have our Gemini API Key lets see how we can use it.

Step 1: Install the Gemini Python SDK

Python
pip install google-generativeai

Step 2: Set up the API Key and Initialize Gemini

Here we need to mention our API Key.

Python
import google.generativeai as genai

# Configure API key
genai.configure(api_key='your_api_key_here')

Step 3: Create a Generative Model Instance

Here we are using gemini 1.5 flash model.

Python
# Create GenerativeModel instance
model = genai.GenerativeModel('gemini-1.5-flash')

Step 4: Taking output from Gemini API

Python
response = model.generate_content("What is python?")
print(response.text)

Output:

Output-gemini-API
output from Gemini API

Lets make a real life project using this API Key.

Building a Simple Chatbot with Tkinter

Here:


The code starts by setting up the tkinter window with root = tk.Tk(), creating the main application window.

  • The Generative AI model is configured using genai.configure(api_key='your//key') and an instance of the model is created with model = genai.GenerativeModel('gemini-1.5-flash').
  • chat_with_bot(prompt) is defined to send a user’s input to the chatbot model and receive a response.
  • The user types a message in user_entry and clicks the send_button.
  • The send_message() function is triggered.
  • It checks if the input is 'exit'. If true, it exits the app (root.quit()).
  • Otherwise, it displays the user's message in chat_window.
  • The chatbot generates a response which is then displayed in the chat_window.
  • The chat_window is updated to show both the user’s input and the bot’s response.
  • It ensures that the chat window remains scrollable and the latest message is always visible using chat_window.see(tk.END).
  • The tkinter main event loop root.mainloop() is executed, keeping the application running and responsive to user input until the window is closed.
Python
import tkinter as tk
from tkinter import scrolledtext
import google.generativeai as genai

# Configure API key
genai.configure(api_key='your//key)

# Create GenerativeModel instance
model = genai.GenerativeModel('gemini-1.5-flash')

# Function to interact with the chatbot
def chat_with_bot(prompt):
    response = model.generate_content(prompt)
    return response.text

# Function to handle sending a message
def send_message():
    user_input = user_entry.get()
    if user_input.lower() == 'exit':
        root.quit()
    else:
        chat_window.config(state=tk.NORMAL)
        chat_window.insert(tk.END, "You: " + user_input + "\n")
        chat_window.config(state=tk.DISABLED)
        user_entry.delete(0, tk.END)
        
        bot_response = chat_with_bot(user_input)
        chat_window.config(state=tk.NORMAL)
        chat_window.insert(tk.END, "Bot: " + bot_response + "\n")
        chat_window.config(state=tk.DISABLED)
        chat_window.see(tk.END)

# Create main application window
root = tk.Tk()
root.title("Chatbot")

# Create chat display window
chat_window = scrolledtext.ScrolledText(root, state='disabled', wrap=tk.WORD)
chat_window.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

# Create user input field
user_entry = tk.Entry(root, width=80)
user_entry.pack(padx=10, pady=10, side=tk.LEFT, expand=True, fill=tk.X)

# Create send button
send_button = tk.Button(root, text="Send", command=send_message)
send_button.pack(padx=10, pady=10, side=tk.RIGHT)

# Run the application
root.mainloop()

Output:

Accessing and using the Google Gemini API key can significantly enhance AI-driven applications. By following the steps outlined above. Whether building chatbots, content generators or other innovative solutions Google Gemini API provides tools for their application.


Next Article
Practice Tags :

Similar Reads