# Fetch by multiple contract keys

**URL:** <https://forum.canton.network/t/fetch-by-multiple-contract-keys/990>\
**Category:** App Development\
**Tags:** daml\
**Created:** [August 5, 2020, 4:21pm UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990 "2020-08-05T16:21:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex\_m](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/alex_m/32/550_2.png) [@alex\_m](https://forum.canton.network/u/alex_m)\
**Post date:** [August 5, 2020, 4:21pm UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990/1 "2020-08-05T16:21:15Z")

</div>

Let’s say you have a contract template

```
template Thing
  with
    description: Text
    smallThingKeys: [SmallThingKey]
...

```

where `SmallThingKey` is a contract key to a `SmallThing`. `Thing` and `SmallThing` contain data that is to be shown in the user interface. When fetching the `SmallThing`s, is it possible to submit a single query (containing all the `smallThingKeys`) and receive the `SmallThing`s in one result? I know this can be done by making multiple separate queries, but this would not be ideal given that I am trying to render a table of many `Thing`s.

Thanks again for the help.

Alex

(Ideal solution would use the `@daml/ledger` JS library. That being said, I am considering writing some Java middleware.)

---

<div class="post-metadata">

**Author:** ![Leonid\_Shlyapnikov](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_shlyapnikov/32/241_2.png) [@Leonid\_Shlyapnikov](https://forum.canton.network/u/Leonid_Shlyapnikov)\
**Post date:** [August 5, 2020, 7:44pm UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990/2 "2020-08-05T19:44:18Z")

</div>

Hi @alex_m !

Multiple contract keys per request are not supported in the `fetchByKey` function.

` @daml/ledger` JS library uses JSON API.

JSON API does not support multiple keys in the `Fetch Contract by Key` request (/v1/fetch):  
[https://docs.daml.com/json-api/index.html#fetch-contract-by-key](https://docs.daml.com/json-api/index.html#fetch-contract-by-key)

Please submit a JSON API feature request in here: [HTTP JSON API Maintenance Milestone · GitHub](https://github.com/digital-asset/daml/milestone/21)

---

<div class="post-metadata">

**Author:** ![Leonid\_Shlyapnikov](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_shlyapnikov/32/241_2.png) [@Leonid\_Shlyapnikov](https://forum.canton.network/u/Leonid_Shlyapnikov)\
**Post date:** [August 5, 2020, 7:51pm UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990/3 "2020-08-05T19:51:00Z")

</div>

@alex_m,

Have you considered using Streaming API?

See the docs:

- [https://docs.daml.com/json-api/index.html#fetch-by-key-contracts-stream](https://docs.daml.com/json-api/index.html#fetch-by-key-contracts-stream)

Streaming API (`/v1/stream/fetch`) lets you specify multiple keys per request:

```haskell
[
    {"templateId": "Account:Account", "key": {"_1": "Alice", "_2": "abc123"}},
    {"templateId": "Account:Account", "key": {"_1": "Alice", "_2": "def345"}}
]

```

---

<div class="post-metadata">

**Author:** ![alex\_m](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/alex_m/32/550_2.png) [@alex\_m](https://forum.canton.network/u/alex_m)\
**Post date:** [August 5, 2020, 9:48pm UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990/4 "2020-08-05T21:48:05Z")

</div>

Cheers @Leonid_Shlyapnikov, I’ll give this a try tomorrow!

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [August 6, 2020, 7:53am UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990/5 "2020-08-06T07:53:14Z")

</div>

Here’s an idea! Try this: write a choice like so

```haskell
controller c can LookupMany : Update [(ContractId SmallThing, SmallThing)] with smallThingKeys = 
  do mapA fetchByKey smallThingKeys

```

(n.b. untested)

This uses `mapA` from `Traversable`. It allows you to sequence a `[Update]` (i.e. your key lookups) into an `Update []`. This means, that the transaction will fail in whole if one of the lookups fail; otherwise it will return all the results in a list.

I hope I’ve understood your question correctly.

Let me know if this helps!

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [August 6, 2020, 8:19am UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990/6 "2020-08-06T08:19:35Z")

</div>

Something like that would work, but requires a ledger round-trip. A fetch from the JSON API is much faster, probably an order of magnitude.

---

<div class="post-metadata">

**Author:** ![alex\_m](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/alex_m/32/550_2.png) [@alex\_m](https://forum.canton.network/u/alex_m)\
**Post date:** [August 6, 2020, 12:57pm UTC](https://forum.canton.network/t/fetch-by-multiple-contract-keys/990/7 "2020-08-06T12:57:45Z")

</div>

> [@Leonid\_Shlyapnikov](#):
>
> Please submit a JSON API feature request in here: [HTTP JSON API Maintenance Milestone · GitHub](https://github.com/digital-asset/daml/milestone/21)

@Leonid_Shlyapnikov I have submitted a feature request: [Fetch API - Fetch by multiple contract keys · Issue #7047 · digital-asset/daml · GitHub](https://github.com/digital-asset/daml/issues/7047)

Thanks for the help everyone, I take it there is no way to do this by standard fetch ATM.

@Luciano so execute a choice when wanting to view all the `SmallThing`s? Looks interesting.

TBH ideal solution would be a fetch by multiple contract keys; not stream because I don’t think it’d be good to open many web sockets for a table of many `Thing`s.
