> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trdrs.co/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

> Call every route with typed methods generated from the API contract, instead of writing your own client.

Call the API from TypeScript with typed methods instead of hand-written requests. Use this page
when your server or script is written in TypeScript or JavaScript.

`@trdrs/sdk` is generated from the same OpenAPI document this site documents, so it has a method
for every route and can't describe a route the engine doesn't serve. It has no dependencies and
runs in Node and in the browser, though a key never belongs in browser code. By the end you have
a client, a first call, a stream and refusals handled by name.

## Install it

The SDK isn't on npm yet, so build it from its repository:

```bash theme={null}
git clone https://github.com/Trdrsco/trdrs-sdk.git
cd trdrs-sdk && npm install && npm run build
```

Then add the built package to your project from that folder.

## Create a client

Create one client when your server starts and reuse it. Pass the keys your server holds, from its
secret environment.

```ts theme={null}
import { Trdrs } from '@trdrs/sdk'

const client = new Trdrs({
  apiKey: process.env.TRDRS_TRADING_API_KEY,
  partnerKey: process.env.TRDRS_PARTNER_KEY, // only for Connect pre-registration and the legacy firm routes
})
```

Each call picks the right key from the contract, so a trading call sends the Trading API key and a
Connect call sends the Partner key without you choosing.

## Make a call

Methods are grouped the way the [API reference](/api-reference/market-data/get-price-history)
groups routes, and named after each route's title. Parameters and the request body go in one
object.

```ts theme={null}
const history = await client.marketData.getPriceHistory({ instrument: 'ESZ6', tf: '5m' })

const order = await client.trading.placeAnOrder({
  instrument: 'ESZ6',
  side: 'buy',
  qty: 1,
  orderType: 'market',
})
console.log(order.clientOrderId) // generated for you, starting with sdk-
```

When you leave out `clientOrderId`, the SDK makes one, so a retry of the same call can't place a
second order. When your app retries on its own, mint the id yourself and pass it every time.

Each method carries the route's description in its documentation comment, so your editor shows
the rules while you write the call.

## Read a stream

A stream is an async iterator. It starts with the whole current picture on every connect, then
sends changes.

```ts theme={null}
for await (const event of client.marketData.streamLiveBars({ instrument: 'ESZ6', tf: '1m' })) {
  // a snapshot arrives first on every connect, then live bars
}
```

[Stream live data](/guides/stream-live-data) explains the events.

## Handle refusals by name

A refusal throws an error class named after it, so your code tells a locked account from a
failure without reading status codes.

```ts theme={null}
import { TrdrsRiskLockedError, TrdrsRateLimitedError } from '@trdrs/sdk'

try {
  await client.trading.placeAnOrder({ instrument: 'ESZ6', side: 'buy', qty: 1, orderType: 'market' })
} catch (err) {
  if (err instanceof TrdrsRiskLockedError) return showLockBanner()
  if (err instanceof TrdrsRateLimitedError) return retryAfter(err.retryAfterSeconds)
  throw err
}
```

A risk lock is a state to show, not an error to retry. [Errors](/api/errors) lists every refusal.

## Stay in step with the API

The generated methods and types are committed to the repository, so nothing is downloaded when you
build. When the engine adds a route, `npm run generate` writes the new methods from the served
contract, and the SDK's own test fails if the committed code ever differs from the contract.

## Next steps

<Columns cols={2}>
  <Card title="Place your first order" icon="rocket" href="/guides/place-your-first-order">
    Follow a whole order from risk check to flatten, with curl and the SDK side by side.
  </Card>

  <Card title="Build a front end" icon="display-code" href="/guides/build-a-front-end">
    Use the SDK on your server behind your own trading screens.
  </Card>
</Columns>
