Skip to content
FlagSwiftDocs

Integrate with .NET

ASP.NET Core dependency injection and a route attribute.

Before you start#

  • A server key (fs_srv_…) from the API Keys page of your console. See API keys for how keys differ.
  • The address of your FlagSwift server, for example https://flags.example.com.
  • A flag to read. Create one in the console and turn it on in an environment.

Keep the key on the server

Store the key in your secrets manager or an environment variable. Never commit it and never send it to a browser.

Your server#

Enter your server address and, if your key covers every environment of the project, the environment to read. The examples below fill in as you type. Nothing is sent anywhere: the values stay in this browser.

Set up#

  1. Environment

    bash
    FlagSwift__BaseUrl=https://flags.example.com
    FlagSwift__ApiKey=fs_srv_...   # private; from the API Keys page
  2. Install

    bash
    dotnet add package FlagSwift
    dotnet add package FlagSwift.Extensions.DependencyInjection
  3. Set up

    csharp
    builder.Services.AddFlagSwift(o =>
    {
        o.BaseUrl = builder.Configuration["FlagSwift:BaseUrl"]!;   // https://flags.example.com
        o.ApiKey  = builder.Configuration["FlagSwift:ApiKey"]!;
        o.Fallbacks = new Dictionary<string, object?> { ["new-checkout"] = false };
        o.SnapshotPath = "/var/cache/myapp/flagswift.json";         // optional: start offline
    });
  4. Use

    csharp
    app.MapGet("/checkout", (IFlagSwiftClient flags, ClaimsPrincipal user) =>
        flags.IsEnabled("new-checkout", FlagUser.FromClaimsPrincipal(user, accountClaim: "tenant"))
            ? Results.Ok("new") : Results.Ok("old"));
    
    // MVC: returns 404 when the flag is off
    [RequireFlag("new-checkout")]
    public class CheckoutController : Controller { }

Verify it works#

Before you run your app, check the key, the address and the environment with one request. A server key downloads the configuration your app will evaluate locally.

bash
curl -s "https://flags.example.com/api/v1/evaluation/server?mode=config" \
  -H "Authorization: Bearer $FLAGSWIFT_SERVER_KEY"

A 200 response with your flags means everything is wired up. If it fails, Troubleshooting lists the usual causes.

Next steps#