Skip to main content

Overview

Authentication is required for most API operations. The Primary API uses token-based authentication via custom headers.

Login Method

Authenticate with username and password to receive an access token.
public async Task<bool> Login(string username, string password)

Parameters

username
string
required
User account for authentication
password
string
required
Password for authentication

Returns

result
bool
Returns true if authentication was successful, false otherwise

Example

using Primary;

var api = new Api(Api.DemoEndpoint);

// Login with credentials
bool success = await api.Login("username", "password");

if (success)
{
    Console.WriteLine("Authentication successful!");
    Console.WriteLine($"Access token: {api.AccessToken}");
}
else
{
    Console.WriteLine("Authentication failed");
}

How It Works

The Login method:
  1. Sends a POST request to auth/getToken
  2. Includes credentials in custom headers (X-Username, X-Password)
  3. Receives the access token in the X-Auth-Token response header
  4. Automatically sets the token for all subsequent requests

Set Access Token

Manually set an authentication token (useful for token refresh or session management).
public void SetAccessToken(string token)

Parameters

token
string
required
The authentication token to use for API requests

Example

// Set a previously obtained token
api.SetAccessToken("your-saved-token-here");

// Now you can make authenticated requests without logging in again
var instruments = await api.GetAllInstruments();

Access Token Property

Retrieve the current authentication token.
public string AccessToken { get; private set; }

Example

// After successful login
await api.Login(Api.DemoUsername, Api.DemoPassword);

// Store the token for later use
string token = api.AccessToken;
Console.WriteLine($"Token: {token}");

// Save to config, database, or secure storage
SaveTokenToStorage(token);

Demo Credentials

For testing and development, the SDK includes built-in demo credentials:
public const string DemoUsername = "naicigam2046";
public const string DemoPassword = "nczhmL9@";
public const string DemoAccount = "REM2046";

Example with Demo Credentials

var api = new Api(Api.DemoEndpoint);

// Login with demo credentials
bool success = await api.Login(Api.DemoUsername, Api.DemoPassword);

if (success)
{
    // Submit orders to demo account
    var order = new Order
    {
        Instrument = instrument,
        Side = Side.Buy,
        Quantity = 100,
        Price = 150.50m,
        Type = Type.Limit,
        Expiration = Expiration.Day
    };
    
    var orderId = await api.SubmitOrder(Api.DemoAccount, order);
}

Authentication Flow

1

Initialize API

Create an Api instance with the desired endpoint (production or demo)
var api = new Api(Api.DemoEndpoint);
2

Login

Call the Login method with your credentials
await api.Login(username, password);
3

Make API Calls

All subsequent API calls will automatically include the authentication token
var instruments = await api.GetAllInstruments();
var positions = await api.GetPositions(accountName);

Endpoint Configuration

var api = new Api(Api.ProductionEndpoint);
// Uses: https://api.primary.com.ar

Security Best Practices

Never hardcode production credentials in your source code. Use environment variables, secure configuration files, or credential managers.
// Good: Load from environment
string username = Environment.GetEnvironmentVariable("PRIMARY_USERNAME");
string password = Environment.GetEnvironmentVariable("PRIMARY_PASSWORD");

await api.Login(username, password);
Store the access token securely after login to avoid repeated authentication calls. The token can be reused across application restarts.

Build docs developers (and LLMs) love