# K6BuilderExtensions Methods

- Package: [CommunityToolkit.Aspire.Hosting.k6](/reference/api/csharp/communitytoolkit.aspire.hosting.k6.md)
- Type: [K6BuilderExtensions](/reference/api/csharp/communitytoolkit.aspire.hosting.k6/k6builderextensions.md)
- Kind: `Methods`
- Members: `3`

Provides extension methods for adding Grafana k6 resources to the application model.

## AddK6(IDistributedApplicationBuilder, string, bool, int?)

- Name: `AddK6(IDistributedApplicationBuilder, string, bool, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<K6Resource>`
- Source: [GitHub](https://github.com/CommunityToolkit/Aspire/blob/d9dc6fc02412d7398c5722840513d99965a6e98f/src/CommunityToolkit.Aspire.Hosting.k6/K6BuilderExtensions.cs#L49-L63)

Adds a Grafana k6 container resource to the application model. The default image is and the tag is .

```csharp
public static class K6BuilderExtensions
{
    public static IResourceBuilder<K6Resource> AddK6(
        this IDistributedApplicationBuilder builder,
        string name,
        bool enableBrowserExtensions = false,
        int? port = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `enableBrowserExtensions` (`bool`) `optional`
  Enables browser automation and end-to-end web testing to k6. [https://grafana.com/docs/k6/latest/using-k6-browser/](https://grafana.com/docs/k6/latest/using-k6-browser/)
- `port` (`int?`) `optional`
  The host port to bind the underlying container to.

## Returns

`IResourceBuilder<K6Resource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

Add an Grafana k6 container to the application model and reference it in a .NET project.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var api = builder.AddProject<Projects.Api>("api")
var k6 = builder.AddK6("k6");
    .WithReference(api);

builder.Build().Run();
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithK6OtlpEnvironment(IResourceBuilder<K6Resource>)

- Name: `WithK6OtlpEnvironment(IResourceBuilder<K6Resource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<K6Resource>`
- Source: [GitHub](https://github.com/CommunityToolkit/Aspire/blob/d9dc6fc02412d7398c5722840513d99965a6e98f/src/CommunityToolkit.Aspire.Hosting.k6/K6BuilderExtensions.cs#L126-L135)

Set K6 environment variables from the existing OTEL environment set for this resource. See https://grafana.com/docs/k6/latest/results-output/real-time/opentelemetry/#configuration.

```csharp
public static class K6BuilderExtensions
{
    public static IResourceBuilder<K6Resource> WithK6OtlpEnvironment(
        this IResourceBuilder<K6Resource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<K6Resource>`)
  The resource builder.

## Returns

`IResourceBuilder<K6Resource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithScript(IResourceBuilder<K6Resource>, string, int, string)

- Name: `WithScript(IResourceBuilder<K6Resource>, string, int, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<K6Resource>`
- Source: [GitHub](https://github.com/CommunityToolkit/Aspire/blob/d9dc6fc02412d7398c5722840513d99965a6e98f/src/CommunityToolkit.Aspire.Hosting.k6/K6BuilderExtensions.cs#L100-L113)

Runs a k6 JS script when starting the Grafana k6 container resource.

```csharp
public static class K6BuilderExtensions
{
    public static IResourceBuilder<K6Resource> WithScript(
        this IResourceBuilder<K6Resource> builder,
        string scriptPath,
        int virtualUsers = 10,
        string duration = "30s")
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<K6Resource>`)
  The resource builder.
- `scriptPath` (`string`)
  The path to the JS script to run.
- `virtualUsers` (`int`) `optional`
  The number of virtual users for the test. Defaults to `10`.
- `duration` (`string`) `optional`
  The duration of the test. Defaults to `30s`.

## Returns

`IResourceBuilder<K6Resource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

Add a Grafana k6 container to the application model and reference it in a .NET project. Additionally, in this example a script runs when the container starts.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var api = builder.AddProject<Projects.Api>("api");
var k6 = builder.AddK6("k6")
    .WithBindMount("scripts", "/scripts", true)
    .WithScript("/scripts/main.js")
    .WithReference(api)
    .WaitFor(api);

builder.Build().Run();
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
