# How to fetch a contract from dumlhub Ledger via Java

**URL:** <https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638>\
**Category:** App Development\
**Tags:** daml\
**Created:** [May 29, 2022, 4:27pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638 "2022-05-29T16:27:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 29, 2022, 4:27pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/1 "2022-05-29T16:27:55Z")

</div>

Hi there,

Can you refer me to a documentation or example on how to fetch a certain contract from a Ledger (from damlHub) according to a predefined key (e.g: fetch a product contract by its productId key)

Thanks

---

<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:** [May 29, 2022, 4:30pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/2 "2022-05-29T16:30:36Z")

</div>

Daml Hub exposes the JSON API which provides endpoints for fetching a contract by [contract id](https://docs.daml.com/json-api/index.html#fetch-contract-by-contract-id) and by [contract key](https://docs.daml.com/json-api/index.html#fetch-contract-by-key).

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 29, 2022, 6:02pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/3 "2022-05-29T18:02:13Z")

</div>

Thanks @cocreature.

The documentation lack to explain several things regarding the body:

1. Is the templateId should be the full name? In the screenshot below its “Workflow.Workflow:Product”
2. Is the key \_1 is the name of the key (In the screenshot below it’s “productId”)
3. Is the \_2 is the value of the key? (In the screenshot below it “P1234-337”)

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

```

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/5/5f9322c0df8901dbc02791d4504fd872ee379a57.png)

![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/e/ee25c261c92d326656d5c077fd49927ee00401c1.png)

postman smaple return 404 although the contract exists so i assume i mis understood the body:

```haskell
{
    "templateId": "Workflow.Workflow:Product",
    "key": {
        "_1": "productId",
        "_2": "P1234-337"
    }
}

```

Response:

```haskell
{
    "errors": [
        "HttpMethod(POST), uri: http://p4np49dc43hsdza3.daml.app/v1/stream/fetch%20"
    ],
    "status": 404
}

```

---

<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:** [May 29, 2022, 6:07pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/4 "2022-05-29T18:07:40Z")

</div>

`templateId` is the template id of the contract you’re trying to fetch. As anywhere else in the JSON API where a template id is specified you can either use

```haskell
<package ID>:<module>:<entity>

```

or if it’s unambiguous

```haskell
<module>:<entity>

```

The key is not separated into name and value, it is only the value. In the example in the docs, the key is a tuple `(Party, Text)`. Daml tuples are translated to records with fields `_1, …, _n` ([docs](https://docs.daml.com/app-dev/daml-lf-translation.html#tuple-types)) so that’s why the JSON representation looks like this.

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 30, 2022, 9:11am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/5 "2022-05-30T09:11:33Z")

</div>

Hi @cocreature ,

When you refer to the contract key, is it as below screenshot?

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/f/f7c967fcae7c2e85c16a47a6be3add13d5d92295.png)

Can you see anything missing in the below request that prevents from 200OK to respond (response is the second screenshot)?

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/1/14ac4e191f515b53e01eb707ee0d3d849db3a3be.png)

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/4/435ef0a8def6e88a5e8207d8bce5ffc444c9a20d.png)

---

<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:** [May 30, 2022, 9:53am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/6 "2022-05-30T09:53:44Z")

</div>

It depends on how your defined your key. Can you share the key definition of `Product`?

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 30, 2022, 9:58am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/7 "2022-05-30T09:58:31Z")

</div>

Are you referring to this?

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/9/9353cb357c9731d782f66fc593df7de23219b0e5.png)

---

<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:** [May 30, 2022, 10:01am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/8 "2022-05-30T10:01:40Z")

</div>

I was thinking of the Daml definition but this is sufficient. Your request looks correct but I believe you’re contacting the wrong endpoint: You’re querying the streaming endpoint with a POST request. Try `/v1/fetch` instead of `/v1/stream/fetch`. The docs seem off there, I’ll make sure that gets fixed.

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 30, 2022, 10:32am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/9 "2022-05-30T10:32:31Z")

</div>

Still the same error.

Although i should have received 401 (instead of 404), maybe the Bearer token is wrong?  
Should i place the token of the contract’s Party (e.g the Carrier in my example)

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/2/217e54456cc6b604c5e1a9a7e4e886b215980160.png)

---

<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:** [May 30, 2022, 10:34am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/10 "2022-05-30T10:34:45Z")

</div>

You have a `%20` so a space at the end of your url.

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 30, 2022, 11:38am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/11 "2022-05-30T11:38:39Z")

</div>

Embarrassing…It works 😄  
Thanks @cocreature

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 30, 2022, 2:28pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/12 "2022-05-30T14:28:20Z")

</div>

@cocreature Can you advise on the best way to deserialize the returned JSON into a contract?

My question is not technical since i’m aware of kibraries such as Gson or Jackson. My question is wether there is a recommended way to create an instance of a contract by its corresponding json (it’s a huge one by the way)

My end game is to submit and exercise a Command with this contract

---

<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:** [May 30, 2022, 2:51pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/13 "2022-05-30T14:51:52Z")

</div>

There is no recommended way to decode the JSON API responses in Java atm. You have to implement it yourself.

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 30, 2022, 8:03pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/14 "2022-05-30T20:03:07Z")

</div>

I’ll ask it differently:

I want to exercise a multi party submission command on a certain contract which i fetched from a Ledger.

According to my knowledge, for a single party submission, i need to create a com.daml.ledger.javaapi.data.Command and use the Ledger client to “submitAndWait” it.

My question is: Given a contractId, what are the steps to perform such operation (submit multi party command)

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/7/79247c9bb91da7277ea50dafde3864def4d1d279.png)

Is there a constructor like CreateAndExerciseCommand(templateId, createArguments, choice, choiceArgument) that receives the contractId as an argument?

---

<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:** [May 31, 2022, 5:08pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/15 "2022-05-31T17:08:59Z")

</div>

If you want to exercise a choice on a contract id use `ExerciseCommand` instead of `CreateAndExerciseCommand`. You don’t need to fetch the contract for that. The command is also independent of whether you do a multi-command submission or not.

For a multi-party submission you need to set `actAs` and `readAs` on the [commands](https://docs.daml.com/app-dev/bindings-java/javadocs/com/daml/ledger/api/v1/CommandsOuterClass.Commands.Builder.html).

Note though that you also need a token for those parties and Daml Hub does not issue multi-party tokens at the moment so while you can use that locally you cannot make multi-party submissions against hub.

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 31, 2022, 5:38pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/16 "2022-05-31T17:38:48Z")

</div>

@cocreature,

Is “Submit Multi” only supported in sandbox (via **Java** )?

I’m asking because i’m pretty sure it’s supported in **DAML** against damlhub…

---

<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:** [May 31, 2022, 5:46pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/17 "2022-05-31T17:46:31Z")

</div>

It’s supported against any ledger. The issue is not the lack of ledger support in Daml Hub but the fact that the Hub IAM does not issue you a token for multiple parties.

---

<div class="post-metadata">

**Author:** ![liav](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/liav/32/3304_2.png) [@liav](https://forum.canton.network/u/liav)\
**Post date:** [May 31, 2022, 7:58pm UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/18 "2022-05-31T19:58:13Z")

</div>

I thought that “Submit Multi” for two parties receives as an input two separated tokens. According to you it requires a **single** token allocated for multiple parties and that’s not supported?

---

<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:** [June 1, 2022, 6:12am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/19 "2022-06-01T06:12:10Z")

</div>

Exactly.

---

<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:** [June 2, 2022, 2:32am UTC](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638/20 "2022-06-02T02:32:59Z")

</div>

@liav To correct @cocreature slightly, you have to ask hub to create a multiparty token for you, which you can do using the cli [damlhub-cli - npm](https://www.npmjs.com/package/damlhub-cli), please see

`damlhub-cli ledger multipartyToken`

With that token you can make `submitMulti` requests as those are natural requests via the LedgerAPI. The only wrinkle is that all of the parties that you aggregate into a single token must be **controlled** by the party that created the ledger; which I think is true in your case.

[Next page](https://forum.canton.network/t/how-to-fetch-a-contract-from-dumlhub-ledger-via-java/4638.md?page=2)
