Available languages: [Python](https://databento.com/docs/examples/basics-live/live-snapshot.python.md) · [C++](https://databento.com/docs/examples/basics-live/live-snapshot.cpp.md) · [Rust](https://databento.com/docs/examples/basics-live/live-snapshot.rust.md) · [HTTP](https://databento.com/docs/examples/basics-live/live-snapshot.http.md) · [Raw](https://databento.com/docs/examples/basics-live/live-snapshot.raw.md)

Source: https://databento.com/docs/examples/basics-live/live-snapshot


# Subscribe to MBO snapshot

## Overview

This example shows how to use the [Live client](https://databento.com/docs/api-live/client/live.md) to request a snapshot for [MBO schema](https://databento.com/docs/schemas/mbo.md), which can be used to reconstruct the [limit order book](https://databento.com/docs/examples/order-book/limit-order-book.md) without replaying the entire trading session.

More details about live snapshot can be found in this [article](https://databento.com/docs/conventions/mbo-snapshot.md).

## Requesting live snapshot

To request a subscription with snapshot, we use [SubscribeWithSnapshot](https://databento.com/docs/api-live/client/live-blocking-subscribe-with-snapshot.md) method from the live client. Once the session has started, it streams all the snapshot records (with the `F_SNAPSHOT` [flag](https://databento.com/docs/conventions/common-fields-enums-types.md#flags) set), followed by real-time records. We stop iterating over the records when encountering a record with the last flag set, to make sure the book is in a valid state.

## Example

```cpp
#include <databento/constants.hpp>  // dataset
#include <databento/live.hpp>       // LiveBuilder
#include <databento/record.hpp>     // MboMsg
#include <iostream>

namespace db = databento;

int main() {
  // First, create a live client and connect
  auto client = db::LiveBlocking::Builder()
                    .SetKey("$YOUR_API_KEY")
                    .SetDataset(db::Dataset::GlbxMdp3)
                    .BuildBlocking();

  // Next, we will subscribe to the MBO snapshot and start the session
  client.SubscribeWithSnapshot({"ES.c.0"}, db::Schema::Mbo,
                               db::SType::Continuous);
  client.Start();

  constexpr auto timeout = std::chrono::seconds{5};

  // Finally, we will process all snapshot records, and stop at the first record
  // with F_LAST flag, which indicates that the snapshot is complete and the
  // order book is in a valid state
  while (auto* record = client.NextRecord(timeout)) {
    if (auto* msg = record->GetIf<db::MboMsg>()) {
      if (msg->flags.IsSnapshot()) {
        std::cout << "Snapshot: " << *msg << '\n';
      } else {
        std::cout << "Live " << *msg << '\n';
      }

      if (msg->flags.IsLast()) {
        std::cout << "Snapshot is complete" << '\n';
        break;
      }
    }
  }

  return 0;
}
```
## Result

```plaintext
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-23T10:06:12.163585630Z },
    order_id = 0,
    price = kUndefPrice,
    size = 0,
    flags = SNAPSHOT | BAD_TS_RECV (40),
    channel_id = 0,
    action = Clear,
    side = None,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 0
}
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-21T16:28:16.070204729Z },
    order_id = 6413514530616,
    price = 5547.500000000,
    size = 8,
    flags = SNAPSHOT | BAD_TS_RECV (40),
    channel_id = 0,
    action = Add,
    side = Bid,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 1022
}
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-21T16:28:16.070204729Z },
    order_id = 6413514530330,
    price = 5547.250000000,
    size = 7,
    flags = SNAPSHOT | BAD_TS_RECV (40),
    channel_id = 0,
    action = Add,
    side = Bid,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 1022
}
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-21T16:28:16.070204729Z },
    order_id = 6413514530315,
    price = 5547.000000000,
    size = 9,
    flags = SNAPSHOT | BAD_TS_RECV (40),
    channel_id = 0,
    action = Add,
    side = Bid,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 1022
}
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-21T16:28:16.070204729Z },
    order_id = 6413514528530,
    price = 5546.750000000,
    size = 8,
    flags = SNAPSHOT | BAD_TS_RECV (40),
    channel_id = 0,
    action = Add,
    side = Bid,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 1021
}
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-21T16:28:16.070204729Z },
    order_id = 6413514517321,
    price = 5546.500000000,
    size = 7,
    flags = SNAPSHOT | BAD_TS_RECV (40),
    channel_id = 0,
    action = Add,
    side = Bid,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 1020
}
...
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-23T10:06:10.357094773Z },
    order_id = 6413522250995,
    price = 5605.250000000,
    size = 3,
    flags = SNAPSHOT | BAD_TS_RECV (42),
    channel_id = 0,
    action = Add,
    side = Bid,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 12967152
}
Snapshot: MboMsg {
    hd = RecordHeader { length = 14, rtype = Mbo, publisher_id = 1, instrument_id = 118, ts_event = 2024-07-23T10:06:10.493868863Z },
    order_id = 6413522250996,
    price = 5606.250000000,
    size = 1,
    flags = LAST | SNAPSHOT | BAD_TS_RECV (170),
    channel_id = 0,
    action = Add,
    side = Bid,
    ts_recv = 2024-07-23T10:06:12.163585630Z,
    ts_in_delta = 0,
    sequence = 12967153
}
Snapshot is complete
```


# Subscribe to MBO snapshot

## Overview

This example shows how to use the [Live client](https://databento.com/docs/api-live/client/live.md) to request a snapshot for [MBO schema](https://databento.com/docs/schemas/mbo.md), which can be used to reconstruct the [limit order book](https://databento.com/docs/examples/order-book/limit-order-book.md) without replaying the entire trading session.

More details about live snapshot can be found in this [article](https://databento.com/docs/conventions/mbo-snapshot.md).

## Requesting live snapshot

To request a subscription with snapshot, we provide `snapshot=True` and leave `start` default to `None`. Once the session has started, it streams all the snapshot records (with the `F_SNAPSHOT` [flag](https://databento.com/docs/conventions/common-fields-enums-types.md#flags) set), followed by real-time records. We stop iterating over the records when encountering a record with the last flag set, to make sure the book is in a valid state.

## Example

```python
import databento as db

# First, create a live client and connect
live_client = db.Live(key="$YOUR_API_KEY")

# Next, we will subscribe to the MBO snapshot
live_client.subscribe(
    dataset="GLBX.MDP3",
    schema="mbo",
    stype_in="continuous",
    symbols="ES.c.0",
    snapshot=True,
)

# Finally, we will process all snapshot records, and stop at the first record
# with F_LAST flag, which indicates that the snapshot is complete and the
# order book is in a valid state
for record in live_client:
    if isinstance(record, db.MBOMsg):
        if record.flags & db.RecordFlags.F_SNAPSHOT:
            print(f"Snapshot: {record}")
        else:
            print(f"Live: {record}")

        if record.flags & db.RecordFlags.F_LAST:
            print("Snapshot is complete")
            break
    elif isinstance(record, db.ErrorMsg):
        print(f"Error {record.err}")
        break
```
## Result

```plaintext
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1769043341317912993, pretty_ts_event='2026-01-22T00:55:41.317912993Z', action='R', side='N', price=9223372036854775807, pretty_price=NaN, size=0, channel_id=0, order_id=0, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=0)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6811250000000, pretty_price=6811.25, size=1, channel_id=0, order_id=6416171004846, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=761)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6810000000000, pretty_price=6810, size=2, channel_id=0, order_id=6416138613427, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=757)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6805000000000, pretty_price=6805, size=2, channel_id=0, order_id=6416108605777, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=756)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6800000000000, pretty_price=6800, size=50, channel_id=0, order_id=6416091817328, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=754)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6800000000000, pretty_price=6800, size=10, channel_id=0, order_id=6416102672407, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=755)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6800000000000, pretty_price=6800, size=5, channel_id=0, order_id=6416117277784, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=756)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6800000000000, pretty_price=6800, size=20, channel_id=0, order_id=6416141452066, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=758)
...
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6795000000000, pretty_price=6795, size=25, channel_id=0, order_id=6416122261431, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=757)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6791000000000, pretty_price=6791, size=1, channel_id=0, order_id=6416120339416, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=757)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6780500000000, pretty_price=6780.5, size=1, channel_id=0, order_id=6416121479238, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=757)
Snapshot: MboMsg(ts_recv=1769043341317912993, pretty_ts_recv='2026-01-22T00:55:41.317912993Z', rtype=<RType.MBO: 160>, publisher_id=GLBX.MDP3.GLBX (1), instrument_id=42140878, ts_event=1768746891991522549, pretty_ts_event='2026-01-18T14:34:51.991522549Z', action='A', side='B', price=6775000000000, pretty_price=6775, size=1, channel_id=0, order_id=6416127400070, flags=SNAPSHOT | BAD_TS_RECV (40), ts_in_delta=0, sequence=757)
```


# Subscribe to MBO snapshot

## Overview

This example shows how to use the [Live client](https://databento.com/docs/api-live/client/live.md) to request a snapshot for [MBO schema](https://databento.com/docs/schemas/mbo.md), which can be used to reconstruct the [limit order book](https://databento.com/docs/examples/order-book/limit-order-book.md) without replaying the entire trading session.

More details about live snapshot can be found in this [article](https://databento.com/docs/conventions/mbo-snapshot.md).

## Requesting live snapshot

To request a subscription with snapshot, we call `use_snapshot(true)` method on the subscription builder and leave `start` unset. Once the session has started, it streams all the snapshot records (with the `F_SNAPSHOT` [flag](https://databento.com/docs/conventions/common-fields-enums-types.md#flags) set), followed by real-time records. We stop iterating over the records when encountering a record with the last flag set, to make sure the book is in a valid state.

## Example

```rust
use databento::{
    LiveClient,
    dbn::{Dataset, MboMsg, SType, Schema},
    live::Subscription,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // First, create a live client and connect
    let mut client = LiveClient::builder()
        .key_from_env()?
        .dataset(Dataset::GlbxMdp3)
        .build()
        .await?;

    // Next, we will subscribe to the MBO snapshot and start the session
    client
        .subscribe(
            Subscription::builder()
                .schema(Schema::Mbo)
                .symbols("ES.c.0")
                .stype_in(SType::Continuous)
                .use_snapshot(true)
                .build(),
        )
        .await?;
    client.start().await?;

    // Then, we will process all snapshot records, and stop at the first record
    // with F_LAST flag, which indicates that the snapshot is complete and the
    // order book is in a valid state
    while let Some(record) = client.next_record().await? {
        if let Some(mbo) = record.get::<MboMsg>() {
            if mbo.flags.is_snapshot() {
                println!("Snapshot: {mbo:?}");
            } else {
                println!("Live: {mbo:?}");
            }
            if mbo.flags.is_last() {
                println!("Snapshot is complete");
                break;
            }
        }
    }

    // Finally, we close the connection
    client.close().await?;

    Ok(())
}
```
## Result

```plaintext
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721730014428582820 }, order_id: 0, price: UNDEF_PRICE, size: 0, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'R', side: 'N', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 0 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514530616, price: 5547.500000000, size: 8, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 1022 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514530330, price: 5547.250000000, size: 7, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 1022 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514530315, price: 5547.000000000, size: 9, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 1022 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514528530, price: 5546.750000000, size: 8, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 1021 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514517321, price: 5546.500000000, size: 7, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 1020 }
...
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721730013056310351 }, order_id: 6413522263444, price: 5607.250000000, size: 1, flags: SNAPSHOT | BAD_TS_RECV (42), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 12990544 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721730013859038005 }, order_id: 6413522263447, price: 5607.500000000, size: 1, flags: LAST | SNAPSHOT | BAD_TS_RECV (170), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730014428582820, ts_in_delta: 0, sequence: 12990552 }
Snapshot is complete
```
