Time SeventeenDocs

Quickstart

A dependency-free Node.js client that links a key, reads the order book and your account, and places and cancels an order on testnet.

This walk-through uses Node.js 22 and only its built-in crypto. The same steps work in any language with Ed25519 and little-endian integers; the byte layouts and test vectors are in the API reference.

Before you start

You need a testnet account number and a one-time invite. See Testnet access.

1. The client library

Save as t17.mjs. It produces exactly the bytes the exchange verifies (checked against the exchange’s own test vectors).

t17.mjs
// Minimal Time Seventeen client: Node.js 22+, no dependencies.
import crypto from "node:crypto";

// ---- keys ------------------------------------------------------------------
// A new key: crypto.generateKeyPairSync("ed25519"). From a 32-byte seed:
export function keyFromSeed(seedHex) {
  const der = Buffer.concat([Buffer.from("302e020100300506032b657004220420", "hex"), Buffer.from(seedHex, "hex")]);
  const privateKey = crypto.createPrivateKey({ key: der, format: "der", type: "pkcs8" });
  return { privateKey, publicKeyHex: publicKeyHex(crypto.createPublicKey(privateKey)) };
}
export function publicKeyHex(publicKey) {
  return Buffer.from(publicKey.export({ format: "jwk" }).x, "base64url").toString("hex");
}
const sign = (privateKey, bytes) => crypto.sign(null, bytes, privateKey); // 64 bytes

// ---- little-endian writer --------------------------------------------------
class Out {
  b = [];
  u8(v) { this.b.push(v & 0xff); return this; }
  u16(v) { this.b.push(v & 0xff, (v >>> 8) & 0xff); return this; }
  u32(v) { for (let i = 0; i < 4; i++) this.b.push((v >>> (8 * i)) & 0xff); return this; }
  u64(v) { v = BigInt(v); for (let i = 0n; i < 8n; i++) this.b.push(Number((v >> (8n * i)) & 0xffn)); return this; }
  bytes(x) { for (const v of x) this.b.push(v); return this; }
  done() { return Buffer.from(this.b); }
}

// ---- commands (journal encoding) -------------------------------------------
const TIF = { gtc: 0, ioc: 1, fok: 2 };
/** New limit order. price: own-outcome ticks (1 tick = $0.000001); qty: contracts. */
export function newOrder({ id, account, market, outcome, side, tif = "gtc", price, qty, postOnly = false, reduceOnly = false }) {
  const flags = (outcome === "no" ? 1 : 0) | (side === "sell" ? 2 : 0) | (postOnly ? 4 : 0) | (reduceOnly ? 8 : 0);
  return new Out().u8(10).u64(id).u32(account).u32(market).u8(flags)
    .u8(0).u32(0).u32(0)        // kind 0 = limit (no trigger, no offset)
    .u8(TIF[tif]).u64(0)        // time in force, GTD time
    .u32(price).u64(qty)
    .u64(0)                     // display quantity: 0 = all visible
    .u64(0)                     // OCO sibling: none
    .done();
}
export const cancel = (account, id) => new Out().u8(11).u32(account).u64(id).done();
export const cancelAll = (account, market = null) => new Out().u8(13).u32(account).u64(market == null ? 0 : BigInt(market) + 1n).done();

/** Order id: your account in the high 32 bits, any u32 of yours in the low 32. */
export const orderId = (account, local) => (BigInt(account) << 32n) | BigInt(local >>> 0);
/** Nonce: lane (0-255) in the top 8 bits; a counter you never reuse within the lane. */
export const nonce = (lane, counter) => (BigInt(lane) << 56n) | BigInt(counter);

// ---- signed frame ----------------------------------------------------------
export function frame(privateKey, account, nonceValue, body) {
  const message = new Out().bytes(Buffer.from("T17-ORDER/2")).u32(account).u64(nonceValue).bytes(body).done();
  const sig = sign(privateKey, message);
  return new Out().u32(account).u16(body.length).u64(nonceValue).bytes(body).bytes(sig).done();
}

/** Headers of a signed account read (valid for 30 s). */
export function accountHeaders(privateKey, account, unixMs = Date.now()) {
  const sig = sign(privateKey, Buffer.from(`T17-FAST-ACCOUNT/1|${account}|${unixMs}`));
  return { "x-t17-ts": String(unixMs), "x-t17-signature": sig.toString("hex") };
}
setup.mjs
import crypto from "node:crypto";
import fs from "node:fs";
import { publicKeyHex } from "./t17.mjs";

const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519");
fs.writeFileSync("t17-key.pem", privateKey.export({ format: "pem", type: "pkcs8" }), { mode: 0o600 });

const res = await fetch("https://testnet.timeseventeen.com/api/gw/v2/enroll", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ account: 1001, invite: process.env.T17_INVITE, public_key: publicKeyHex(publicKey) }),
});
console.log(res.status, await res.json()); // 200 { registered: true }

Replace 1001 with your account number and set T17_INVITE. The private key never leaves t17-key.pem.

3. Read the market and your account

read.mjs
import crypto from "node:crypto";
import fs from "node:fs";
import { accountHeaders } from "./t17.mjs";

const API = "https://api-testnet.timeseventeen.com";
const ACCOUNT = 1001;
const key = crypto.createPrivateKey(fs.readFileSync("t17-key.pem"));

const book = await (await fetch(`${API}/v2/book/1`, { headers: { "x-t17-depth": "5" } })).json();
console.log("best bid", book.bids[0], "best ask", book.asks[0]); // [price ticks, contracts]

const account = await (await fetch(`${API}/v2/account/${ACCOUNT}`, { headers: accountHeaders(key, ACCOUNT) })).json();
console.log("cash $", (account.balance.cash - account.balance.reserved) / 1e6, "positions", account.positions);

4. Place and cancel an order

trade.mjs
import crypto from "node:crypto";
import fs from "node:fs";
import { newOrder, cancel, frame, orderId, nonce } from "./t17.mjs";

const API = "https://api-testnet.timeseventeen.com";
const ACCOUNT = 1001;
const key = crypto.createPrivateKey(fs.readFileSync("t17-key.pem"));
const LANE = 2; // your own nonce lane for this program (the web terminal uses lane 1)
let counter = Date.now() * 1000;

async function send(body) {
  const res = await fetch(`${API}/v2/orders`, {
    method: "POST",
    headers: { "content-type": "application/octet-stream" },
    body: frame(key, ACCOUNT, nonce(LANE, ++counter), body),
  });
  return (await res.json()).results[0];
}

// Buy 10 YES in market 1 at $0.25, resting until cancelled.
const id = orderId(ACCOUNT, Math.floor(Date.now() / 1000));
console.log(JSON.stringify(await send(newOrder({ id, account: ACCOUNT, market: 1, outcome: "yes", side: "buy", tif: "gtc", price: 250_000, qty: 10 }))));
// {"Applied":{"seq":…,"events":[{"Accepted":{…}},{"Book":{…}}, …]}}

console.log(JSON.stringify(await send(cancel(ACCOUNT, id))));
// {"Applied":{"seq":…,"events":[{"Done":{"id":…,"reason":"Cancelled"}}, …]}}

A result is either {"Applied": {seq, events}} — the command was sequenced; its events say what happened, including Rejected with a reason — or {"Refused": "<reason>"} — it never reached the book (bad signature, replayed nonce, rate limit…). See Errors.

Next

  • Subscribe to pushes instead of polling: WebSocket.
  • Market orders, NO orders and sizing in dollars: Trading.