How to Access and Use Google Gemini API Key (with Examples)
Last Updated :
18 Jun, 2025
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.
Visit the home pageStep 2: Click on "Get API Key"
- On the top-right corner, click on “Get API Key”.
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 KeyStep 4: Select Google Cloud Project
- Select an existing Google Cloud project or create a new one when prompted.
Choose a ProjectStep 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.
Copy the APIHow 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 from Gemini APILets 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.
Similar Reads
How To Use an API? The Complete Guide APIs (Application Programming Interfaces) are essential tools in modern software development, enabling applications to communicate with each other. Whether you're building a web app, mobile app, or any other software that needs to interact with external services, understanding how to use an API is c
4 min read
What is Google Sheets API and How to Use it? We all are familiar with spreadsheets and worked with them since we first learned about computers. We are used to arranging our data in a tabular manner in the form of rows and columns. When we are working on a project and wish to save our data in a tabular form, we think of relational databases. In
8 min read
What Is Google Gemini AI? How to Use the New Chatbot Model Big news came in the world of artificial intelligence as Google unveiled Gemini, a powerful AI model family set to rival OpenAI's GPT-4. According to Google, Gemini's standout feature is its ability to outperform nearly all other models, showcasing superiority across 30 out of 32 widely-used benchma
6 min read
Generating API Keys For Using Any Google APIs Like most software giants, Google provides its enthusiastic developers community with its APIs, SDKs and Services. These APIs from Google are hosted on their cloud platform, popularly known as Google Cloud Platform (GCP). Software such as Google Maps, YouTube, Gmail, etc., use the same APIs and now
3 min read
How To Access Google Docs From A Non Gmail Account Google Docs is a platform where you can log in when you have a Google Gmail account, but you can also log in with a non-Gmail account into Google Docs. To access Google Docs with your non-Gmail account, you must make a Google account first with your existing Gmail. In this article, we will explore h
4 min read