> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sportsxapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Get started with the SportsX C# SDK

> Add STX.Sdk to a .NET 8 project, register services with one DI call, authenticate against staging, and place your first limit order.

This guide walks you from an empty .NET 8 console project to a working SportsX integration: install the NuGet package, wire up dependency injection, authenticate against staging, fetch live markets, and place a resting limit order.

<Warning>
  Use the staging endpoints while you are developing. Switching to the production URLs routes requests to the live exchange and trades with real balances. Do not point a new integration at production until you have fully tested it on staging.
</Warning>

## Before you begin

* .NET 8 SDK installed (`dotnet --version` should print `8.x`)
* A SportsX account that can log in to [app-staging.on.sportsxapp.com](https://app-staging.on.sportsxapp.com)

## Walkthrough

<Steps>
  <Step title="Create a project and install the SDK">
    Create a new console project and add the `STX.Sdk` NuGet package along with the hosting package needed for dependency injection:

    ```bash theme={null}
    dotnet new console -n SportsXQuickstart
    cd SportsXQuickstart
    dotnet add package STX.Sdk
    dotnet add package Microsoft.Extensions.Hosting
    ```

    Confirm the package resolved correctly:

    ```bash theme={null}
    dotnet list package | grep STX.Sdk
    ```

    You should see `STX.Sdk` listed with its version number.
  </Step>

  <Step title="Register STX services">
    `STX.Sdk` integrates with the standard .NET DI container through a single extension method, `ConfigureSTXServices`. It registers every service the SDK provides — GraphQL clients, Phoenix channel classes, user storage, and background workers for session keep-alive and geolocation.

    Replace `Program.cs` with the following:

    ```csharp Program.cs theme={null}
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using STX.Sdk;
    using STX.Sdk.Services;

    var host = Host.CreateDefaultBuilder(args)
        .ConfigureServices(services =>
        {
            services.ConfigureSTXServices(
                graphQLUri:  "https://in-api-staging.stxapp.io/graphiql",
                channelsUri: "wss://in-api-staging.stxapp.io/socket/websocket?token={0}&vsn=2.0.0");
        })
        .Build();

    var login  = host.Services.GetRequiredService<STXLoginService>();
    var market = host.Services.GetRequiredService<STXMarketService>();

    await login.LoginAsync(
        email:    Environment.GetEnvironmentVariable("STX_EMAIL")!,
        password: Environment.GetEnvironmentVariable("STX_PASSWORD")!);

    var resp = await market.GetMarketInfosAsync();

    Console.WriteLine($"Got {resp.MarketInfos.Count} markets");
    foreach (var m in resp.MarketInfos.Take(5))
    {
        Console.WriteLine($"  {m.MarketId}  {m.Status,-10}  {m.Title}");
    }
    ```

    <Note>
      The `{0}` placeholder in `channelsUri` is intentional — the SDK substitutes your JWT before opening the WebSocket connection.
    </Note>
  </Step>

  <Step title="Set credentials and run">
    Export your staging account credentials, then run the project:

    <CodeGroup>
      ```bash macOS / Linux theme={null}
      export STX_EMAIL="you@example.com"
      export STX_PASSWORD="your-password"
      dotnet run
      ```

      ```powershell Windows (PowerShell) theme={null}
      $env:STX_EMAIL = "you@example.com"
      $env:STX_PASSWORD = "your-password"
      dotnet run
      ```
    </CodeGroup>

    Expected output:

    ```
    Got 217 markets
      mkt_8f3...  open        Who wins Game 4 of the NBA Finals?
      mkt_a12...  open        LAL @ BOS moneyline
      ...
    ```

    <AccordionGroup>
      <Accordion title="STXWrongCredentialsException">
        Your email or password is incorrect for the staging environment. Staging and production accounts are separate — confirm you can log in at [app-staging.on.sportsxapp.com](https://app-staging.on.sportsxapp.com) with the same credentials.
      </Accordion>

      <Accordion title="HttpRequestException">
        The endpoint URL is unreachable. Check that `graphQLUri` is set to the staging URL above and that your network allows outbound HTTPS to `in-api-staging.stxapp.io`.
      </Accordion>
    </AccordionGroup>
  </Step>

  <Step title="Place a resting order and cancel it">
    After the market fetch, pick an open market and submit a small limit order. The price of 10¢ is deliberately low so the order rests in the book without matching, letting you test the full placement and cancellation flow without taking a real position.

    Add the following after your market-fetch code in `Program.cs`:

    ```csharp theme={null}
    using STX.Sdk.Enums;

    var orderService  = host.Services.GetRequiredService<STXOrderService>();
    var targetMarket  = resp.MarketInfos.First(m => m.Status == STXMarketStatus.open);

    var order = await orderService.ConfirmOrderAsync(
        price:              10,                      // cents
        quantity:           1,
        marketId:           targetMarket.MarketId,
        action:             STXOrderAction.buy,
        orderType:          STXOrderType.limit,
        cancelOnDisconnect: false);

    Console.WriteLine($"Placed:    {order.Id} at {order.Price}c × {order.Quantity}");

    await orderService.CancelOrderAsync(order.Id);
    Console.WriteLine($"Cancelled: {order.Id}");
    ```

    Run the project again — you should see both the placed and cancelled order IDs printed to the console.

    <Tip>
      Set `cancelOnDisconnect: true` to have the exchange automatically cancel the order if your process loses its WebSocket connection. This requires the active orders channel to be joined first. See [Trading](/sdks/csharp/trading) for details.
    </Tip>
  </Step>
</Steps>

## What you just did

* Created a .NET 8 console project and installed `STX.Sdk` from NuGet.
* Registered all SDK services in one call with `ConfigureSTXServices`, pointing at the staging endpoints.
* Authenticated with `STXLoginService.LoginAsync` — the token is automatically stored in `STXUserStorage` and attached to every subsequent request.
* Fetched live market data with `STXMarketService.GetMarketInfosAsync`.
* Placed and cancelled a resting limit order using `STXOrderService`.

## Next steps

<CardGroup cols={2}>
  <Card title="Authentication" icon="key" href="/sdks/csharp/authentication">
    2FA, keep-session-alive background service, and manual token refresh.
  </Card>

  <Card title="Configuration" icon="sliders" href="/sdks/csharp/configuration">
    Per-environment endpoints, pulling URIs from `appsettings.json`, and DI tips.
  </Card>

  <Card title="Trading" icon="arrows-rotate" href="/sdks/csharp/trading">
    Cancel-all, batch orders, and cancel-on-disconnect.
  </Card>

  <Card title="WebSockets" icon="bolt-lightning" href="/sdks/csharp/websockets">
    Subscribe to portfolio, order status, trade tape, and market updates.
  </Card>
</CardGroup>
