T-Invest Node SDK Node.js

Run an order lifecycle in the sandbox

The example creates and funds a test account, places a limit order, checks its state, and cancels any active remainder. The finally block closes only the account created by the example.

The example encodes the price of 100.25 rubles as { units: 100, nano: 250_000_000 }. Use a price that is a multiple of the instrument's price increment. The quantity field is a number of lots, and each lot contains instrument.lot shares. The example funds the account in rubles.

sandbox-orders.ts
import { randomUUID } from 'node:crypto';
import {
  OrderDirection,
  OrderExecutionReportStatus,
  OrderIdType,
  OrderType,
  PriceType,
  TInvestNodeSDK
} from '@woodger/t-invest-node-sdk';

const sdk = new TInvestNodeSDK({
  token: 'YOUR_TOKEN',
  endpoint: 'sandbox-invest-public-api.tbank.ru:443'
});
const instrumentUid = 'YOUR_INSTRUMENT_UID';
const quantity = 1;
const price = { units: 100, nano: 250_000_000 };
const initialBalance = { currency: 'rub', units: 100_000, nano: 0 };
const orderRequestId = randomUUID();

try {
  const { accountId } = await sdk.sandbox.openSandboxAccount({
    name: 'SDK example'
  });

  try {
    await sdk.sandbox.sandboxPayIn({ accountId, amount: initialBalance });

    console.log({ accountId, orderRequestId });

    const placed = await sdk.sandbox.postSandboxOrder({
      accountId,
      instrumentId: instrumentUid,
      quantity,
      price,
      direction: OrderDirection.ORDER_DIRECTION_BUY,
      orderType: OrderType.ORDER_TYPE_LIMIT,
      priceType: PriceType.PRICE_TYPE_CURRENCY,
      orderId: orderRequestId
    });
    const order = {
      accountId,
      orderId: placed.orderId,
      orderIdType: OrderIdType.ORDER_ID_TYPE_EXCHANGE,
      priceType: PriceType.PRICE_TYPE_CURRENCY
    };
    let state = await sdk.sandbox.getSandboxOrderState(order);

    if (
      state.executionReportStatus === OrderExecutionReportStatus.EXECUTION_REPORT_STATUS_NEW
      || state.executionReportStatus
        === OrderExecutionReportStatus.EXECUTION_REPORT_STATUS_PARTIALLYFILL
    ) {
      await sdk.sandbox.cancelSandboxOrder({
        accountId,
        orderId: placed.orderId,
        orderIdType: OrderIdType.ORDER_ID_TYPE_EXCHANGE
      });
      state = await sdk.sandbox.getSandboxOrderState(order);
    }

    console.log(state);
  }
  finally {
    await sdk.sandbox.closeSandboxAccount({ accountId });
  }
}
finally {
  sdk.close();
}

The client orderRequestId is sent as orderId when placing an order. Reuse this key when retrying the same instruction. Use placed.orderId from the response to check the order state or cancel it. The SDK leaves retries to the caller.

Only a new or partially filled order can be canceled. The order can fill between the state check and cancellation, in which case the provider rejects the cancellation. The error propagates to the caller and finally still runs. Recheck the order state after this error. The orders contract defines the parameters and statuses.

If you plan to keep using the account, close it at the end of your scenario. You can also stream order state changes.