Gemini API Key for Excel: A Step-by-Step Guide to Generate It
This article will walk you through the simple, step-by-step process of getting a Google Gemini API key. This key will allow you to connect your VBA code to the power of Google’s Gemini models, so you can start creating intelligent and dynamic applications right from your spreadsheet.
The process is straightforward and doesn’t require any prior knowledge of APIs or coding. We’ll be using Google AI Studio, a free web-based tool from Google that makes it easy to get started with their generative AI models.
Before You Begin: You’ll need a Google Account to access Google AI Studio. If you don’t have one, you can create one for free.
Step 1: Go to Google AI Studio
Open your web browser and navigate to Google AI Studio.
If you are not already signed in, you will be prompted to sign in with your Google Account.

Step 2: Create a New API Key
To create a new API key, simple click on ‘Create API key’ button.

You will then be given two options:
- “Create API key in a new project”
- “Create API key in an existing project”
For first-time users, we recommend selecting “Create API key in a new project.” This will generate a new Google Cloud project automatically, which is the easiest way to get started.
Step 3: Copy Your API Key
After a moment, your new API key will be generated and displayed on the screen. It’s a long string of characters.
This is the only time you will be able to see the full key. Make sure to copy it and save it in a secure location.
You can click the copy icon next to the key to save it to your clipboard.

Understanding and Choosing Gemini Models
When you use the Gemini API, your code needs to know which specific model to call. This is done by including the model’s name directly in the API request URL, like this:
https://generativelanguage.googleapis.com/v1beta/models/ gemini-2.0-flash-lite:generateContent?key=…
In the URL above, gemini-2.0-flash-lite is the name of the model you are requesting. It’s crucial to use a model name that actually exists and is suited for your task.
You can find the most current and comprehensive list of all available Gemini models on the Google AI for Developers website. This page will provide a complete list of models, their capabilities, and their status (e.g., stable, preview).
For a basic text-based application like the one in your VBA code, you will likely be interested in models designed for text generation. Here is a brief overview of some of the most common models you might encounter:
- gemini-2.5-pro: This is a powerful, general-purpose model with enhanced reasoning and multimodal capabilities. It is best suited for complex tasks, advanced coding, and when you need the highest quality output.
- gemini-2.5-flash: A versatile and fast model, 2.5 Flash is a great choice for a wide variety of tasks. It provides a good balance between performance, speed, and cost-efficiency.
- gemini-2.5-flash-lite: As the name suggests, this is a highly cost-efficient model with low latency. It is an excellent choice for high-volume tasks that require quick responses and for situations where you want to minimize costs.
How to Use Your Gemini API Key for Excel VBA Code
With your API key and model name ready, you can now begin to write the VBA code that will connect your Excel spreadsheet to the power of Gemini.
If you’re a beginner working with VBA, using the Gemini API key for Excel lets you experiment with AI integration in the most practical way — within tools you already use.
The following function is a foundational example that demonstrates how to make an HTTP POST request to the Gemini API
Function GetResponsefromGemini(strPrompt As String) As String
Dim apiKey As String
Dim strModel As String
Dim endpointUrl As String
Dim requestData As String
Dim xmlHttp As Object
Dim strRawResponse As String
Dim startPos As Long
Dim endPos As Long
' === IMPORTANT: Replace these two values with your own ===
apiKey = "AbC9XYZ8abc7_123B_t31h7E"
strModel = "gemini-2.0-flash-lite"
' =========================================================
' Construct the endpoint URL
endpointUrl = "https://generativelanguage.googleapis.com/v1beta/models/" & strModel & ":generateContent?key=" & apiKey
' Prepare the request data as a JSON string
requestData = "{""contents"": [{""parts"":[{""text"": """ & strPrompt & """}]}]}"
' Create an XMLHTTP object to make the API request
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
' Set the request headers
xmlHttp.Open "POST", endpointUrl, False
xmlHttp.setRequestHeader "Content-Type", "application/json"
' Send the request with the JSON data
xmlHttp.Send requestData
' Check for a successful response (HTTP status 200)
If xmlHttp.Status = 200 Then
strRawResponse = xmlHttp.responseText
' Find the start of the "text" field
startPos = InStr(1, strRawResponse, """text"":")
' Check if the "text" field was found
If startPos > 0 Then
' Move past the label and the opening quote
startPos = startPos + 8
' Find the start of the "role" field
endPos = InStr(startPos, strRawResponse, """role"": ""model")
' Ensure a closing quote was found
If endPos > 0 Then
' Extract the text content
GetResponsefromGemini = Mid(strRawResponse, startPos + 1, endPos - startPos - 34)
Else
' Handle cases where the text field is present but empty or malformed
GetResponsefromGemini = ""
End If
Else
' Handle cases where the "text" field is not in the response
GetResponsefromGemini = "No text content found in response."
End If
' ---------------------------------------------------
Else
' Show an error message if the request failed
MsgBox "Error: " & xmlHttp.Status & " - " & xmlHttp.statusText
GetResponsefromGemini = ""
End If
' Clean up the XMLHTTP object
Set xmlHttp = Nothing
End Function
If your Gemini-powered tool needs to send Excel data in a clean, readable format, consider using VBA to convert your Excel range into an HTML table before sending it to the API.
Important Security Note:
If you are using a free account, your usage will be subject to a certain limit. For example, you might be allowed a specific number of requests per minute and a total number of requests per day. This is a generous limit for personal use and for learning purposes.
It’s crucial to understand that this API key is essentially the password to your Google account’s Gemini AI access. Therefore:
- Do Not Share It: Never share your API key with others. If someone else gets hold of it, they can use it and you will be responsible for any usage costs that may be incurred if your usage exceeds the free limits.
- Do Not Use in Production: The method of storing the API key directly in a public-facing file like an Excel spreadsheet is highly insecure. This is acceptable for personal projects and learning, but it is not suitable for production environments. For a production application, you should use more secure methods like environment variables or a secure backend service to manage your API key.
Your Gemini API key for Excel should always be stored securely, especially if you plan to use it across multiple spreadsheets.
Check Your API Usage and Available Models
Now that you have your Gemini API key for Excel, it’s a good idea to know how to monitor your usage and understand which models you can use.
How to Check Your Usage
Google provides a dashboard where you can easily see your API usage. This is especially important for free tier users to ensure they don’t hit their limits.
- Go to the Google AI Studio page where you got your API key.
- On the left-hand navigation menu, click on “Usage & Billing”.

This page provides a simple and clear overview of your API usage. You will see two key metrics:
- Input Token Count per Day: This graph shows the number of “tokens” (a token is a piece of a word) that you have sent to the Gemini models. This helps you monitor how much data you are processing.
- Request Count per Day: This graph shows the total number of times your code has successfully called the Gemini API. This is the primary metric for tracking your usage against the free-tier limits.
By monitoring these graphs, you can easily keep track of your API activity and make sure you stay within your account’s limits.
FAQs – Getting Started with Gemini API Key for Excel VBA
No, the process is beginner-friendly and doesn’t require any prior coding or API experience. The article walks you through every step using Google AI Studio, a simple web-based tool.
You can generate your Gemini API key by signing in to Google AI Studio, clicking “Create API key,” and choosing to create a new project. The article explains this step-by-step.
For personal projects, you can temporarily store the API key in your VBA code. However, for production use, it’s recommended to use secure methods like environment variables or a backend server to protect the key.
Yes, Google offers a free tier with usage limits. You can check your token and request usage in the “Usage & Billing” section of Google AI Studio to ensure you stay within the free limits.
Gemini models are different versions of Google’s generative AI. For Excel VBA, lightweight models like gemini-2.5-flash or gemini-2.5-flash-lite are great for quick, cost-efficient responses. The article explains how to choose the right one.






