![]() |
VOOZH | about |
dotnet add package Google_GenerativeAI.Web --version 3.6.6
NuGet\Install-Package Google_GenerativeAI.Web -Version 3.6.6
<PackageReference Include="Google_GenerativeAI.Web" Version="3.6.6" />
<PackageVersion Include="Google_GenerativeAI.Web" Version="3.6.6" />Directory.Packages.props
<PackageReference Include="Google_GenerativeAI.Web" />Project file
paket add Google_GenerativeAI.Web --version 3.6.6
#r "nuget: Google_GenerativeAI.Web, 3.6.6"
#:package Google_GenerativeAI.Web@3.6.6
#addin nuget:?package=Google_GenerativeAI.Web&version=3.6.6Install as a Cake Addin
#tool nuget:?package=Google_GenerativeAI.Web&version=3.6.6Install as a Cake Tool
Google_GenerativeAI.Web simplifies integrating Google Generative AI (Gemini and Vertex AI) into .NET web applications (ASP.NET Core, Minimal APIs, etc.). It provides convenient extensions for IServiceCollection to configure and inject IGenerativeAiService, making it easy to use Google's generative models in your controllers, services, or other components.
IGenerativeAiService for easy injection via constructor injection.GOOGLE_PROJECT_ID is set) or the Gemini API.appsettings.json (or other configuration providers)GenerativeAIOptionsIGenerativeAiService (from the underlying Google_GenerativeAI library) provides both streaming and non-streaming methods.Install the NuGet package:
dotnet add package Google_GenerativeAI.Web
Configure the library using one of the following methods.
Important: Choose one authentication method. The methods are listed in order of precedence (highest to lowest).
These methods set the Authenticator property of the options, which takes precedence over any API key or access token set via Credentials.
Google Application Default Credentials (ADC):
The recommended approach for applications running on Google Cloud (Compute Engine, Cloud Run, App Engine, etc.).
// In Program.cs or Startup.cs
using Google_GenerativeAI.Web;
builder.Services.AddGenerativeAI().builder.Services.WithGoogleAdcAuthentication(); // Or any other AddGenerativeAI overload.
Google Service Account (JSON key file):
For applications running outside of Google Cloud, or where you need to use a specific service account.
builder.Services.AddGenerativeAI().WithGoogleServiceAuthentication("path/to/your/service-account.json");;
Google Service Account (PKCS12/P12 certificate file):
An alternative to the JSON key file.
builder.Services.AddGenerativeAI().WithGoogleServiceAuthentication("[email address removed]", "path/to/certificate.p12", "your-passphrase");
Google OAuth 2.0 (client secret JSON file):
For applications that need to access Google AI on behalf of a user.
builder.Services.AddGenerativeAI().WithGoogleOAuthAuthentication("path/to/your/client_secret.json");
These methods set the Credentials property. You can configure these options via environment variables, appsettings.json, or directly in code.
Environment Variables (Recommended for API Key):
Set the following environment variables:
// In Program.cs or Startup.cs
builder.Services.AddGenerativeAI(); // Reads from environment variables.
appsettings.json (or other configuration providers):
{
"GenerativeAI": {
"Credentials": {
"ApiKey": "YOUR_API_KEY"
// OR, for OAuth 2.0:
// "AccessToken": "YOUR_ACCESS_TOKEN",
// "Expiry": "2024-03-15T12:00:00Z" // Optional. ISO 8601 format.
},
"ProjectId": "YOUR_PROJECT_ID", // Optional (for Vertex AI)
"Region": "us-central1", // Optional (defaults to us-central1)
"Model": "gemini-pro", // Optional (defaults to gemini-1.0-pro)
"IsVertex": false, // Optional. Set to true to force Vertex AI.
"ExpressMode": false // Optional
}
}
// In Program.cs or Startup.cs
builder.Services.AddGenerativeAI(builder.Configuration.GetSection("GenerativeAI"));
// OR, using a configuration path:
builder.Services.AddGenerativeAI("GenerativeAI");
Directly in Code (using GenerativeAIOptions):
using Google_GenerativeAI.Web;
using GenerativeAI.GoogleAuth;
// API Key:
builder.Services.AddGenerativeAI(new GenerativeAIOptions
{
Credentials = new GoogleAICredentials("YOUR_API_KEY"),
ProjectId = "YOUR_PROJECT_ID", // Optional (for Vertex AI)
Region = "us-central1", // Optional
Model = "gemini-pro" // Optional
});
// OR, for OAuth 2.0 Access Token:
builder.Services.AddGenerativeAI(new GenerativeAIOptions
{
Credentials = new GoogleAICredentials(null, "YOUR_ACCESS_TOKEN", DateTime.UtcNow.AddHours(1)), // Access token with expiry
ProjectId = "YOUR_PROJECT_ID", // Optional
Region = "us-central1", // Optional
Model = "gemini-pro" // Optional
});
// OR, using an Action:
builder.Services.AddGenerativeAI(options =>
{
options.Credentials = new GoogleAICredentials("YOUR_API_KEY");
// OR, for OAuth 2.0:
// options.Credentials = new GoogleAICredentials(null, "YOUR_ACCESS_TOKEN", DateTime.UtcNow.AddHours(1));
options.ProjectId = "YOUR_PROJECT_ID"; // Optional
options.Region = "us-central1"; // Optional
options.Model = "gemini-pro"; // Optional
});
Inject IGenerativeAiService into your controllers, services, or other components using constructor injection:
using GenerativeAI; // From the main Google_GenerativeAI SDK
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
public class MyController : Controller
{
private readonly IGenerativeAiService _aiService;
public MyController(IGenerativeAiService aiService)
{
_aiService = aiService;
}
[HttpGet]
public async Task<IActionResult> GenerateText(string prompt)
{
var response = await _aiService.GenerateContentAsync(prompt);
return Ok(response.Text()); // Access the generated text.
}
[HttpGet]
public async Task StreamText(string prompt)
{
Response.ContentType = "text/plain"; // Set content type for streaming.
await foreach (var chunk in _aiService.GenerateContentStreamAsync(prompt))
{
await Response.WriteAsync(chunk.Text());
await Response.Body.FlushAsync(); // Crucial for streaming!
}
}
}
Complete Example (ASP.NET Core Minimal API)
using GenerativeAI;
using Google_GenerativeAI.Web;
using GenerativeAI.GoogleAuth;
var builder = WebApplication.CreateBuilder(args);
// API Key Example (read from environment variables):
builder.Services.AddGenerativeAI();
// OR OAuth 2.0 Example (read access token however you obtain it):
//builder.Services.AddGenerativeAI(options =>
//{
// options.Credentials = new GoogleAICredentials(null, "YOUR_ACCESS_TOKEN", DateTime.UtcNow.AddHours(1));
//});
//OR Service Account
//builder.Services.WithGoogleServiceAuthentication("path/to/your/service-account.json");
//builder.Services.AddGenerativeAI();
var app = builder.Build();
app.MapGet("/generate", async (IGenerativeAiService aiService, string prompt) =>
{
var response = await aiService.GenerateContentAsync(prompt);
return response.Text();
});
app.MapGet("/stream", async (IGenerativeAiService aiService, string prompt, HttpContext context) =>
{
context.Response.ContentType = "text/plain";
await foreach (var chunk in aiService.GenerateContentStreamAsync(prompt))
{
await context.Response.WriteAsync(chunk.Text());
await context.Response.Body.FlushAsync();
}
});
app.Run();
Contributions are welcome! Please submit pull requests or open issues to discuss proposed changes or report bugs.
MIT License - see the LICENSE file.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.6.6 | 348 | 4/20/2026 |
| 3.6.5 | 119 | 4/18/2026 |
| 3.6.4 | 374 | 3/26/2026 |
| 3.6.3 | 274 | 1/23/2026 |
| 3.6.2 | 129 | 1/15/2026 |
| 3.6.1 | 243 | 12/28/2025 |
| 3.5.0 | 171 | 12/26/2025 |
| 3.4.1 | 265 | 11/8/2025 |
| 3.4.0 | 317 | 10/21/2025 |
| 3.3.0 | 541 | 10/3/2025 |
| 3.2.0 | 721 | 9/10/2025 |
| 3.1.1 | 196 | 9/7/2025 |
| 3.0.0 | 843 | 7/21/2025 |
| 2.7.0 | 869 | 6/2/2025 |
| 2.6.0 | 425 | 5/25/2025 |
| 2.5.10 | 270 | 5/9/2025 |
| 2.5.9.1 | 225 | 5/25/2025 |
| 2.5.9 | 282 | 4/25/2025 |
| 2.5.8.1 | 240 | 5/25/2025 |
| 2.5.8 | 293 | 4/18/2025 |