C# implementation for Saxo Bank OpenAPI authentication and real-time streaming with automatic token management.
- .NET 9.0 or later
appsettings.jsonconfigured with own app credentials
- WebSocket Streaming →
streaming/WebSocketSample.cs - Authorization Code Flow →
auth/AuthenticationCodeFlow/AuthSample.cs - Authorization Code with PKCE →
auth/AuthenticationPKCE/PkceAuthSample.cs
- Copy configuration file:
cp appsettings.example.json appsettings.json - Configure
appsettings.jsonwith credentials from developer portal:
{
"AppName": "your_app_name_here",
"AppKey": "your_app_key_here",
"AppSecret": "your_app_secret_here",
"AuthorizationEndpoint": "https://sim.logonvalidation.net/authorize",
"TokenEndpoint": "https://sim.logonvalidation.net/token",
"GrantType": "Code",
"OpenApiBaseUrl": "https://gateway.saxobank.com/sim/openapi",
"RedirectUrls": ["http://localhost:3000/callback"]
}Note:
GrantTypecan be"Code","PKCE", or any other value for manual token entry (case-insensitive)AppSecretis only required for Authorization Code flow (not needed for PKCE)RedirectUrlsis an array; the scripts use the first entry- Streaming URLs are automatically determined from
OpenApiBaseUrl(sim vs live environment)
Automatically handles OAuth authentication and streams real-time price updates:
cd streaming
dotnet runAuthentication Options:
- OAuth Code Flow - Set
GrantType: "Code"in appsettings.json - OAuth PKCE Flow - Set
GrantType: "PKCE"in appsettings.json - Manual Token Entry - Set
GrantTypeto any other value (or omit it)
The streaming sample subscribes to account balance updates and demonstrates:
- Binary WebSocket message parsing
- Control message handling (_heartbeat, _resetsubscriptions, _disconnect)
- Automatic token refresh before expiry
- WebSocket reauthorization with new tokens
- Connection recovery on errors
Port Handling:
- Reads port from first url in
RedirectUrlsproperty in appsettings.json (defaults to 3000 if not specified) - If port is busy, displays error message and stops (user must free the port or update config)
- Authentication: Automatically selects OAuth flow based on
GrantTypesetting"Code"→ Uses Authorization Code Flow with client secret"PKCE"→ Uses PKCE flow (no client secret needed)- Other → Prompts for manual token entry (disables refresh)
- Token Management: Automatically refreshes tokens at 85% of lifetime
- WebSocket Reauth: Reauthorizes active WebSocket connections on token refresh
- Control Message and Error Handling: Reconnects (if possible) on WebSocket errors or control messages.
The OAuth 2.0 flow samples can be run independently to test authentication:
Authorization Code Flow:
cd auth/AuthenticationCodeFlow
dotnet runAuthorization Code with PKCE:
cd auth/AuthenticationPKCE
dotnet runBoth samples:
- Start a local HTTP listener for OAuth callbacks
- Display authorization URL for browser login
- Exchange authorization code for access and refresh tokens
- Automatically refresh the token at 85% of its lifetime in a loop
Register apps at: https://www.developer.saxo/openapi/appmanagement
The app variables are what should populate appsettings.json.