Foundation classes for all API interactions.
Main entry point for API operations.
class Context {
constructor(hostname: string, port: number, ssl: boolean, application?: string);
setToken(token: string): void;
request(method: string, path: string, body: object, streamChunkHandler: function, responseHandler: function): void;
// API module properties
account: AccountAPI;
order: OrderAPI;
trade: TradeAPI;
position: PositionAPI;
transaction: TransactionAPI;
pricing: PricingAPI;
instrument: InstrumentAPI;
user: UserAPI;
primitives: PrimitivesAPI;
pricing_common: PricingCommonAPI;
site: SiteAPI;
}Usage:
const { Context } = require('@oanda/v20');
const ctx = new Context('api-fxpractice.oanda.com', 443, true, 'MyApp');
ctx.setToken('token');
ctx.account.list(accountID, callback);HTTP response wrapper with status helpers.
class Response {
method: string;
path: string;
statusCode: string;
statusMessage: string;
contentType: string;
rawBody: string;
body: object;
isSuccess(): boolean; // 2xx
isRedirection(): boolean; // 3xx
isClientError(): boolean; // 4xx
isServerError(): boolean; // 5xx
isError(): boolean; // 4xx or 5xx
}Usage:
ctx.account.get(accountID, (response) => {
if (response.isSuccess()) {
const account = response.body.account;
} else if (response.isClientError()) {
console.error(response.body.errorMessage);
}
});Base class for all API entity classes.
class Definition {
toJSON(): object;
name(): string;
summary(): string;
title(): string;
toString(): string;
fields(): Field[];
}Usage:
const trade = response.body.trade;
console.log(trade.name()); // "Trade 12345"
console.log(trade.summary()); // "1000 (1000) of EUR_USD @ 1.08500"
console.log(trade.toJSON()); // JSON representationclass Property {
constructor(name: string, displayName: string, description: string, typeClass: string, typeName: string);
}
class Field extends Property {
constructor(property: Property, value: any);
}{
account: { /* Account object */ },
lastTransactionID: "12345"
}{
accounts: [ /* Array of Account objects */ ],
lastTransactionID: "12345"
}{
orderCreateTransaction: { /* Transaction object */ },
orderFillTransaction: { /* Transaction object */ },
relatedTransactionIDs: ["12345", "12346"],
lastTransactionID: "12346"
}{
errorCode: "INVALID_UNITS",
errorMessage: "The units specified is invalid"
}Streaming endpoints provide real-time data:
ctx.module.stream(
accountID,
queryParams,
(chunk) => {
const data = JSON.parse(chunk);
if (data.type === 'PRICE') {
// Handle price update
} else if (data.type === 'HEARTBEAT') {
// Keep-alive
}
},
(response) => {
if (response.isError()) {
console.error('Stream error:', response.statusMessage);
}
}
);Available streams:
ctx.pricing.stream() - Real-time pricesctx.transaction.stream() - Real-time transactionsBearer token authentication:
ctx.setToken('your-bearer-token');
// Authorization: Bearer <token>Headers automatically included:
Content-Type: application/json
OANDA-Agent: v20-javascript/3.0.25 (YourApplicationName)
Authorization: Bearer <token>function handleResponse(response, successCallback) {
if (response.isSuccess()) {
successCallback(response.body);
} else if (response.statusCode === '401') {
console.error('Authentication failed');
} else if (response.statusCode === '404') {
console.error('Resource not found');
} else if (response.statusCode === '400') {
console.error('Bad request:', response.body.errorMessage);
} else if (response.isClientError()) {
console.error(`Client error ${response.statusCode}:`, response.body);
} else if (response.isServerError()) {
console.error(`Server error ${response.statusCode}:`, response.statusMessage);
}
}class Instrument extends Definition {
name: string;
type: string;
displayName: string;
pipLocation: number;
displayPrecision: number;
tradeUnitsPrecision: number;
minimumTradeSize: string;
maximumTrailingStopDistance: string;
minimumTrailingStopDistance: string;
maximumPositionSize: string;
maximumOrderUnits: string;
marginRate: string;
commission: InstrumentCommission;
}class InstrumentCommission extends Definition {
commission: string;
unitsTraded: string;
minimumCommission: string;
}class GuaranteedStopLossOrderLevelRestriction extends Definition {
volume: string;
priceRange: string;
}http, https