Google’s Gemini models round out the big three (alongside OpenAI and Claude), and they’re worth having in your toolkit — strong multimodal support and competitive pricing. This guide shows how to call Gemini from C#, whether through its REST API directly or a provider-agnostic .NET abstraction.
Getting access
You can reach Gemini two ways:
- Google AI Studio — a simple API key, quickest to start with.
- Vertex AI — Gemini through Google Cloud, with IAM/Workload Identity auth. Use this for production on GCP (and it pairs with deploying on GKE via Workload Identity — no keys).
For a first call, grab a key from AI Studio and store it in an environment variable.
Calling the REST API from C#
Gemini’s generateContent endpoint is a straightforward HTTP POST:
using System.Net.Http.Json;
var http = new HttpClient();
var key = Environment.GetEnvironmentVariable("GEMINI_API_KEY");
var url = $"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key={key}";
var body = new
{
contents = new[]
{
new { parts = new[] { new { text = "Explain eventual consistency in two sentences." } } }
}
};
var res = await http.PostAsJsonAsync(url, body);
var json = await res.Content.ReadFromJsonAsync<JsonElement>();
var text = json.GetProperty("candidates")[0]
.GetProperty("content").GetProperty("parts")[0].GetProperty("text").GetString();
Console.WriteLine(text);
Note Gemini’s request shape differs from OpenAI’s: content is a list of parts, not messages with roles (though there’s a role-based structure for multi-turn).
The portable way: Microsoft.Extensions.AI
As with every provider, the maintainable approach is to hide Gemini behind Microsoft.Extensions.AI’s IChatClient, so your app and any agent code is model-agnostic:
IChatClient chat = /* a Gemini-backed IChatClient */;
var reply = await chat.GetResponseAsync("Give me three uses for a message queue.");
Then Gemini, GPT, and Claude are interchangeable, and you can benchmark them on your own eval set.
Where Gemini shines
Gemini’s strong multimodal support (text, images, and more in one request) makes it a natural pick when your input isn’t just text — analyzing screenshots, documents, or diagrams. If your workload is multimodal, it’s worth benchmarking Gemini specifically rather than defaulting to a text-first model.
Production on Google Cloud
If you’re already on GCP, prefer Vertex AI over an API key: your app authenticates with a managed identity via Workload Identity, so there’s no key to leak. This is the same keyless pattern covered in the GKE deployment guide and recommended in securing AI agents.
Note: Gemini model IDs (e.g.
gemini-2.5-flash) and endpoints evolve. Verify against the Gemini API docs for current models; the approach — RESTgenerateContentorIChatClientfor portability, Vertex AI for keyless production — is stable.
Takeaway
Calling Gemini from C# is a simple REST POST to generateContent, or — better — a call through Microsoft.Extensions.AI so it’s one swappable provider. Use an AI Studio key to prototype, move to Vertex AI with Workload Identity for keyless production on GCP, and lean on Gemini’s multimodal strength when your inputs go beyond plain text. Build on IChatClient and switching between Gemini, GPT, and Claude stays a config change.
Have a correction or a topic you want covered? Email mani.bc72@gmail.com.