OpenCodeClient 0.1.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package OpenCodeClient --version 0.1.1
NuGet\Install-Package OpenCodeClient -Version 0.1.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenCodeClient" Version="0.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenCodeClient" Version="0.1.1" />
<PackageReference Include="OpenCodeClient" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenCodeClient --version 0.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OpenCodeClient, 0.1.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenCodeClient@0.1.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenCodeClient&version=0.1.1
#tool nuget:?package=OpenCodeClient&version=0.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
OpenCodeClient
A .NET client library for interacting with the OpenCode API.
Installation
Install the NuGet package:
dotnet add package OpenCodeClient
Quick Start
1. Register services
using OpenCodeClient.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenCodeClient(options =>
{
options.BaseUrl = "http://localhost:4096";
});
2. Inject and use services
using OpenCodeClient.Abstractions;
public class MyService(ISessionService sessions, IGlobalService global)
{
public async Task RunAsync()
{
// Check server health
var health = await global.GetHealthAsync();
Console.WriteLine($"Server v{health.Version}, healthy: {health.Healthy}");
// List sessions
var allSessions = await sessions.ListAsync();
foreach (var session in allSessions)
{
Console.WriteLine($" [{session.Id}] {session.Title}");
}
// Create a new session
var newSession = await sessions.CreateAsync(new() { Title = "My Session" });
// Send a prompt
var response = await sessions.PromptAsync(newSession.Id, new()
{
Parts = [new TextPartInput { Text = "Hello, world!" }]
});
Console.WriteLine($"Response cost: {response.Info.Cost}");
}
}
3. Subscribe to events (SSE)
using OpenCodeClient.Abstractions;
public class EventListener(IGlobalService global)
{
public async Task ListenAsync(CancellationToken ct)
{
await foreach (var globalEvent in global.SubscribeToEventsAsync(ct))
{
Console.WriteLine($"[{globalEvent.Directory}] Event: {globalEvent.Payload.Type}");
}
}
}
Available Services
| Interface | Description |
|---|---|
IGlobalService |
Server health, global config, SSE events, dispose |
IAuthService |
Authentication credential management |
IProjectService |
Project listing, creation, and updates |
IPtyService |
Pseudo-terminal session management |
IConfigService |
Project-level configuration |
ISessionService |
AI session management, messaging, prompts |
IPermissionService |
Permission request management |
IQuestionService |
Question request management |
IProviderService |
AI provider and OAuth management |
IFindService |
Text, file, and symbol search |
IFileService |
File listing, reading, git status |
IMcpService |
MCP server management |
ITuiService |
Terminal UI interactions |
IExperimentalService |
Tools, workspaces, worktrees |
IAppService |
Logging, agents, skills, commands |
IInfraService |
Paths, VCS, LSP, formatter, event subscription |
Configuration
The OpenCodeClientOptions class provides the following settings:
| Property | Type | Default | Description |
|---|---|---|---|
BaseUrl |
string |
http://localhost:4096 |
Base URL of the OpenCode server |
Username |
string |
opencode |
Username for HTTP Basic authentication |
Password |
string? |
null |
Password for HTTP Basic authentication. When set, every request includes an Authorization: Basic header |
Authentication
If the server is protected with OPENCODE_SERVER_PASSWORD, configure credentials:
builder.Services.AddOpenCodeClient(options =>
{
options.BaseUrl = "http://localhost:4096";
options.Password = "your-password";
// options.Username = "opencode"; // default, override if OPENCODE_SERVER_USERNAME is set
});
Advanced HTTP client configuration
The AddOpenCodeClient method returns an IHttpClientBuilder, allowing further customization:
builder.Services.AddOpenCodeClient(o => o.BaseUrl = "https://my-server.example.com")
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
// Custom handler settings
})
.AddStandardResilienceHandler(); // Requires Microsoft.Extensions.Http.Resilience
Project Structure
OpenCodeClient/
Models/ -- DTO models (records, classes, enums)
Abstractions/ -- Service interfaces with XML docs
Services/ -- HTTP service implementations
Events/ -- SSE client for streaming endpoints
Extensions/ -- IServiceCollection extensions
OpenCodeClientOptions.cs -- Configuration options
Requirements
- .NET 9.0 or later
Microsoft.Extensions.DependencyInjection.Abstractions9.0.0Microsoft.Extensions.Http9.0.0Microsoft.Extensions.Options9.0.0
License
Apache-2.0
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.