> ## 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.

# Configure the SportsX C# SDK and DI services

> Register STX.Sdk with one DI extension method, supply environment-specific endpoints via appsettings, and wire up logging and geolocation.

`STX.Sdk` integrates with the standard .NET dependency-injection container through a single extension method. One call registers every HTTP service, WebSocket channel, background worker, and storage singleton the SDK provides. You then resolve services by their type anywhere in your app.

## Register services

Call `ConfigureSTXServices` during host setup, passing the GraphQL HTTP endpoint and the Phoenix WebSocket endpoint for your target environment:

```csharp theme={null}
using STX.Sdk;
using Microsoft.Extensions.DependencyInjection;

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");
```

<Note>
  `{0}` in `channelsUri` is a format placeholder. The SDK substitutes the current JWT automatically before opening the socket — you never need to interpolate it yourself.
</Note>

`ConfigureSTXServices` adds:

* **GraphQL HTTP client** and all typed services: `STXLoginService`, `STXMarketService`, `STXOrderService`, `STXTradeService`, `STXSettlementService`, `STXProfileService`, and more.
* **Phoenix channel wrappers**: `STXPortfolioChannel`, `STXActiveOrdersChannel`, `STXActiveTradesChannel`, `STXPositionsChannel`, `STXMarketChannel`, and others.
* **`STXUserStorage`** as a singleton to hold the JWT between calls.
* **Two hosted background services**: `STXSessionBackgroundService` (JWT refresh) and `STXGeoLocationBackgroundService` (geolocation token). These start automatically under `IHost`.

## Endpoints per environment

| Environment | `graphQLUri`                                | `channelsUri`                                                         |
| ----------- | ------------------------------------------- | --------------------------------------------------------------------- |
| Staging     | `https://in-api-staging.stxapp.io/graphiql` | `wss://in-api-staging.stxapp.io/socket/websocket?token={0}&vsn=2.0.0` |
| Production  | `https://in-api.stxapp.io/graphiql`         | `wss://in-api.stxapp.io/socket/websocket?token={0}&vsn=2.0.0`         |

Pull these from configuration so deploys can switch environments without recompiling:

```csharp theme={null}
services.ConfigureSTXServices(
    graphQLUri:  builder.Configuration["STX:GraphQLUri"]!,
    channelsUri: builder.Configuration["STX:ChannelsUri"]!);
```

`appsettings.json`:

```json theme={null}
{
  "STX": {
    "GraphQLUri":  "https://in-api-staging.stxapp.io/graphiql",
    "ChannelsUri": "wss://in-api-staging.stxapp.io/socket/websocket?token={0}&vsn=2.0.0"
  }
}
```

Override per environment with `appsettings.Production.json`, or use environment variables (double-underscore as separator):

```bash theme={null}
STX__GraphQLUri=https://in-api.stxapp.io/graphiql
STX__ChannelsUri=wss://in-api.stxapp.io/socket/websocket?token={0}&vsn=2.0.0
```

## Dynamic endpoint resolution

If your endpoints depend on something inside the DI container — a tenant selector, feature flag service, or remote config provider — use the overload that takes `Func<IServiceProvider, string>` callbacks:

```csharp theme={null}
services.ConfigureSTXServices(
    getGraphQLUri:  sp => sp.GetRequiredService<IMyEnvResolver>().GraphQLUri,
    getChannelsUri: sp => sp.GetRequiredService<IMyEnvResolver>().ChannelsUri);
```

## Resolving services

Services are registered as `Transient`, so it's safe to resolve a new instance per operation or inject them into scoped/transient components. Channel classes are registered as `Singleton` — the active socket and subscribed topics are shared across the app.

Inject them through the constructor as usual:

```csharp theme={null}
public class OrderPlacer
{
    private readonly STXOrderService _orders;
    private readonly STXMarketService _markets;

    public OrderPlacer(STXOrderService orders, STXMarketService markets)
    {
        _orders  = orders;
        _markets = markets;
    }

    public async Task PlaceAsync() { /* ... */ }
}
```

## Logging

The SDK uses `Microsoft.Extensions.Logging`. Configure the provider you prefer:

```csharp theme={null}
services.AddLogging(builder =>
    builder
        .SetMinimumLevel(LogLevel.Information)
        .AddConsole());
```

Log levels the SDK emits:

| Level         | What gets logged                                              |
| ------------- | ------------------------------------------------------------- |
| `Debug`       | Full GraphQL request and response bodies, including variables |
| `Information` | Connection lifecycle events (connect, disconnect, reconnect)  |
| `Warning`     | Transient failures before retry                               |
| `Error`       | Fatal errors that surface as exceptions                       |

## Geolocation

`ConfigureSTXServices` registers `STXGeoLocationBackgroundService` internally. For server-side trading bots the default behavior is what you want — the geolocation token is fetched lazily and cached in the background. If you embed the SDK in a desktop or mobile client app, integrate with GeoComply's native SDK per their documentation before calling `LoginAsync`.
