# Bids and asks

## What is this endpoint for? <a href="#what-is-this-endpoint-for" id="what-is-this-endpoint-for"></a>

All bids and asks on an exchange's order book. Use Kaiko Stream for real-time tick-level updates, or CSV for backdated information. When you connect, you first receive a full snapshot of the current order book with every bid and ask, followed by continuous real-time updates for every change ("delta") that takes place.&#x20;

{% hint style="warning" %}
When you subscribe, you'll initially receive a full snapshot within a few seconds, followed by all subsequent tick-level updates. If you receive another full snapshot, replace your local snapshot with the new one. This should happen infrequently and only occurs if we detect a consistency issue, such as a lost connection to the exchange or the exchange being down for some time.

If you receive an update with an amount of 0, you should remove the corresponding price level from your local order book. You'll occasionally receive a 0 message for a price level that doesn't exist in your local order book - this can be safely ignored and is due to various exchange limitations.&#x20;

The order book should not be considered valid until all updates with the same `tsExchange` have been applied to your local order book, meaning all potential messages from the same update batch have been processed.
{% endhint %}

## Endpoints

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

```url
gateway-v0-grpc.kaiko.ovh
```

{% endtab %}

{% tab title="http" %}

```url
gateway-v0-http.kaiko.ovh
```

{% endtab %}

{% tab title="API (testing)" %}

```url
https://gateway-v0-http.kaiko.ovh/api/stream/orderbookl2_v1
https://gateway-v0-http.kaiko.ovh/api/stream/orderbookl2_replay_v1
```

{% endtab %}
{% endtabs %}

## Request parameters

<table><thead><tr><th width="129">Parameter</th><th width="387">Description</th><th>Examples</th></tr></thead><tbody><tr><td><code>instrumentCriteria</code></td><td><p>A <strong>nested object</strong> to configure following properties for your stream: </p><ul><li><code>exchange</code> (String) - The code(s) for the exchange(s)</li><li><code>instrument_class</code> (String) - The class(es) of the instrument(s) .</li><li><code>code (String) -</code> The Kaiko code for the instrument.</li></ul><p>Explore instruments, codes and exchanges in the <a href="https://instruments.kaiko.com/#/instruments">Instrument Explorer</a> or <a data-mention href="https://app.gitbook.com/s/ltAbhvgBfrAWlpUnC7ys/data-feeds/reference-data">Reference Data</a>. </p></td><td><p><code>cbse</code></p><p></p><p><code>spot</code></p><p></p><p><code>algo-btc</code><br><br></p></td></tr></tbody></table>

{% hint style="info" %}
**Configuring a wildcard**

A wildcard allows you to request all information we have on a specific instrument, class, or exchange in the same stream.\
\
Use a `*` in place of the relevant `exchange`, `instrument`, or `class` parameter. <br>

For example, the configuration below would deliver trades for BTC/USD across all exchanges where it’s supported:

`exchange: *`\
`class: spot`\
`instrument: btc-usd`
{% endhint %}

## Response fields

<table><thead><tr><th width="244">Field</th><th width="537">Description</th></tr></thead><tbody><tr><td><code>class</code></td><td>Instrument class, empty when not mapped.</td></tr><tr><td><code>code</code></td><td>Instrument code, empty when not mapped.</td></tr><tr><td><code>exchange</code></td><td>Instrument exchange code.</td></tr><tr><td><code>sequenceId</code></td><td>Sequence ID for event. Sortable in lexicographic order.</td></tr><tr><td><code>updateType</code></td><td><code>SNAPSHOT</code> - A new Snapshot of the order book<br><code>UPDATED</code>- A new batch of bids &#x26; asks since the snapshot</td></tr><tr><td><code>asks</code></td><td><p>Represents sell orders</p><p><br><code>amount</code>: the quantity of the base asset available for sale</p><p><code>price</code>: the price per unit of the base that the seller is willing to accept in the quote asset, represented as a scientific notation<br><br><strong>Example:</strong> algo-btc</p><ul><li>base asset = algo</li><li>quote asset = btc<br><br><code>amount</code> : <code>42656.0</code><br><code>price</code>: <code>1.97e-06</code></li><li>The asker has <code>42656.0</code> Algo available</li><li>For each unit of Algo, the buyer is willing to accept <code>1.97e-06</code> BTC, equal to 0.00000197 when converted to a decimal number</li></ul></td></tr><tr><td><code>bids</code></td><td><p>Represents buy orders. </p><p><br><code>amount</code>: the quantity of the base asset the buyer is willing to purchase</p><p><code>price</code>: the price per unit of the base that the buyer is willing to pay in the quote asset, represented as a scientific notation<br><br><strong>Example:</strong> algo-btc</p><ul><li>base asset = algo</li><li>quote asset = btc<br><br><code>amount</code> : <code>80569.0</code><br><code>price</code>: <code>1.96e-06</code></li><li>The buyer will purchase up to <code>80569.0</code> Algo</li><li>For each unit of Algo, the buyer is willing to pay <code>1.96e-06</code> BTC, equal to 0.00000196 when converted to a decimal number</li></ul></td></tr><tr><td><code>tsExchange</code></td><td><p>The timestamp provided by the exchange for the data. Where exchanges do not provide this data, we fill this with <code>tsCollection</code>. <br><br>This applies to the following exchanges: <br></p><ul><li>Bitfinex (bfnx)</li><li>Bitflyer (bfly)</li><li>CEX.io (cexi)</li></ul></td></tr><tr><td><code>tsCollection</code></td><td>The timestamp for when Kaiko received the data from the exchange.</td></tr><tr><td><code>tsEvent</code></td><td>The timestamp the data became available in the Kaiko system.</td></tr></tbody></table>

## Request examples

{% tabs %}
{% tab title="Python" %}
{% @github-files/github-code-block url="<https://github.com/kaikodata/kaiko-sdk-examples/blob/master/python/code-example/orderbookl2_v1_request.py>" visible="false" %}
{% endtab %}

{% tab title="Python (Replay)" %}
**This example demonstrates how to request historical data using replay. The maximum amount of data you can request for one replay cannot exceed a total of 24 hours in `hours`, `seconds`, and `minutes`.**&#x20;

**Replay data is available on a 72-hour rolling basis and should only be used to retrieve missed data. If full history is required, please use Rest API or CSV deployment methods.**

{% @github-files/github-code-block url="<https://github.com/kaikodata/kaiko-sdk-examples/blob/master/python/code-example/orderbookl2_v1_replay_request.py>" %}
{% endtab %}

{% tab title="cURL" %}
**cURL requests are intended for testing purposes only.**

{% code overflow="wrap" %}

```
#Live

curl -X POST "https://gateway-v0-http.kaiko.ovh/api/stream/orderbookl2_v1" -H "accept: application/json" -H "X-Api-Key: $KAIKO_API_KEY" -H "Content-Type: application/json" -d "{ \"instrumentCriteria\": { \"exchange\": \"cbse\", \"instrumentClass\": \"spot\", \"code\": \"algo-btc\"}}"


#Replay/Historical Data

curl -X POST "https://gateway-v0-http.kaiko.ovh/api/stream/orderbookl2_replay_v1" -H "accept: application/json" -H "X-Api-Key: $KAIKO_API_KEY" -H "Content-Type: application/json" -d "{ \"instrumentCriteria\": {  \"exchange\": \"cbse\",  \"instrumentClass\": \"spot\",  \"code\": \"algo-btc\" }, \"interval\": { \"startTime\": \"2025-01-14T12:00:00.000Z\", \"endTime\": \"2025-01-14T18:00:00.000Z\" }}"
```

{% endcode %}
{% endtab %}

{% tab title="Other examples" %}
For more advanced users, you can access our full SDK [here](https://github.com/kaikodata/kaiko-sdk-examples/tree/master), where you'll find more coding languages, examples and guidance.
{% endtab %}
{% endtabs %}

## Response Example

```json
Received message {"class":"spot",
"code":"bch-eur",
"exchange":"cbse",
"sequenceId":"cu2o2sokocas72q8rkq0",
"updateType":"SNAPSHOT",
"asks":[
  {
    "amount":0.02778327,
    "price":414.07
  },
  {
    "amount":0.4,
    "price":414.08
  }
],
"bids":[
  {
    "amount":0.4,
    "price":413.93
  },
  {
    "amount":0.0277962,
    "price":413.85
  } ],
"tsExchange":{
  "value":"2025-01-13T21:11:15.064Z"
},
"tsCollection":{
  "value":"2025-01-13T21:11:15.064284559Z"
},
"tsEvent":"2025-01-13T21:11:15.260258142Z",
"additionalProperties":{}
}

Received message {"class":"spot",
"code":"bch-eur",
"exchange":"cbse",
"sequenceId":"cu2o2sokocas72q8rla0",
"updateType":"UPDATE",
"asks":[
  {
    "amount":4,
    "price":414.42
  }
  ],
"bids":[],
"tsExchange":{
  "value":"2025-01-13T21:11:15.016291Z"
},
"tsCollection":{
  "value":"2025-01-13T21:11:15.086529551Z"
},
"tsEvent":"2025-01-13T21:11:15.277722255Z",
"additionalProperties":{}
}

Received message {"class":"spot",
"code":"bch-eur",
"exchange":"cbse",
"sequenceId":"cu2o2sokocas72q8t9q0",
"updateType":"UPDATE",
"asks":[],
"bids":[
  {
    "amount":0,
    "price":413.85
  },
  {
    "amount":0.02779016,
    "price":413.94
  }
  ],
"tsExchange":{
  "value":"2025-01-13T21:11:15.235251Z"
  },
"tsCollection":{
  "value":"2025-01-13T21:11:15.248921649Z"
  },
"tsEvent":"2025-01-13T21:11:15.635226002Z",
"additionalProperties":{}
}

```
