@ton-api/streaming
⚠️ Note that the package is in public beta right now.
TonAPI streaming api SDK
Learn more about the streaming api
Note, that you need to set up an account to utilize TonApi.
Overview
This library allows you to subscribe to the Block, Account, Trace or Mempool events of the TON blockchain in real-time.
Use it to track transactions and accounts changes.
Getting started
Install package npm i @ton-api/streaming
Subscribe to new blocks
import { BlocksObserver, WebsocketStreamProvider, BlockEvent } from '@ton-api/streaming';
const wsProvider = new WebsocketStreamProvider(
`wss://tonapi.io/v2/websocket?token=${YOUR_TONAPI_TOKEN}`
);
await wsProvider.open();
const blockObserver = new BlocksObserver(wsProvider);
const unsubscribeCallback = blockObserver.subscribe(0, (event: BlockEvent) => {
console.log(event);
});
Subscribe to the account events
import { AccountsObserver, WebsocketStreamProvider, AccountEvent } from '@ton-api/streaming';
const wsProvider = new WebsocketStreamProvider(
`wss://tonapi.io/v2/websocket?token=${YOUR_TONAPI_TOKEN}`
);
await wsProvider.open();
const accountsObserver = new AccountsObserver(wsProvider);
const unsubscribeCallback = accountsObserver.subscribe({ account: "-1:5555555555555555555555555555555555555555555555555555555555555555" }, (event: AccountEvent) => {
console.log(event);
});
const unsubscribeCallback2 = accountsObserver.subscribe([
{ account: "-1:5555555555555555555555555555555555555555555555555555555555555555", operations: ['JettonTransfer','0x0524c7ae'] },
{ account: "-1:5555555555555555555555555555555555555555555555555555550000000000" }
],
(event: AccountEvent) => {
console.log(event);
}
);
Subscribe to trace events
import { TraceObserver, WebsocketStreamProvider, TraceEvent } from '@ton-api/streaming';
import { HttpClient, Api } from '@ton-api/client';
const wsProvider = new WebsocketStreamProvider(
`wss://tonapi.io/v2/websocket?token=${YOUR_TONAPI_TOKEN}`
);
await wsProvider.open();
const traceObserver = new TraceObserver(wsProvider);
const unsubscribeCallback = traceObserver.subscribe("-1:5555555555555555555555555555555555555555555555555555555555555555", (event: TraceEvent) => {
console.log(event);
tonapiClient.traces.getTrace(event.hash).then(traceData => {
console.log('Detailed trace data', traceData);
})
});
const httpClient = new HttpClient({
baseUrl: 'https://tonapi.io',
baseApiParams: {
headers: {
Authorization: `Bearer ${YOUR_TOKEN}`,
'Content-type': 'application/json'
}
}
});
const tonapiClient = new Api(httpClient);
Subscribe to mempool events
import { MempoolObserver, WebsocketStreamProvider, MempoolEvent } from '@ton-api/streaming';
const wsProvider = new WebsocketStreamProvider(
`wss://tonapi.io/v2/websocket?token=${YOUR_TONAPI_TOKEN}`
);
await wsProvider.open();
const mempoolObserver = new MempoolObserver(wsProvider);
const unsubscribeCallback = mempoolObserver.subscribe('all', (event: MempoolEvent) => {
console.log(event);
});
const unsubscribeCallback2 = mempoolObserver.subscribe([
"-1:5555555555555555555555555555555555555555555555555555555555555555"
],
(event: MempoolEvent) => {
console.log(event);
}
);