> For the complete documentation index, see [llms.txt](https://docs.tradevest.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tradevest.ai/documentation/prices.md).

# 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`](/websockets/websocket-prices/prices.md#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`](/websockets/websocket-prices/prices.md#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`](/websockets/websocket-prices/prices.md#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`](/websockets/websocket-prices/prices.md#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`](/websockets/websocket-prices/prices.md#get-websocket-prices) endpoint:

* [`WebSocketRequest`](/websockets/websocket-prices/schemas.md)
* [`WebSocketResponse`](/websockets/websocket-prices/schemas.md)

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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.tradevest.ai/documentation/prices.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
