# What's the right way to use \`offset\` with the contract query stream in the JSON API?

**URL:** <https://forum.canton.network/t/whats-the-right-way-to-use-offset-with-the-contract-query-stream-in-the-json-api/437>\
**Category:** App Development\
**Created:** [May 12, 2020, 3:31pm UTC](https://forum.canton.network/t/whats-the-right-way-to-use-offset-with-the-contract-query-stream-in-the-json-api/437 "2020-05-12T15:31:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Matson](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/alex_matson/32/1572_2.png) [@Alex\_Matson](https://forum.canton.network/u/Alex_Matson)\
**Post date:** [May 12, 2020, 3:31pm UTC](https://forum.canton.network/t/whats-the-right-way-to-use-offset-with-the-contract-query-stream-in-the-json-api/437/1 "2020-05-12T15:31:54Z")

</div>

Hi all!

I have a websocket connection open to `/v1/stream/query` which works great, and now want to start making use of `offset` in future connections as described in the [docs](https://docs.daml.com/json-api/index.html#contracts-query-stream):

> An optional `offset` returned by a prior query (see output examples below) may be specified _before_ the above, as a separate body. It must be a string, and if specified, the stream will begin immediately _after_ the response body that included that offset

So I have a connection open, I keep track of offset coming in from the server (let’s say I read `"219"`), and then at some time later I close a connection and open a new one. In the new connection, I first tried sending two distinct/separate message bodies like so:

`{"offset": "219"}`

`{"templateIds": [...] }`

This gives me the following error message  
`Endpoints.InvalidUserInput: JsonReaderError. Cannot read JSON: <{"offset":"219"}>. Cause: spray.json.DeserializationException: Object is missing required member 'templateIds'`

Since it seems like the endpoint was expecting templateIds as a required member, I tried various ways of combining the offset with the query. For example, I tried:

`{"offset": "219", "templateIds": [...] }` which gives another invalid json error, and also

`{"templateIds": [...], "query": { "offset": "219"} }` which doesn’t seem to work either.

Any help with what I might be doing wrong would be greatly appreciated! 🙂

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [May 12, 2020, 3:37pm UTC](https://forum.canton.network/t/whats-the-right-way-to-use-offset-with-the-contract-query-stream-in-the-json-api/437/2 "2020-05-12T15:37:47Z")

</div>

Sending separate messages as done in your first try is the correct syntax; you can be sure that the other formats you tried will not work.

What version of the JSON API are you using? The first cause that comes to mind would be that this version was from before we supported offsets as arguments; the error you get is exactly the error one would get in that case. (The presence of offsets in the output stream is no guarantee that your version is new enough, because we added offsets to the output stream _before_ supporting them as arguments.)

---

<div class="post-metadata">

**Author:** ![Alex\_Matson](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/alex_matson/32/1572_2.png) [@Alex\_Matson](https://forum.canton.network/u/Alex_Matson)\
**Post date:** [May 12, 2020, 3:45pm UTC](https://forum.canton.network/t/whats-the-right-way-to-use-offset-with-the-contract-query-stream-in-the-json-api/437/3 "2020-05-12T15:45:43Z")

</div>

Thanks for the response!

Which version were the offset arguments added in? We’re using 0.13.55

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [May 12, 2020, 3:51pm UTC](https://forum.canton.network/t/whats-the-right-way-to-use-offset-with-the-contract-query-stream-in-the-json-api/437/4 "2020-05-12T15:51:33Z")

</div>

1.0.0 was the first version; specifically, the docs you quoted do not exist in [the 0.13.55 version of the docs](https://docs.daml.com/0.13.55/json-api/index.html#contracts-query-stream). You can rely on this consistently, as we’ve kept the JSON API documentation up-to-date throughout its various releases.

---

<div class="post-metadata">

**Author:** ![Alex\_Matson](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/alex_matson/32/1572_2.png) [@Alex\_Matson](https://forum.canton.network/u/Alex_Matson)\
**Post date:** [May 12, 2020, 3:54pm UTC](https://forum.canton.network/t/whats-the-right-way-to-use-offset-with-the-contract-query-stream-in-the-json-api/437/5 "2020-05-12T15:54:15Z")

</div>

Ah, I see. That solves it, then!
