Stagehand 3.0.2

dotnet add package Stagehand --version 3.0.2
                    
NuGet\Install-Package Stagehand -Version 3.0.2
                    
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="Stagehand" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Stagehand" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Stagehand" />
                    
Project file
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 Stagehand --version 3.0.2
                    
#r "nuget: Stagehand, 3.0.2"
                    
#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 Stagehand@3.0.2
                    
#: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=Stagehand&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Stagehand&version=3.0.2
                    
Install as a Cake Tool

Stagehand C# API Library

The Stagehand C# SDK provides convenient access to the Stagehand REST API from applications written in C#.

It is generated with Stainless.

The REST API documentation can be found on docs.stagehand.dev.

Installation

Install the package from NuGet:

dotnet add package Stagehand

Requirements

This library requires .NET Standard 2.0 or later.

Usage

See the examples directory for complete and runnable examples.

using System;
using Stagehand;
using Stagehand.Models.Sessions;

StagehandClient client = new();

SessionActParams parameters = new()
{
    ID = "00000000-your-session-id-000000000000",
    Input = "click the first link on the page",
};

var response = await client.Sessions.Act(parameters);

Console.WriteLine(response);

Client configuration

Configure the client using environment variables:

using Stagehand;

// Configured using the BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY and STAGEHAND_BASE_URL environment variables
StagehandClient client = new();

Or manually:

using Stagehand;

StagehandClient client = new()
{
    BrowserbaseApiKey = "My Browserbase API Key",
    BrowserbaseProjectID = "My Browserbase Project ID",
    ModelApiKey = "My Model API Key",
};

Or using a combination of the two approaches.

See this table for the available options:

Property Environment variable Required Default value
BrowserbaseApiKey BROWSERBASE_API_KEY true -
BrowserbaseProjectID BROWSERBASE_PROJECT_ID true -
ModelApiKey MODEL_API_KEY true -
BaseUrl STAGEHAND_BASE_URL true "https://api.stagehand.browserbase.com"

Modifying configuration

To temporarily use a modified client configuration, while reusing the same connection and thread pools, call WithOptions on any client or service:

using System;

var response = await client
    .WithOptions(options =>
        options with
        {
            BaseUrl = "https://example.com",
            Timeout = TimeSpan.FromSeconds(42),
        }
    )
    .Sessions.Start(parameters);

Console.WriteLine(response);

Using a with expression makes it easy to construct the modified options.

The WithOptions method does not affect the original client or service.

Requests and responses

To send a request to the Stagehand API, build an instance of some Params class and pass it to the corresponding client method. When the response is received, it will be deserialized into an instance of a C# class.

For example, client.Sessions.Act should be called with an instance of SessionActParams, and it will return an instance of Task<SessionActResponse>.

Streaming

The SDK defines methods that return response "chunk" streams, where each chunk can be individually processed as soon as it arrives instead of waiting on the full response. Streaming methods generally correspond to SSE or JSONL responses.

Some of these methods may have streaming and non-streaming variants, but a streaming method will always have a Streaming suffix in its name, even if it doesn't have a non-streaming variant.

These streaming methods return IAsyncEnumerable:

using System;
using Stagehand.Models.Sessions;

SessionActParams parameters = new()
{
    ID = "00000000-your-session-id-000000000000",
    Input = "click the first link on the page",
};

await foreach (var response in client.Sessions.ActStreaming(parameters))
{
    Console.WriteLine(response);
}

Raw responses

The SDK defines methods that deserialize responses into instances of C# classes. However, these methods don't provide access to the response headers, status code, or the raw response body.

To access this data, prefix any HTTP method call on a client or service with WithRawResponse:

var response = await client.WithRawResponse.Sessions.Start(parameters);
var statusCode = response.StatusCode;
var headers = response.Headers;

The raw HttpResponseMessage can also be accessed through the RawMessage property.

For non-streaming responses, you can deserialize the response into an instance of a C# class if needed:

using System;
using Stagehand.Models.Sessions;

var response = await client.WithRawResponse.Sessions.Start(parameters);
SessionStartResponse deserialized = await response.Deserialize();
Console.WriteLine(deserialized);

For streaming responses, you can deserialize the response to an IAsyncEnumerable if needed:

using System;

var response = await client.WithRawResponse.Sessions.ActStreaming(parameters);
await foreach (var item in response.Enumerate())
{
    Console.WriteLine(item);
}

Error handling

The SDK throws custom unchecked exception types:

  • StagehandApiException: Base class for API errors. See this table for which exception subclass is thrown for each HTTP status code:
Status Exception
400 StagehandBadRequestException
401 StagehandUnauthorizedException
403 StagehandForbiddenException
404 StagehandNotFoundException
422 StagehandUnprocessableEntityException
429 StagehandRateLimitException
5xx Stagehand5xxException
others StagehandUnexpectedStatusCodeException

Additionally, all 4xx errors inherit from Stagehand4xxException.

  • StagehandSseException: thrown for errors encountered during SSE streaming after a successful initial HTTP response.

  • StagehandIOException: I/O networking errors.

  • StagehandInvalidDataException: Failure to interpret successfully parsed data. For example, when accessing a property that's supposed to be required, but the API unexpectedly omitted it from the response.

  • StagehandException: Base class for all exceptions.

Network options

Retries

The SDK automatically retries 2 times by default, with a short exponential backoff between requests.

Only the following error types are retried:

  • Connection errors (for example, due to a network connectivity problem)
  • 408 Request Timeout
  • 409 Conflict
  • 429 Rate Limit
  • 5xx Internal

The API may also explicitly instruct the SDK to retry or not retry a request.

To set a custom number of retries, configure the client using the MaxRetries method:

using Stagehand;

StagehandClient client = new() { MaxRetries = 3 };

Or configure a single method call using WithOptions:

using System;

var response = await client
    .WithOptions(options =>
        options with { MaxRetries = 3 }
    )
    .Sessions.Start(parameters);

Console.WriteLine(response);

Timeouts

Requests time out after 1 minute by default.

To set a custom timeout, configure the client using the Timeout option:

using System;
using Stagehand;

StagehandClient client = new() { Timeout = TimeSpan.FromSeconds(42) };

Or configure a single method call using WithOptions:

using System;

var response = await client
    .WithOptions(options =>
        options with { Timeout = TimeSpan.FromSeconds(42) }
    )
    .Sessions.Start(parameters);

Console.WriteLine(response);

Undocumented API functionality

The SDK is typed for convenient usage of the documented API. However, it also supports working with undocumented or not yet supported parts of the API.

Response validation

In rare cases, the API may return a response that doesn't match the expected type. For example, the SDK may expect a property to contain a string, but the API could return something else.

By default, the SDK will not throw an exception in this case. It will throw StagehandInvalidDataException only if you directly access the property.

If you would prefer to check that the response is completely well-typed upfront, then either call Validate:

var response = client.Sessions.Act(parameters);
response.Validate();

Or configure the client using the ResponseValidation option:

using Stagehand;

StagehandClient client = new() { ResponseValidation = true };

Or configure a single method call using WithOptions:

using System;

var response = await client
    .WithOptions(options =>
        options with { ResponseValidation = true }
    )
    .Sessions.Act(parameters);

Console.WriteLine(response);

Semantic versioning

This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions:

  1. Changes to library internals which are technically public but not intended or documented for external use. (Please open a GitHub issue to let us know if you are relying on such internals.)
  2. Changes that we do not expect to impact the vast majority of users in practice.

We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.

We are keen for your feedback; please open an issue with questions, bugs, or suggestions.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 0 1/16/2026