# In DAML Script, do we have equivalent command for lookupbykey, fetchbykey?

**URL:** https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919
**Category:** App Development
**Tags:** daml, script
**Created:** [July 27, 2020, 12:02pm UTC](https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919 "2020-07-27T12:02:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lour.mae.g.balbin](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/lour.mae.g.balbin/32/714_2.png) [@lour.mae.g.balbin](https://forum.canton.network/u/lour.mae.g.balbin)
#### Post date: [July 27, 2020, 12:02pm UTC](https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919/1 "2020-07-27T12:02:51Z")

</div>

I’ve checked some topics here in daml discuss but was not able to see the one i’m looking for so I’m creating this one here.

I’ve checked on some sample DAML Scripts I can find here in daml discuss, as well as the daml docs’ daml script library page ([daml docs \> daml script library](https://docs.daml.com/daml-script/daml-script-docs.html#type-daml-script-commands-61298)) but was not able to see equivalent command we can use for lookupbykey and fetchbykey.  
I can only see “query” which results in all the active contracts available for a queried template and party.

Do we have equivalent command for lookupbykey, fetchbykey in daml script?  
If not, do we have any reason why?

Hoping for your response. Thank you. 🙂

Just wondering this as we can use the said commands via DAML Scenario. And that we are trying to convert a created DAML Scenario into a DAML Script. One scenario we have uses those commands. So we are trying to figure out if there is a direct way to do the said commands via DAML Script too.

---

<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: [July 27, 2020, 12:51pm UTC](https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919/2 "2020-07-27T12:51:37Z")

</div>

Hi @lour.mae.g.balbin, welcome to the forum!  
Great question! DAML Script communicates via the gRPC Ledger API so it is limited to the 4 commands provided by that:

1. `createCmd`
2. `exerciseCmd`
3. `exerciseByKeyCmd`
4. `createAndExerciseCmd`.

However, that doesn’t stop you from doing other things. You can call arbitrary `Update` actions via `createAndExerciseCmd`. Let’s look at a simple example. Here’s a template with a key and a scenario that fetches that key:

```haskell
template TKey
  with
    p : Party
  where
    signatory p
    key p : Party
    maintainer key

fetchByKeyScenario = scenario do
  p <- getParty "p"
  cid <- submit p (create (TKey p))
  r <- submit p (fetchByKey @TKey p)
  debug r
  assert (r._1 == cid)

```

To do the same in DAML Script, we first create a helper template with a choice that will fetch the contract with the given key:

```haskell
template TKeyHelper
  with
    p : Party
  where
    signatory p
    choice FetchByKey : (ContractId TKey, TKey)
      with
        keyP : Party
      controller p
      do fetchByKey @TKey keyP

```

You can then call this `FetchByKey` choice using `createAndExerciseCmd`:

```haskell
fetchByKeyScript = do
  p <- allocateParty "p"
  cid <- submit p (createCmd (TKey p))
  r <- submit p (createAndExerciseCmd (TKeyHelper p) (FetchByKey p))
  debug r
  assert (r._1 == cid)

```

Depending on your needs, another option is to create only one instance of this helper template and make `FetchByKey` non-consuming. You can then call the choice via a regular `exerciseCmd` or if you add a key, `exerciseByKeyCmd`.

---

<div class="post-metadata">

### Author: ![lour.mae.g.balbin](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/lour.mae.g.balbin/32/714_2.png) [@lour.mae.g.balbin](https://forum.canton.network/u/lour.mae.g.balbin)
#### Post date: [July 28, 2020, 7:32am UTC](https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919/3 "2020-07-28T07:32:40Z")

</div>

Thank you @cocreature  
This response (specifically the sample you provided) is really helpful and had answered my inquiry fully. Thank you again! 🙂 😁

---

<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: [July 30, 2020, 8:16am UTC](https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919/4 "2020-07-30T08:16:15Z")

</div>

> [@cocreature](#):
>
> so it is limited to the 4 command

Hey @cocreature is this a design decision, or is the ultimate goal for daml-script to have feature-parity with `scenario`s, so as to replace them completely?

---

<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: [July 30, 2020, 8:23am UTC](https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919/5 "2020-07-30T08:23:09Z")

</div>

@Luciano DAML Script is limited by what the Ledger API provides. At this point it only provides those 4 commands. We do plan to extend DAML Script so that it can be used in the IDE. However, while it would be possible to add more commands there in principle, I don’t think this is a good idea. The fact that you can do more in a scenario than you can do via the API is pretty confusing and we have seen several examples where it has lead to models that were usable in a scenario but were not usable via the ledger API.

---

<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: [November 26, 2020, 2:35pm UTC](https://forum.canton.network/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919/6 "2020-11-26T14:35:33Z")

</div>

A post was split to a new topic: [Access to transaction stream in DAML Script](https://forum.canton.network/t/access-to-transaction-stream-in-daml-script/1710)
