# Prices

Our API exposes a WebSocket based endpoint which allows partners to subscribe to an incoming\
stream of digital asset prices.

After establishing an HTTPS connection to [`Websocket Prices`](https://docs.tradevest.ai/websockets/websocket-prices/prices#get-websocket-prices) endpoint\
the client is expected to upgrade the connection to WebSocket protocol (a step handled automatically by most WebSocket client libraries).\
After a successful upgrade the client is expected to send a single `PRICE_SUBSCRIPTION` message, specifying which fiat currency/digital asset pair prices are supposed to be streamed. Attribute `markets` is optional and if omitted will indicate that the subscription should include all `EUR` and `cryptoSymbol` pairs supported by the platform.

[`WebSocketRequest`](https://docs.tradevest.ai/websockets/websocket-prices/prices#get-websocket-prices)

```json
{
  "type": "PRICE_SUBSCRIPTION",
  "priceSubscription": {
    "markets": [
      {
        "currency": "EUR",
        "cryptoSymbol": "BTC"
      },
      {
        "currency": "EUR",
        "cryptoSymbol": "ETH"
      }
    ]
  }
}
```

Upon successful processing of the `PRICE_SUBSCRIPTION` message the server will begin to produce `PRICES` messages containing array of sets of Asks and Bids for requested `currency` / `cryptoSymbol` pair.\
This stream of messages will continue until an error is encountered or either the client or server terminate the connection.\
To maintain the session, our prices provider service, server will send `PRICES` message every second with empty `asks` and `bids` arrays in market if no price change.

[`WebSocketResponse`](https://docs.tradevest.ai/websockets/websocket-prices/prices#get-websocket-prices)

```json
{
  "type": "PRICES",
  "prices": [
    {
      "market": {
        "currency": "EUR",
        "cryptoSymbol": "BTC"
      },
      "time": "2024-07-10T08:01:22.783+02:00",
      "asks": [
        {
          "price": "54577.54",
          "quantity": "0.01832255"
        },
        {
          "price": "54578.84",
          "quantity": "0.07328847"
        },
        ...
      ],
      "bids": [
        {
          "price": "54564.14",
          "quantity": "0.01832705"
        },
        {
          "price": "54559.06",
          "quantity": "0.07331504"
        },
        ...
      ]
    },
    ...
  ]
}
```

If at any point in time an error is encountered, the server will send a single `ERROR` message and will immediately close the connection:

[`WebSocketResponse`](https://docs.tradevest.ai/websockets/websocket-prices/prices#get-websocket-prices)

```json
{
  "type": "ERROR",
  "error": "Error message",
  "connectionId": "0116ec3f-6237-4c13-97b0-c3de46c5f6f1"
}
```

The `connectionId` attribute is meant to be used as a reference identifier provided to us if there is a need to investigate a specific error occurence.

Once the `PRICE_SUBSCRIPTION` message has been processed and `PRICES` messages start to flow the server does not expect to receive any additional\
messages. If the client would like to modify the list of subscribed assets then a new connection should be established and a new `PRICE_SUBSCRIPTION` message should be sent.

The client should assume that the WebSocket connection might be closed by the server at any time and in such case a new\
connection should be established again.

## Communication Diagram

Example communication diagram

{% @mermaid/diagram content="sequenceDiagram
box Partner
participant P as API Client
end
participant T as Our API
autonumber

```
P->>T: HTTP connect to /websockets/prices endpoint


    P-->>T: upgrade the connection to WebSocket protocol
    T-->>P: OK
    P-->>T: PRICE_SUBSCRIPTION message (ex. EUR/ETH & EUR/BTC)
    
    loop long running subscription loop
        T-->>P: PRICES message (ex EUR/ETH)
        T-->>P: PRICES message  (ex EUR/BTC)
        T-->>P: PRICES message  (ex EUR/ETH)
    end

    opt error encountered
        T-->>P: ERROR message
        T->>P: HTTP disconnect
    end" %}
```

## OpenAPI models

Our OpenAPI specification contains definitions of both request and response messages used by the [`Websocket Prices`](https://docs.tradevest.ai/websockets/websocket-prices/prices#get-websocket-prices) endpoint:

* [`WebSocketRequest`](https://docs.tradevest.ai/websockets/websocket-prices/schemas)
* [`WebSocketResponse`](https://docs.tradevest.ai/websockets/websocket-prices/schemas)

These structures serve as wrappers to all current and future message types and for consumer's convenience their content\
can always be differentiated by value of `TYPE` attribute (`PRICES_SUBSCRIPTION`, `PRICES`, `ERROR`) and its accompanying blocks.
