# Bun integration

<Image
  src={bunIcon}
  alt="Bun logo"
  width={100}
  height={100}
  class:list={'float-inline-left icon'}
  data-zoom-off
/>

The Aspire Bun hosting integration enables you to run [Bun](https://bun.sh/) applications alongside your other Aspire resources in the app host. Bun apps participate in the same service discovery, health checks, OpenTelemetry export, and Aspire dashboard support as the rest of your solution.

:::note
The `AddBunApp(...)` integration was previously implemented as part of the [📦 CommunityToolkit.Aspire.Hosting.Bun](https://www.nuget.org/packages/CommunityToolkit.Aspire.Hosting.Bun) package but is now built in to `Aspire.Hosting.JavaScript`.
:::

## Hosting integration

To access the Bun hosting APIs in your [`AppHost`](/get-started/app-host/) project, install the [📦 Aspire.Hosting.JavaScript](https://www.nuget.org/packages/Aspire.Hosting.JavaScript) NuGet package:

<InstallPackage packageName="Aspire.Hosting.JavaScript" />

### Add Bun app

Add a Bun application to your app host using the `AddBunApp` extension method:

```csharp title="C# — AppHost.cs"
var builder = DistributedApplication.CreateBuilder(args);

var bunApp = builder.AddBunApp("bun-api", "../bun-app", "server.ts")
    .WithHttpEndpoint(port: 3000, env: "PORT");

builder.AddProject<Projects.ExampleProject>("apiservice")
    .WithReference(bunApp);

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

`AddBunApp` requires:

- **name**: The name of the resource in the Aspire dashboard.
- **appDirectory**: The path to the directory containing your Bun application, relative to the AppHost project.
- **scriptPath**: The script to run relative to `appDirectory`, such as `server.ts`.

### Specify a custom entrypoint

Pass a different `scriptPath` to run a different script:

```csharp title="C# — AppHost.cs"
var builder = DistributedApplication.CreateBuilder(args);

var bunApp = builder.AddBunApp("bun-api", "../bun-app", "src/http/server.ts")
    .WithHttpEndpoint(port: 3000, env: "PORT");

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

### Install packages before startup

When your Bun app includes a `package.json` file, Aspire uses Bun as the package manager and installs packages automatically before the application starts.

### Configure HTTP endpoints

Bun applications typically read the port from an environment variable. Use `WithHttpEndpoint` to declare the HTTP endpoint and bind it to a named environment variable:

```csharp title="C# — AppHost.cs"
var builder = DistributedApplication.CreateBuilder(args);

var bunApp = builder.AddBunApp("bun-api", "../bun-app", "server.ts")
    .WithHttpEndpoint(port: 3000, env: "PORT");

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

Your Bun application reads the `PORT` variable at startup:

```typescript title="TypeScript — server.ts"
const server = Bun.serve({
    port: process.env.PORT || 3000,
    fetch(request) {
        return new Response("Hello from Bun!");
    },
});

console.log(`Server listening on port ${server.port}`);
```

## See also

- [📦 Aspire.Hosting.JavaScript](https://www.nuget.org/packages/Aspire.Hosting.JavaScript)
- [Bun documentation](https://bun.sh/docs)
- [Aspire integrations overview](/integrations/overview/)
- [Aspire GitHub repo](https://github.com/microsoft/aspire)