# NatsBuilderExtensions Methods

- Package: [Aspire.Hosting.Nats](/reference/api/csharp/aspire.hosting.nats.md)
- Type: [NatsBuilderExtensions](/reference/api/csharp/aspire.hosting.nats/natsbuilderextensions.md)
- Kind: `Methods`
- Members: `6`

Provides extension methods for adding NATS resources to the application model.

## AddNats(IDistributedApplicationBuilder, string, int?)

- Name: `AddNats(IDistributedApplicationBuilder, string, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<NatsServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Nats/NatsBuilderExtensions.cs#L34)

Adds a NATS server resource to the application model. A container is used for local development. This configures a default user name and password for the NATS server.

```csharp
public static class NatsBuilderExtensions
{
    public static IResourceBuilder<NatsServerResource> AddNats(
        this IDistributedApplicationBuilder builder,
        string name,
        int? port)
    {
        // ...
    }
}
```

## 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.
- `port` (`int?`)
  The host port for NATS server.

## Returns

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

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AddNats(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)

- Name: `AddNats(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<NatsServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Nats/NatsBuilderExtensions.cs#L55-L107)

Adds a NATS server resource to the application model. A container is used for local development.

```csharp
public static class NatsBuilderExtensions
{
    public static IResourceBuilder<NatsServerResource> AddNats(
        this IDistributedApplicationBuilder builder,
        string name,
        int? port = null,
        IResourceBuilder<ParameterResource>? userName = null,
        IResourceBuilder<ParameterResource>? password = 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.
- `port` (`int?`) `optional`
  The host port for NATS server.
- `userName` (`IResourceBuilder<ParameterResource>`) `optional`
  The parameter used to provide the user name for the NATS resource. If `null` a default value will be used.
- `password` (`IResourceBuilder<ParameterResource>`) `optional`
  The parameter used to provide the administrator password for the NATS resource. If `null` a random password will be generated.

## Returns

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

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## WithDataBindMount(IResourceBuilder<NatsServerResource>, string, bool)

- Name: `WithDataBindMount(IResourceBuilder<NatsServerResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<NatsServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Nats/NatsBuilderExtensions.cs#L187-L191)

Adds a bind mount for the data folder to a NATS container resource.

```csharp
public static class NatsBuilderExtensions
{
    public static IResourceBuilder<NatsServerResource> WithDataBindMount(
        this IResourceBuilder<NatsServerResource> builder,
        string source,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<NatsServerResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only mount.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataVolume(IResourceBuilder<NatsServerResource>, string?, bool)

- Name: `WithDataVolume(IResourceBuilder<NatsServerResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<NatsServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Nats/NatsBuilderExtensions.cs#L169-L173)

Adds a named volume for the data folder to a NATS container resource.

```csharp
public static class NatsBuilderExtensions
{
    public static IResourceBuilder<NatsServerResource> WithDataVolume(
        this IResourceBuilder<NatsServerResource> builder,
        string? name = null,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<NatsServerResource>`)
  The resource builder.
- `name` (`string?`) `optional`
  The name of the volume. Defaults to an auto-generated name based on the application and resource names.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only volume.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithJetStream(IResourceBuilder<NatsServerResource>, string?)

> **Obsolete:** This method is obsolete and will be removed in a future version. Use the overload without the srcMountPath parameter and WithDataBindMount extension instead if you want to keep data locally.

- Name: `WithJetStream(IResourceBuilder<NatsServerResource>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<NatsServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Nats/NatsBuilderExtensions.cs#L131-L141)

Adds JetStream support to the NATS server resource.

```csharp
public static class NatsBuilderExtensions
{
    public static IResourceBuilder<NatsServerResource> WithJetStream(
        this IResourceBuilder<NatsServerResource> builder,
        string? srcMountPath = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<NatsServerResource>`)
  The resource builder.
- `srcMountPath` (`string?`) `optional`
  Optional mount path providing persistence between restarts.

## Returns

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

## WithJetStream(IResourceBuilder<NatsServerResource>)

- Name: `WithJetStream(IResourceBuilder<NatsServerResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<NatsServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Nats/NatsBuilderExtensions.cs#L153-L155)

Adds JetStream support to the NATS server resource.

```csharp
public static class NatsBuilderExtensions
{
    public static IResourceBuilder<NatsServerResource> WithJetStream(
        this IResourceBuilder<NatsServerResource> builder)
    {
        // ...
    }
}
```

## Parameters

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

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
