# Add Feature Flags to a .NET API

Yes. An agent adds Microsoft.FeatureManagement to your ASP.NET Core solution, gates an endpoint on a configuration flag, and opens a PR with a passing build.

[Run this on Rush](https://cloud.getrush.ai/run/add-feature-flags/dotnet/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the solution is wired before it adds a package, because a Clean Architecture template has one right place for each piece. In a solution like jasontaylordev/CleanArchitecture, `global.json` pins SDK `10.0.403`, `Directory.Build.props` sets `net10.0`, `TreatWarningsAsErrors` and `Nullable`, and `Directory.Packages.props` turns on central package management with `CentralPackageTransitivePinningEnabled`, so every version lives there and the `.csproj` files list bare `PackageReference` items. `src/Web/Program.cs` calls `builder.AddServiceDefaults()`, `AddKeyVaultIfConfigured()`, `AddApplicationServices()`, `AddInfrastructureServices()` and `AddWebServices()`, and `src/Web/DependencyInjection.cs` is where the web-layer services (`AddHttpContextAccessor`, `AddExceptionHandler`, `AddOpenApi`, `AddCors`) are registered. Endpoints are classes implementing `IEndpointGroup` in `src/Web/Endpoints`, mapped by `WebApplicationExtensions.MapEndpoints` under `/api/{ClassName}`; `WeatherForecasts.cs` maps one `GET` behind `RequireAuthorization()` that sends `GetWeatherForecastsQuery` through MediatR. `appsettings.json` holds only `ConnectionStrings`, `Logging` and `AllowedHosts`.

The agent adds `<PackageVersion Include="Microsoft.FeatureManagement.AspNetCore" ... />` to `Directory.Packages.props` and a versionless `<PackageReference>` to `src/Web/Web.csproj`, then `builder.Services.AddFeatureManagement()` in `AddWebServices`, which binds the `FeatureManagement` section of configuration. `appsettings.json` gains that section with `"WeatherForecasts": true`, and `appsettings.Development.json` is left alone so the flag is read the same way in every environment and can be flipped with the environment variable `FeatureManagement__WeatherForecasts=false` without a rebuild. Flag names go in one static class, `FeatureFlags`, under `src/Web`, so endpoints never repeat a string.

The gate itself sits on the endpoint. `GetWeatherForecasts` takes an `IFeatureManager` alongside `ISender`, returns `Results<Ok<IEnumerable<WeatherForecast>>, NotFound>`, and answers `TypedResults.NotFound()` when `IsEnabledAsync(FeatureFlags.WeatherForecasts)` is false, so a disabled feature looks like a route that does not exist rather than a server error. Authorization still runs first because `RequireAuthorization()` is on the group, so an anonymous caller gets 401 whether or not the flag is on, which the agent points out in the pull request. The OpenAPI document from `MapOpenApi()` keeps listing the route, and the description says how to add a document transformer to hide it if the team prefers.

To prove it, the agent runs `dotnet restore`, `dotnet build --configuration Release` (warnings are errors here, so a stray nullable warning would fail) and `dotnet test` for `Domain.UnitTests` and `Application.UnitTests`, which need no database. It then starts `src/Web` against the SQLite connection string in `appsettings.json`, registers a user through the `MapIdentityApi` endpoints under `/api/Users`, logs in for a bearer token, and calls `/api/WeatherForecasts` twice: once with the flag on, once with `FeatureManagement__WeatherForecasts=false` in the environment, and records the 200 and the 404. `tests/Application.FunctionalTests` and `Web.AcceptanceTests` start an Aspire app host, so the agent leaves them to CI and says so.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add feature flags to this ASP.NET Core solution with Microsoft.FeatureManagement. Read global.json, Directory.Build.props, Directory.Packages.props, src/Web/Web.csproj, src/Web/Program.cs, src/Web/DependencyInjection.cs, the endpoint classes in src/Web/Endpoints and appsettings.json first. Add the package through central package management, register AddFeatureManagement in the web layer's DependencyInjection, add a FeatureManagement section to appsettings.json, and put flag names in one static class. Gate the WeatherForecasts GET endpoint: inject IFeatureManager, return NotFound when the flag is off, and keep RequireAuthorization as it is. Do not change the Application or Domain projects. Run dotnet build in Release (warnings are errors) and the unit test projects that need no database. Start the Web project with the SQLite connection string, register and log in through the identity endpoints, and show the endpoint answering 200 with the flag on and 404 with it off via the environment variable. Open a pull request that lists what you ran.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `Directory.Packages.props` (one `PackageVersion`), `src/Web/Web.csproj` (one `PackageReference`), `src/Web/DependencyInjection.cs` (`AddFeatureManagement()`), `src/Web/appsettings.json` (the `FeatureManagement` section) and `src/Web/Endpoints/WeatherForecasts.cs` (the gate), and adds `src/Web/FeatureFlags.cs`. `Program.cs`, the Application and Domain projects, the Angular and React client apps and the test projects are untouched.

The description quotes the Release build result, the unit test summary, and the two `curl` calls against the running Web project with their status codes, and names the environment variable that flips the flag. The commands and their full output are in the transcript at `GET /o/{org}/sessions/{id}/messages`; the repository's `build.yml` workflow runs the whole test suite on the pull request.

Before merging, a reviewer decides whether a disabled feature should answer 404 or 503, which other endpoints should be gated, and whether flags should move to Azure App Configuration later. A follow-up message to the same session can gate another endpoint or add a percentage filter.

## Run it on every Linear issue labelled feature-flag

Set up an automation with a Linear trigger on the `feature-flag` label and this prompt, scoped to the Project that holds the repository. The trigger appends the issue to the prompt, so an issue that says which endpoint to gate and what the flag is called starts a run that does exactly that, in its own sandbox, and opens its own pull request.

To start it from your own server instead, send `POST /o/{org}/sessions` with the repository in `resources` and the issue URL as a second resource, and a `request_id` that is a UUID v5 of the issue identifier so a retried call returns the run already started. The key's `webhook_url` reports when each run the key starts finishes.

## Why run it on Rush?

Wiring a feature flag library is a small change that has to land in four files that all have to agree, in a solution where warnings fail the build and central package management refuses a version in the wrong place. The agent reads the conventions first and follows them, builds in Release, and keeps the output.

The pull request includes the running proof: a registered user, a token, and the endpoint answering 200 and then 404 when the environment variable flips. A reviewer reads the diff and the two status lines rather than cloning the solution, and the same prompt can gate endpoints across several services at once.

## Frequently asked questions

### Why return 404 instead of an error when the flag is off?

A feature that is off should look absent to the caller, and 404 is what a route that does not exist returns. If you prefer 503 or a problem-details body, the gate is one line in the endpoint and a follow-up message changes it.

### Does the gate run before or after authorization?

After. `RequireAuthorization()` is on the route group, so an anonymous request is rejected with 401 before the handler and its `IFeatureManager` check run. Only an authenticated caller can observe the 200 versus 404 difference.

### Where do flags live in production?

In configuration. The `FeatureManagement` section in `appsettings.json` is the default, so an environment variable such as `FeatureManagement__WeatherForecasts=false` on the host turns the feature off without a build. If the solution's Key Vault configuration is enabled, the same section can be served from there, and Azure App Configuration can be added later for runtime refresh.

### Will this change the Application or Domain layers?

No. The query, its handler and the domain stay as they are; the flag is a web-layer concern and is checked in the endpoint. That keeps the unit tests in `Application.UnitTests` and `Domain.UnitTests` unchanged and passing.

## More .NET jobs

- [Add Mutation Testing to a .NET Solution](https://getrush.ai/run/add-mutation-testing/dotnet/)
- [Add NuGet Vulnerability Auditing to a .NET Solution's CI](https://getrush.ai/run/add-nuget-audit-to-ci/dotnet/)
- [Add NuGet Lock Files to a .NET Solution](https://getrush.ai/run/add-nuget-lock-files/dotnet/)

## More jobs like this: Turn a Linear label into a PR

- [Add Feed Exports to a Scrapy Project](https://getrush.ai/run/add-feed-exports/scrapy/)
- [Add Open and Save File Dialogs to an Electron App from a Linear Issue](https://getrush.ai/run/add-file-dialogs/electron/)
- [Add Native File Dialogs to a Tauri App](https://getrush.ai/run/add-file-dialogs/tauri/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
