# Stream Trade

Stream trade is a method used to obtain the prices of a specific pair of tokens across multiple decentralized exchanges (DEXs).\
(Currently Available for Ethereum Only)

### Response Schema:&#x20;

| Field    | Description                     |
| -------- | ------------------------------- |
| type     | Type of transaction.            |
| platform | Specific name of  DEX platform. |
| symbol   | Token pair.                     |
| price    | Price of token pair.            |

### Streaming trades:

Users can stream trades 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:&#x20;

```shell
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** :

```shell
#!/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
```

{% hint style="info" %}
Once the file has been saved, the user will need to grant permission for it to be executed. This can be accomplished by executing the following command:
{% endhint %}

```bash
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**:&#x20;

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:

```shell
{"action": "streamTrade"}
```

{% hint style="info" %}
Make sure you are connected to the WebSocket before you perform the following queries.&#x20;
{% endhint %}

**Applying a filter in the query**:&#x20;

Users can apply the following filters in their query:-

* token0
* token1

**The above filters can be applied in any combination, using the following syntax**:&#x20;

```shell
{"action": "streamTrade", "token0": "token_address", "token1": "token_address" }
```

**Sample Query**:

```shell
{"action": "streamTrade", "token0": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", "token1": "0xdAC17F958D2ee523a2206206994597C13D831ec7" }
```

#### Example 2. via javascript code

First, the user needs to run the following command in their terminal to install wscat in their system:&#x20;

```shell
npm i ws
```

After installing ws, the user needs to run the following sample code on their device to start streaming:&#x20;

**Client Side Sample Socket Code** :

```javascript
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: "streamTrade",
    token0: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
    token1: "0xdAC17F958D2ee523a2206206994597C13D831ec7"
  };
  ws.send(JSON.stringify(payload));
});


ws.on('message', (data) => {
  console.log(`Received data: ${data}`);
});

ws.on('close', () => {
  console.log('WebSocket connection closed.');
});
```

{% hint style="info" %}
The payload can be modified, depending on the action.
{% endhint %}

### Expected Output:

```javascript
{
  "type": "DEX",
  "platform": "UniswapV2",
  "symbol": "ETHUSDT",
  "price": "1731798672"
}
```

### How to disconnect:&#x20;

To disconnect from our API, enter `ctrl-c`  in the connected console.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.expand.network/how-to/stream-data-websocket/stream-trade.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
