T-Invest Node SDK Node.js
On this page

Make related unary gRPC requests

Use this example when your application needs to fetch data in a sequence of unary RPCs with individual deadlines. The application creates one SDK instance, requests accounts, a portfolio, candles, or signals, and passes an AbortSignal to each call.

Each call gets its own AbortSignal. It returns data, fails, or is canceled when its deadline expires. Close the shared channel after the requests finish.

Make requests with one SDK instance

The snippets fetch a portfolio, one-minute candles, and active signals. Each RPC has its own deadline. All snippets share one sdk, which you close at the end.

Create the SDK

Replace YOUR_TOKEN with an access token and set the API address through endpoint. Keep the TInvestNodeSDK instance in sdk for the calls below.

unary-calls.ts
import { TInvestNodeSDK } from '@woodger/t-invest-node-sdk';

const sdk = new TInvestNodeSDK({
  token: 'YOUR_TOKEN',
  endpoint: 'invest-public-api.tbank.ru:443'
});

Get a portfolio for an available account

The example takes the first account from sdk.users.getAccounts() and passes its account.id to sdk.operations.getPortfolio(). When an account is available, it requests the portfolio in rubles.

unary-calls.ts
import { PortfolioRequest_CurrencyRequest } from '@woodger/t-invest-node-sdk';

const { accounts } = await sdk.users.getAccounts(
  {},
  { signal: AbortSignal.timeout(5_000) }
);
const account = accounts[0];

if (account) {
  const portfolio = await sdk.operations.getPortfolio(
    {
      accountId: account.id,
      currency: PortfolioRequest_CurrencyRequest.RUB
    },
    { signal: AbortSignal.timeout(5_000) }
  );

  console.log(portfolio);
}
else {
  console.log('Нет доступных счетов');
}

Get one-minute candles

Pass the instrument ID and the start and end of the last five minutes to sdk.marketData.getCandles(). CandleInterval.CANDLE_INTERVAL_1_MIN sets the candle interval to one minute.

unary-calls.ts
import { CandleInterval } from '@woodger/t-invest-node-sdk';

const to = new Date();
const from = new Date(to.getTime() - 5 * 60 * 1000);
const { candles } = await sdk.marketData.getCandles(
  {
    instrumentId: 'BBG00QPYJ5H0',
    interval: CandleInterval.CANDLE_INTERVAL_1_MIN,
    from,
    to
  },
  { signal: AbortSignal.timeout(5_000) }
);

console.log(candles);

Get active signals

Take the first strategy returned by sdk.signals.getStrategies() and pass its ID to sdk.signals.getSignals(). The second call requests the first page of active signals from the last 24 hours, with up to 20 entries.

unary-calls.ts
import { SignalState } from '@woodger/t-invest-node-sdk';

const { strategies } = await sdk.signals.getStrategies(
  {},
  { signal: AbortSignal.timeout(5_000) }
);
const strategy = strategies[0];

if (strategy) {
  const { signals } = await sdk.signals.getSignals(
    {
      strategyId: strategy.strategyId,
      active: SignalState.SIGNAL_STATE_ACTIVE,
      from: new Date(Date.now() - 24 * 60 * 60 * 1000),
      to: new Date(),
      paging: { limit: 20, pageNumber: 0 }
    },
    { signal: AbortSignal.timeout(5_000) }
  );

  console.log(signals);
}
else {
  console.log('Нет доступных стратегий');
}

Pass options for a single call

TInvestCallOptions applies to one RPC.

  • signal cancels the wait for local unary quota and the subsequent gRPC call.
  • onHeader receives initial response metadata.
  • onTrailer receives final response metadata.

For example, read the limit and remaining quota in the handlers.

unary-calls.ts
await sdk.users.getAccounts({}, {
  signal: AbortSignal.timeout(5_000),
  onHeader(metadata) {
    console.log('Лимит запросов:', metadata.get('x-ratelimit-limit'));
  },
  onTrailer(metadata) {
    console.log('Осталось запросов:', metadata.get('x-ratelimit-remaining'));
    console.log('Сброс квоты:', metadata.get('x-ratelimit-reset'));
  }
});

Metadata handlers run synchronously. If onHeader or onTrailer throws, the SDK rejects the current RPC with the same error and cancels the unfinished gRPC call. Do asynchronous work after the call completes and keep async functions out of the handlers.

AbortSignal.timeout() sets an independent deadline for each call in this example. If several RPCs share a signal, aborting it stops all of them.

The SDK manages the authorization and x-app-name headers for the instance. It merges per-call metadata with instance metadata and protects those two headers from overrides.

Close the SDK after the requests

After the requests finish, run the following.

unary-calls.ts
sdk.close();

In an application, close the SDK in finally as shown in Your first SDK call so the channel is released even if a request fails.

Rate-limit requests

Before retrying a call that returns RESOURCE_EXHAUSTED, consider RPC idempotency, provider metadata, and the delay between attempts. See Errors and lifecycle for an example of checking the error.

By default, the SDK sends unary requests without waiting for quota. See the request rate-limiting guide to enable the built-in limiter and configure quotas.

See Deprecated provider fields for differences between obsolete risk fields and guidance on migrating to current contracts.