The client
Trading212 is the one class you create. It holds your key, keeps to the rate limits, and gives you every part of
the API as a property.
Creating a client
Section titled “Creating a client”new Trading212(options: Trading212Options)Trading212.fromEnv(overrides?: Partial<Trading212Options>, env?: Record<string, string | undefined>)new Trading212() takes the options directly; environment, apiKey and apiSecret are required, and every other
option is listed in Configuration. Trading212.fromEnv() reads the
environment, credentials from T212_ENV, T212_API_KEY and T212_API_SECRET, and takes any other option as
overrides. Pass env to read from an object instead of process.env.
Both check the options at once and throw ConfigurationError, naming the option at fault, before any request is
made.
import { consoleLogger, MemoryCache, Trading212 } from "@kkkermit/trading212.js";
const t212 = new Trading212({ environment: "demo", apiKey: process.env.T212_API_KEY ?? "", apiSecret: process.env.T212_API_SECRET ?? "",
trading: "enabled", timeoutMs: 15_000, retry: { maxAttempts: 3, baseDelayMs: 500, maxDelayMs: 10_000 }, rateLimit: "pace", maxRateLimitWaitMs: 60_000, logger: consoleLogger({ level: "info" }), hooks: { onResponse: ({ endpoint, status }) => console.log(endpoint, status) }, cache: new MemoryCache(), metadataTtlMs: 60 * 60 * 1000, userAgent: "my-portfolio-bot/1.0",});
console.log(t212.environment, t212.trading); // demo enabledconst { consoleLogger, MemoryCache, Trading212 } = require("@kkkermit/trading212.js");
const t212 = new Trading212({ environment: "demo", apiKey: process.env.T212_API_KEY ?? "", apiSecret: process.env.T212_API_SECRET ?? "",
trading: "enabled", timeoutMs: 15_000, retry: { maxAttempts: 3, baseDelayMs: 500, maxDelayMs: 10_000 }, rateLimit: "pace", maxRateLimitWaitMs: 60_000, logger: consoleLogger({ level: "info" }), hooks: { onResponse: ({ endpoint, status }) => console.log(endpoint, status) }, cache: new MemoryCache(), metadataTtlMs: 60 * 60 * 1000, userAgent: "my-portfolio-bot/1.0",});
console.log(t212.environment, t212.trading);Properties
Section titled “Properties”| Property | Holds |
|---|---|
environment |
"demo" or "live" |
trading |
"enabled" or "disabled": whether this client may place orders |
account |
The account summary |
positions |
Open positions |
portfolio |
The portfolio snapshot |
instruments |
Everything you can trade |
exchanges |
Exchanges and market hours |
orders |
Placing and managing orders |
history |
Past orders, dividends and transactions |
reports |
CSV exports |
watch |
Watchers for orders, positions and cash |
pies |
Pies, deprecated by Trading 212 |
Methods
Section titled “Methods”verify()
Section titled “verify()”verify(options?: CallOptions): Promise<{ environment: Environment; accountId?: number; currency?: string; trading: "enabled" | "disabled" }>Checks the key works with one request, and says which account it belongs to. A wrong key, or one made for the other
environment, fails here with AuthenticationError and a hint saying which. Needs the account permission.
rateLimits()
Section titled “rateLimits()”rateLimits(): Record<EndpointId, { limit: number; periodMs: number; remaining?: number; resetAt?: Date }>What is known about every endpoint’s budget: its limit and period, and what the last response’s headers said was left and when it resets. Every client using the same key in the same process shares these budgets.
toJSON()
Section titled “toJSON()”toJSON(): { environment: Environment; trading: "enabled" | "disabled"; origin: string }What JSON.stringify(t212) gives. The key and secret are kept in private fields, so printing, logging or inspecting
the client never shows them.