Skip to content

trading212.js

A TypeScript client for the Trading 212 Public API that is safe around real money, tells you how to fix what went wrong, and comes with a fake Trading 212 to test your code against.
import { Trading212 } from "@kkkermit/trading212.js";
const t212 = new Trading212({ environment: "demo", apiKey, apiSecret });
const { totalValue, holdings } = await t212.portfolio.snapshot();
const apple = await t212.instruments.resolve("Apple");
const order = await t212.orders.buy({ ticker: apple.ticker, quantity: 0.5 });
await t212.orders.waitFor(order); // FILLED

A live client cannot trade by accident

It only reads until you pass trading: "enabled". Orders are never retried, and a request that may or may not have run throws OrderOutcomeUnknownError instead of a plain failure.

Errors tell you what to do

Every error is a named class with a hint in plain English. A 403 names the permission your key lacks.

Your key stays secret

Credentials are removed from every log line, error and test recording. Redirects are refused and pagination links are pinned to Trading 212.

Rate limits are handled

Every endpoint has its own budget, generated from the official spec and corrected from the response headers.

Common tasks take one call

Turn “Apple” into AAPL_US_EQ, sell a whole position, wait for a fill, export a year of history, or check whether a market is open.

Test without the network

createFakeTrading212() gives you the real client talking to an in-memory Trading 212 that fills orders and fails on demand.

Zero runtime dependencies, about 19 kB minified and compressed, ESM and CommonJS, full types. Works on Node 22 or later, Bun and Deno, from TypeScript or plain JavaScript: every example in these docs has both.