Account API Keys
These API keys are granted on an account-per-account basis and allow access to the account to which the key is bound.
Keys can be whitelisted to certain IP addresses (as specified by the ips field).
Create an Account API Key
Creates a new API key for the specified account.
import { AccountApiKey } from '@tradrapi/trading-sdk';
const key: AccountApiKey = await tradrApi.accounts.createApiKey({
accountId: 1234567890,
friendlyName: 'Reporting Key',
roles: ['read'],
ips: [],
});
The format of the AccountAPIKey
model which is returned as a response from the functionality described below is
as follows:
export enum UserRole {
Read = 'read',
Write = 'write',
Delete = 'delete',
}
interface AccountApiKey {
/** The TradrAPI ID of the entity */
id: number;
/**
* The date the entity was created
* @format date-time
*/
createdAt: string;
/**
* The date the entity was last updated
* @format date-time
*/
updatedAt: string;
/** The roles assigned to the key. Roles determine what actions the key can perform */
roles: UserRole[];
/** The id of the account the API key belongs to */
accountId: number;
/** A friendly name for the API key */
friendlyName?: string;
/** The API key (hashed) */
apiKey: string;
/** The IPs the API key can be used from. Empty array means any IP is allowed */
ips?: string[];
/**
* The last time the key was used
* @format date-time
*/
lastUsedAt?: string;
}
Please note that you will only ever be able to retrieve the API key once, when it is created. If you lose the API key you will need to create a new one.
Using an Account API Key
Once you have created a user API key, you can use it to authenticate your requests to the API.
import { Tradr } from '@tradrapi/trading-sdk';
export const tradrApi = Tradr.make({
// Bootstrap the SDK with your Account API key
auth: { apiKey: 'YOUR_ACCOUNT_API_KEY' }
});
Fetch an Account API Key
Returns a single account API key by its ID.
import { AccountApiKey } from '@tradrapi/trading-sdk';
const key: AccountApiKey = await tradrApi.accounts.getApiKey(32452);
List Account API Keys
Returns a list of all API keys associated with the account.
The method accepts an accountId
as a parameter and returns a list of all account level API keys belonging to the account.
import { AccountApiKey } from '@tradrapi/trading-sdk';
const keys: AccountApiKey[] = await tradrApi.accounts.listApiKeys(101000123);
Update an Account API Key
Allows for the modification of an existing account API key. The key must be owned by the authenticated account.
const result: boolean = await tradrApi.accounts.updateApiKey({
accountApiKeyId: 23122,
friendlyName: 'My renamed key',
});
Deleting an Account API Key
An existing account level API key can be deleted by calling the deleteApiKey
method.
const result: boolean = await tradrApi.accounts.deleteApiKey(23122);
This method accepts an accountApiKeyId
as a parameter and returns a boolean indicating whether the API key was deleted.