Skip to main content
net8.0net9.0net10.0.NETStandard2.0

A client for ASP.NET Core designed to consume Gemini AI.

Published
Dec 31, 2025
9mo ago
Total Downloads
73.5K
73,550

Install

dotnet add package Mscc.GenerativeAI.Web --version 3.0.2
ProductVersions
.NET
net10.0net8.0net9.0
.NET Standard
netstandard2.0

README

Gemini AI Client for ASP.NET Core

GitHub GitHub last commit

Integrate Gemini API into your web projects. It works both Google AI (Studio) and Google Cloud Vertex AI.

Client for ASP.NET Core: NuGet Version NuGet Downloads

Getting started

Either create a new ASP.NET Core project or use an existing one.

> dotnet new web -o Web.Minimal.Api

Navigate to the project's root folder and install the package Mscc.GenerativeAI.Web from NuGet. You can install the package from the command line using either the command line or the NuGet Package Manager Console. Or you add it directly to your .NET project.

Add the package using the dotnet command line tool in your .NET project folder.

> dotnet add package Mscc.GenerativeAI.Web

Working with Visual Studio use the NuGet Package Manager to install the package Mscc.GenerativeAI.Web.

PM> Install-Package Mscc.GenerativeAI.Web

Alternatively, add the following line to your .csproj file.

  <ItemGroup>
    <PackageReference Include="Mscc.GenerativeAI.Web" Version="3.0.2" />
  </ItemGroup>

You can then add this code to your sources whenever you need to access any Gemini API provided by Google. This package works currently for Google AI (Google AI Studio) only. Use with Google Cloud Vertex AI is provided by the underlying Mscc.GenerativeAI package but not exposed yet.

Using Google AI Gemini API

Working with Google AI in your application requires an API key. Get an API key from Google AI Studio.

Add the following configuration to the appsettings.json file.

{
// section name and location to your liking.
  "Gemini": {
    "Credentials": {
      "ApiKey": "YOUR_API_KEY"  // replace value with key from AI Studio
    },
    "ProjectId": "",
    "Region": "us-central1",
    "Model": "gemini-1.5-pro"   // default value
  },
// any other settings...
  "Logging": {
  },
}

The section name Gemini is arbitrary as well as the location of the section. Although, it needs to be referenced correctly in the Configuration builder.

Using the AddGenerativeAI service

Next, add the service AddGenerativeAI() to the ASP.NET Core web app and map the routes as needed. Following is the most minimal implementation.

using Mscc.GenerativeAI.Web;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGenerativeAI(builder.Configuration.GetSection("Gemini"));

var app = builder.Build();

app.MapGet("/", async (IGenerativeModelService service) =>
{
    var model = service.CreateInstance();
    var result = await model.GenerateContent("Write about the history of Mauritius.");
    return result.Text;
});

app.Run();

There are overloads of the extension method AddGenerativeAI() according to Options pattern guidance for .NET library authors. Following approaches are available:

  • Parameterless
  • IConfiguration parameter (as shown above)
  • Configuration section path parameter
  • Action parameter
  • Options instance parameter

Hoping this provides enough flexibility for individual preferences.

Using a typed HttpClient

Alternatively to the above described service extension methods Mscc.GenerativeAI.Web also provides a typed HttpClient that can be added to your ASP.NET web application.

sing Mscc.GenerativeAI.Web;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpClient<GenerativeAIClient>()
{
    BaseAddress = "";
};

var app = builder.Build();

app.MapGet("/", async (IGenerativeModelService service) =>
{
    var model = service.CreateInstance();
    var result = await model.GenerateContent("Write about the history of Mauritius.");
    return result.Text;
});

app.Run();

Use global usings

Keeping the code base minimal, one should consider creating a GlobalUsings.cs file and define the using statements there.

global using Mscc.GenerativeAI.Web;

This approach renders repetitive use of using Mscc.GenerativeAI.Web; in each .cs file obsolete.

Configure the service

Find each approach documented. Surrounding source code skipped for brevity.

Parameterless๐Ÿ”—

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGenerativeAI();

var app = builder.Build();
// ...

IConfiguration parameter (as shown above)๐Ÿ”—

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGenerativeAI(builder.Configuration.GetSection("Gemini"));

var app = builder.Build();
// ...

Configuration section path parameter๐Ÿ”—

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGenerativeAI("Gemini");

var app = builder.Build();
// ...

Action parameter๐Ÿ”—

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGenerativeAI(options =>
{
    // User defined option values
    options.ProjectId = string.Empty;
    options.Model = GenerativeAI.Types.Model.GeminiProVision;
    options.Credentials.ApiKey = "YOUR_API_KEY";
});

var app = builder.Build();
// ...

Options instance parameter๐Ÿ”—

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGenerativeAI(new GenerativeAIOptions
{
    // Specify option values
    ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"),
    Region = Environment.GetEnvironmentVariable("GOOGLE_REGION"),
    Model = Environment.GetEnvironmentVariable("GOOGLE_MODEL"),
    Credentials = new() { ApiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY") }
});

var app = builder.Build();
// ...

The choice of configuring the service is yours.

More examples

The folders samples and tests contain more examples.

Troubleshooting

tba

Feedback

You can create issues at the https://github.com/mscraftsman/generative-ai repository.

Changes from Previous Version

No structural changes between v3.0.1 and v3.0.2.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 4 target frameworks; confirm the exact target used by your application.The published metadata declares 48 direct dependency entries across framework groups. Inspect the resolved dependency graph for your target framework.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.