# Can we add query capability to fetchByKey from the JSON API?

**URL:** <https://forum.canton.network/t/can-we-add-query-capability-to-fetchbykey-from-the-json-api/3726>\
**Category:** App Development\
**Created:** [January 12, 2022, 7:04pm UTC](https://forum.canton.network/t/can-we-add-query-capability-to-fetchbykey-from-the-json-api/3726 "2022-01-12T19:04:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonid\_Rozenberg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_rozenberg/32/48_2.png) [@Leonid\_Rozenberg](https://forum.canton.network/u/Leonid_Rozenberg)\
**Post date:** [January 12, 2022, 7:04pm UTC](https://forum.canton.network/t/can-we-add-query-capability-to-fetchbykey-from-the-json-api/3726/1 "2022-01-12T19:04:48Z")

</div>

I noticed that the [fetch-by-key](https://docs.daml.com/json-api/index.html#fetch-contract-by-key) endpoint does not support the [query-lang](https://docs.daml.com/json-api/search-query-language.html) because the intent to just fetch one key. Would it be possible to extend that end-point so that I can ask for all contracts based on a restricted set of keys?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [January 12, 2022, 8:05pm UTC](https://forum.canton.network/t/can-we-add-query-capability-to-fetchbykey-from-the-json-api/3726/2 "2022-01-12T20:05:50Z")

</div>

The advantage of the fetch by key endpoint is that they are more efficient than regular queries because we have specific indices for key equality.

There is no querying functionality for keys and it’s somewhat redundant: The key is always derived from the underlying contract payload. So (ignoring some potential restrictions in the JSON API querying language) you can translate a query on the key to a query on the contract payload.

As a simple example:  
Consider this template

```haskell
template T
  with
    p : Party
    i : Int
    t : Text
  where
    signatory p
    key (p, i)
    maintainer key._1

```

Instead of querying for everything with keys of the form `(p, i > 5)` you can also query for all contracts with `i > 5` (and the respective party for `p`).

---

<div class="post-metadata">

**Author:** ![Leonid\_Rozenberg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_rozenberg/32/48_2.png) [@Leonid\_Rozenberg](https://forum.canton.network/u/Leonid_Rozenberg)\
**Post date:** [January 12, 2022, 8:29pm UTC](https://forum.canton.network/t/can-we-add-query-capability-to-fetchbykey-from-the-json-api/3726/3 "2022-01-12T20:29:06Z")

</div>

Thank you
