Getting started with Google Gemini AI and C# .Net
I was inspired by an article I read on Hacker News about writing your own agent. The example was in Python and I wanted to try and re-write it in C#.
Using the OpenAI package for dotnet made the initial code simple to write. But it quickly became apparent that what the article had failed to mention was that it costs money to use OpenAI agents in the way specified. The easy way to solve that would of course have been to just buy a little credit from OpenAI, but someone in the comments section mentioned that Google Gemini AI had a number of free tokens included.
Hence the plan was clear. Try to follow the “article” but use C# instead of Python and Google Gemini instead of OpenAI
First stop was finding the Google Gemini Quickstart guide.
It mentions that first stop is creating an API key by going to Google AI Studio. In my case it created it without me doing anything, but otherwise there is a button to create a new API key - so it cannot be much easier.
Then back to the quickstart guide where the next step is to add the Google GenAI SDK:
dotnet add package Google.GenAI
Then comes the following code example in C#:
using System.Threading.Tasks;
using Google.GenAI;
using Google.GenAI.Types;
public class GenerateContentSimpleText {
public static async Task main() {
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
var client = new Client();
var response = await client.Models.GenerateContentAsync(
model: "gemini-2.5-flash", contents: "Explain how AI works in a few words"
);
Console.WriteLine(response.Candidates[0].Content.Parts[0].Text);
}
}
The first issue with this code-snippet became apparent quickly. Google example code had main() spelled with lowercase “m”. So first step was correcting that and submitting it as an error on the Google page.
This enabled me to try and debug the code. Doing so highlighted the next issue; the need to set an environment variable.
My first try in doing so was using launchSettings.json. Here I created the “GEMINI_API_KEY” and inserted the value I had obtained from Google AI Studio.
Debugging the code again showed me that it still did not work.
Hmm strange..
But then it became apparent that where the code breaks it does NOT look for an environment variable named: “GEMINI_API_KEY” - as stated in the code example but instead an environment variable named: “GOOGLE_API_KEY”.
So I changed it and tried to run it again.
But still no luck. It seemed like setting of the environment variable did not work.
I then tried the DotNetEnv package which uses .env files to specify environment variables. In the code I added:
using DotNetEnv
// And in the method:
Env.Load();
Still no luck.
Okay, now I was tired of trying to set environment variables with config-files and decided to just add it programmatically as I wanted to create yet another bug for the Google Example page if I was correct in assuming they had named their API key wrong.
Hence I set it with:
Environment.SetEnvironmentVariable("GOOGLE_API_KEY", "blablablabla");
Finally it worked.
The code could execute and I could create yet another “bug-report” for the Google Gemini AI example page.
How I could be the first to see these errors is beyond me. But maybe there are not that many people using C# to integrate with Google Gemini AI?