Stream Ticker Price
Stream ticker price is a method to obtain the ticker price against USDC. (Currently Available for Ethereum Only)
Response Schema:
symbol
The Symbol of the token or ticker.
timestamp
The current timestamp, which will have the USDC price of the ticker.
Streaming Ticker Price:
Users can stream ticker price in two ways:
Example 1: via wscat script (For Linux based distro)
First, the user needs to run the following command in their terminal to install wscat in their system:
npm install -g wscat
After installing wscat, the user needs to create a shell script and paste the following sample code:
Client Side Sample Socket Code :
#!/bin/bash
echo "Price Discovery\n"
read -p "x-api-key: " YOUR_API_KEY
wscat -c wss://pricediscovery.expand.network -H authorization:secret-token -H x-api-key:$YOUR_API_KEY
chmod +x YOUR_FILE_NAME.sh
After following the above steps, the user needs to run the file. This can be done by entering the filename in the console.
Executing a query:
After executing the aforementioned file(shell script), the user will be prompted to input the appropriate API key.
After entering the API key, the user can subscribe to the following actions:
{"action": "streamTickerPrice"}
Applying a filter in the query:
Users can apply the following filters in their query:-
symbol
The above filters can be applied in any combination, using the following syntax:
{"action":"streamTickerPrice","symbol":"ticker"}
Sample Query:
{"action":"streamTickerPrice","symbol":"REQ"}
Example 2. via javascript code
First, the user needs to run the following command in their terminal to install wscat in their system:
npm i ws
After installing ws, the user needs to run the following sample code on their device to start streaming:
Client Side Sample Socket Code :
const WebSocket = require('ws');
const url = 'wss://pricediscovery.expand.network';
const options = {
headers: {
Authorization: 'secret-token',
'x-api-key': 'YOUR_API_KEY', // Replace this with your API Key
}
};
const ws = new WebSocket(url, options);
ws.on('open', () => {
console.log('WebSocket connection established.');
const payload = {
action: "streamTickerPrice",
symbol: "REQ"
};
ws.send(JSON.stringify(payload));
});
ws.on('message', (data) => {
console.log(`Received data: ${data}`);
});
ws.on('close', () => {
console.log('WebSocket connection closed.');
});
Expected Output:
{
"REQ": {
"1741351111": "0.21764357"
}
}
How to disconnect:
To disconnect from our API, enter ctrl-c
in the connected console.
Last updated