DEV Community

Pawel Janda
Pawel Janda

Posted on • Edited on

The easiest way to connect your .NET app to Gemini using MaIN.NET.

Integrating large language models like Gemini from Google into a .NET application is often associated with setting up HTTP clients, managing API payloads, handling rate limits, and parsing responses. If you're just starting out or want a simpler way, the MaIN.NET library offers a minimal setup to get you connected with just a few lines of code.

This tutorial shows how to configure your app to use Gemini and includes a small example project to demonstrate interaction with the model.


What You’ll need?

  • .NET SDK.
  • A Gemini API key from Google AI Studio or Vertex AI.
  • An IDE or text editor (e.g., Visual Studio Code, Rider, or Cursor)
  • Internet connection for API access

💡 Good to know: Google currently offers a generous free quota for Gemini API usage via both AI Studio and Vertex AI. This is a great opportunity to test LLM features without incurring any cost. Make sure to check your quota limits in your Google Cloud console or AI Studio account.


1. What is MaIN.NET?

MaIN.NET is a powerful open-source framework that standardizes how .NET apps connect to various LLM backends both localy and in the cloud. It supports models from providers like OpenAI, Gemini, Mistral, LLama, Deepseek, Qwen and others. It wraps the low-level HTTP calls and offers a uniform API surface for chat completion and future agent-based capabilities.

You don’t need to write custom HTTP logic. Instead, you just configure the backend and use the built-in methods to orchestrate LLMs.


2. Minimal Gemini integration.

Here’s the full setup needed to enable Gemini in your .NET application using MaIN.NET:

using MaIN.Core;
using MaIN.Core.Hub;
using MaIN.Domain.Configuration;

MaINBootstrapper.Initialize(configureSettings: (options) =>
{
    options.BackendType = BackendType.Gemini;
    options.GeminiKey = "YOUR_GEMINI_API_KEY";
});

string prompt = "What is LLM?";
var response = await AIHub.Chat()
    .WithModel("gemini-2.0-flash")
    .WithMessage(prompt)
    .CompleteAsync(interactive: true);

string result = response.Message.Content;
Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

Replace "YOUR_GEMINI_API_KEY" with your actual key.


3. Example: Simple console app to query Gemini.

We’ll now create a simple app that accepts user input and returns a response from Gemini.

Step-by-Step:

a. Create a new project

dotnet new console -n GeminiExample
cd GeminiExample
Enter fullscreen mode Exit fullscreen mode

b. Add MaIN.NET package

dotnet add package MaIN.NET
Enter fullscreen mode Exit fullscreen mode

c. Add your code

Edit Program.cs:

using System;
using System.Threading.Tasks;
using MaIN.Core;
using MaIN.Core.Hub;
using MaIN.Domain.Configuration;


// Initialize MaIN.NET with Gemini
MaINBootstrapper.Initialize(configureSettings: (options) =>
{
    options.BackendType = BackendType.Gemini;
    options.GeminiKey = "YOUR_GEMINI_API_KEY";
});

Console.Write("Ask Gemini something: ");
var input = Console.ReadLine();

var response = await AIHub.Chat()
    .WithModel("gemini-2.0-flash")
    .WithMessage(input)
    .CompleteAsync(interactive: true);

Console.WriteLine("\nResponse:");
Console.WriteLine(response.Message.Content);

Enter fullscreen mode Exit fullscreen mode

d. Run the app

dotnet run
Enter fullscreen mode Exit fullscreen mode

Try inputs like:

  • "Explain how JWT tokens work"
  • "Generate 3 creative names for a coffee startup"
  • "What’s the difference between async and parallel in C#?"

4. Behind the scenes.

Here’s what happens under the hood when you call CompleteAsync():

  • The MaIN.NET library prepares the API request with the selected backend (Gemini).
  • It serializes your prompt and model choice into the format Gemini expects.
  • It sends the HTTP request and handles the response parsing.
  • You receive the output as a typed object, making it easier to access content directly.

5. Where to go next?

If you want to take it further, try building a Blazor-based website integrated with Gemini — this guide on using MaIN.NET with Blazor can help you get started.

Summary.

Using MaIN.NET, connecting your .NET application to Gemini is quick and clean. You don’t need to handle HTTP setup, JSON parsing, or authentication flows manually. If you’re experimenting with LLMs, Google’s current free tier provides a great environment to build and test ideas without worrying about cost. Happy coding!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.