Skip to main content

Getting started

Install​

npm install @altrady/data

@altrady/data ships ES modules and TypeScript types. It works in the browser and in Node.

:::note Formerly coinrayjs This package used to be published as coinrayjs, with the client class Coinray. Both names still work and get the same releases, so existing code keeps running. New code should use @altrady/data and AltradyData. :::

:::info Paid product Altrady Data API access is a paid product — you need an active Altrady Data plan to obtain a token. Real-time streaming and candle history are included; the trading order builders require an additional paid addon on top. Contact Altrady Data to get access. :::

Create a client​

The AltradyData client takes an Altrady Data token. It talks to the Altrady Data gateway (data-gateway.altrady.com), which fronts the REST API, order placement and the WebSocket stream.

import { AltradyData } from "@altrady/data";

// Defaults to the Altrady Data gateway (data-gateway.altrady.com); pass
// { apiEndpoint, orderEndpoint, websocketEndpoint } to override.
const client = new AltradyData(token);

Get a token​

Create an API key in the Altrady Data portal at data.altrady.com/api_keys. The key gives you a client_id and client_secret. Exchange them for a JWT with the client-credentials endpoint:

curl -X POST https://data.altrady.com/oauth/token \
-d client_id=YOUR_KEY_ID \
-d client_secret=YOUR_SECRET
{ "access_token": "eyJ...", "token_type": "Bearer", "expires_in": 2592000 }

Tokens are valid for 30 days on paid plans (trial keys expire 7 days after registration). There is no refresh token: call the endpoint again to get a fresh one. Keep the client_secret on your backend, never in the browser.

A minimal Node helper that does the exchange, e.g. on your backend:

async function fetchFreshToken() {
const res = await fetch("https://data.altrady.com/oauth/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: process.env.ALTRADY_DATA_KEY_ID,
client_secret: process.env.ALTRADY_DATA_SECRET,
}),
});
if (!res.ok) throw new Error(`client token request failed: ${res.status}`);
const { access_token } = await res.json();
return access_token;
}

Token expiry & refresh​

Altrady Data tokens expire. Register a refresh callback and the client will call it when the token is close to expiring, then keep using the new token. Start the periodic check with checkToken().

client.onTokenExpired(async () => {
const newToken = await myBackend.fetchFreshToken(); // see "Get a token" above
return newToken; // the client adopts it automatically
});

client.checkToken(); // begins the expiry watch loop

// You can also push a new token in at any time:
client.refreshToken(newToken);

Connection lifecycle​

The WebSocket connection is opened lazily on the first subscription, but you can manage it explicitly. Subscriptions are automatically re-established on reconnect.

client.onOpen(() => console.log("connected"));
client.onError((e) => console.error("socket error", e));

await client.connect(); // open the socket
client.reconnect(); // drop and reopen
client.disconnect(); // close the socket, keep subscriptions
client.destroy(); // tear everything down (timers, listeners, socket)

:::tip Always clean up Call client.destroy() when your component unmounts or your process shuts down — it clears the token-check interval and the time-offset timer as well as the socket. :::

Using AltradyDataCache for discovery​

If you need the list of exchanges and their markets (and want them kept fresh), use AltradyDataCache. It wraps a AltradyData client and refreshes on an interval.

import { AltradyDataCache } from "@altrady/data";

const cache = new AltradyDataCache(token, {apiEndpoint: "https://data-gateway.altrady.com"});
await cache.initialize();

const exchanges = cache.getExchanges();
const binanceMarkets = cache.getExchange("BINA").markets;
const market = cache.getMarket("BINA_USDT_BTC");

cache.destroy(); // stop refresh timers when done

Next: stream live market data →