# Phemex

Phemex historical data for **all its derivative instruments** is available since **2020-03-17** (for spot markets since 2020-06-04).

{% embed url="<https://api.tardis.dev/v1/exchanges/phemex>" %}
See Phemex historical data coverage: available symbols, channels, date ranges and incidents
{% endembed %}

### Downloadable **CSV** files

Historical CSV datasets for the first day of each month are **available to download without API key**. See [downloadable CSV files documentation](https://docs.tardis.dev/downloadable-csv-files/overview).

| data type             | symbol | date       |                                                                                                       |
| --------------------- | ------ | ---------- | ----------------------------------------------------------------------------------------------------- |
| incremental\_book\_L2 | BTCUSD | 2020-04-01 | [Download sample](https://datasets.tardis.dev/v1/phemex/incremental_book_L2/2020/04/01/BTCUSD.csv.gz) |
| trades                | BTCUSD | 2020-04-01 | [Download sample](https://datasets.tardis.dev/v1/phemex/trades/2020/04/01/BTCUSD.csv.gz)              |
| derivative\_ticker    | BTCUSD | 2020-04-01 | [Download sample](https://datasets.tardis.dev/v1/phemex/derivative_ticker/2020/04/01/BTCUSD.csv.gz)   |

### API Access and data format

Historical data format is the same as provided by real-time Phemex WebSocket API with addition of local timestamps. If you'd like to work with **normalized data format** instead (same format for each exchange) see [downloadable CSV files](https://docs.tardis.dev/downloadable-csv-files/overview) or official [client libs](https://docs.tardis.dev/api/quickstart) that can perform data normalization client-side.

{% tabs %}
{% tab title="Python" %}

```python
# pip install tardis-dev
import asyncio
from tardis_dev import Channel, replay

async def main():
    async for local_timestamp, message in replay(
        exchange="phemex",
        from_date="2024-01-01",
        to_date="2024-01-02",
        filters=[Channel(name="book", symbols=["BTCUSD"])],
        api_key="YOUR_API_KEY",
    ):
        # messages as provided by Phemex real-time stream
        print(message)

asyncio.run(main())
```

See [Python client docs](https://docs.tardis.dev/python-client/quickstart).
{% endtab %}

{% tab title="Node.js" %}

```javascript
// npm install tardis-dev
import { replay } from 'tardis-dev';

const messages = replay({
  exchange: 'phemex',
  from: '2024-01-01',
  to: '2024-01-02',
  filters: [{ channel: 'book', symbols: ['BTCUSD'] }],
  apiKey: 'YOUR_API_KEY'
});

// messages as provided by Phemex real-time stream
for await (const { localTimestamp, message } of messages) {
  console.log(localTimestamp, message);
}
```

See [Node.js client docs](https://docs.tardis.dev/node-client/quickstart).
{% endtab %}

{% tab title="cURL & HTTP API" %}

```bash
curl --compressed -g 'https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01&filters=[{"channel":"book","symbols":["BTCUSD"]}]&offset=0'
```

{% embed url="<https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01&filters=[{%22channel%22:%22book%22,%22symbols%22:[%22BTCUSD%22]}]&offset=0>" %}
Example API response for Phemex historical market data request
{% endembed %}

See [HTTP API docs](https://docs.tardis.dev/api/http-api-reference).
{% endtab %}

{% tab title="cURL & tardis-machine" %}

```bash
curl -g 'localhost:8000/replay?options={"exchange":"phemex","filters":[{"channel":"book","symbols":["BTCUSD"]}],"from":"2024-01-01","to":"2024-01-02"}'
```

[Tardis-machine](https://docs.tardis.dev/tardis-machine/quickstart) is a locally runnable server that exposes API allowing efficiently requesting historical market data for whole time periods in contrast to [HTTP API](https://docs.tardis.dev/api/http-api-reference) that provides data only in minute by minute slices.

See [tardis-machine](https://docs.tardis.dev/tardis-machine/quickstart) docs.
{% endtab %}
{% endtabs %}

### Captured real-time channels

{% embed url="<https://github.com/phemex/phemex-api-docs>" %}
See Phemex WebSocket API docs providing documentation for each captured channel's format
{% endembed %}

{% hint style="info" %}
Click any channel below to see [HTTP API](https://docs.tardis.dev/api/http-api-reference#data-feeds-exchange) response with historical data recorded for it.
{% endhint %}

* [trades](https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01\&filters=\[{%22channel%22:%22trades%22}]) Public trade executions stream
* [trades\_p](https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01\&filters=\[{%22channel%22:%22trades_p%22}]) — available since **2022-11-23** Public perpetual trade executions stream
* [book](https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01\&filters=\[{%22channel%22:%22book%22}]) Order book snapshots and incremental updates
* [orderbook\_p](https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01\&filters=\[{%22channel%22:%22orderbook_p%22}]) — available since **2022-11-23** Perpetual order book snapshots and incremental updates
* [market24h](https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01\&filters=\[{%22channel%22:%22market24h%22}]) 24h market statistics updates
* [perp\_market24h\_pack\_p](https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01\&filters=\[{%22channel%22:%22perp_market24h_pack_p%22}]) — available since **2022-11-23** Perpetual 24h market statistics batch updates
* [spot\_market24h](https://api.tardis.dev/v1/data-feeds/phemex?from=2024-01-01\&filters=\[{%22channel%22:%22spot_market24h%22}]) — available since **2020-06-04** Spot 24h market statistics updates

### Market data collection details

[Market data collection infrastructure](https://docs.tardis.dev/faq/general#what-is-your-infrastructure-setup) for Phemex **since 2020-06-04** is located in GCP asia-northeast1 (Tokyo, Japan), before that it was located in GCP europe-west2 region (London, UK).

Real-time market data is captured via **multiple WebSocket connections** to `wss://vapi.phemex.com/ws`.

{% hint style="info" %}
Phemex servers are located in AWS ap-southeast-1 region (Singapore, Asia Pacific).
{% endhint %}
