# Fetch remote candid and convert it to JavaScript

**URL:** <https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825>\
**Category:** Developers\
**Created:** [October 4, 2024, 9:33am UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825 "2024-10-04T09:33:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbole/32/1334_2.png) [@rbole](https://forum.dfinity.org/u/rbole)\
**Post date:** [October 4, 2024, 9:33am UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/1 "2024-10-04T09:33:47Z")

</div>

Hey,  
in my use case I try to get some candid information from a remote canister and later I’m going to call a function from the remote canister.

To do so I use the following solution I have found:

```auto
# fetch the candid
const candid_source = await fetchCandid(canisterId, agent);

```

to fetch the candid as text from the remote canister, then I convert this text to javascript with the following function:

```auto
async candidToJS (candid_source: string){
    // call didjs canister
    const didjs_interface: IDL.InterfaceFactory = ({ IDL }) =>
      IDL.Service({
        did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ["query"]),
    });
  
    const candidCanister = 'a4gq6-oaaaa-aaaab-qaa4q-cai';
    const agent2 = HttpAgent.createSync({ host:"https://icp-api.io" });
    
    const didjs = Actor.createActor(didjs_interface, {
      agent: agent2,
      canisterId: candidCanister,
    });
  
    const js: any = await didjs['did_to_js'](candid_source);
    if (Array.isArray(js) && js.length === 0) {
      return undefined;
    }
    return js[0];
};

```

At the end this is working.

My question is, is there another way to convert the Candid text to JavaScript besides using this remote solution? Or, what is the recommended way to do that?

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [October 4, 2024, 9:41am UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/2 "2024-10-04T09:41:58Z")

</div>

Alternatively you can run the [rust crate](https://crates.io/crates/candid) that converts candid to JS in WASM with something like e.g. wasm-pack.

---

<div class="post-metadata">

**Author:** ![rbole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbole/32/1334_2.png) [@rbole](https://forum.dfinity.org/u/rbole)\
**Post date:** [October 4, 2024, 9:46am UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/3 "2024-10-04T09:46:29Z")

</div>

Thank you, but I want to do this in JavaScript directly in an application.

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [October 4, 2024, 9:48am UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/4 "2024-10-04T09:48:19Z")

</div>

Maybe @chenyan has a good idea?

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [October 4, 2024, 9:59am UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/5 "2024-10-04T09:59:57Z")

</div>

You can run WASM directly from JavaScript inside a browser. Also in a native application e.g. Android you can compile the Rust implementation to a native binding.

As far as I’m aware there’s no pure JavaScript implementation of candid as of now.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 4, 2024, 4:16pm UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/6 "2024-10-04T16:16:28Z")

</div>

Yep, either calling the remote didjs canister, or build a wasm and call in JS. For the latter, here is the doc: [candid - Rust](https://docs.rs/candid/latest/candid/#building-the-library-as-a-jswasm-package).

---

<div class="post-metadata">

**Author:** ![rbole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbole/32/1334_2.png) [@rbole](https://forum.dfinity.org/u/rbole)\
**Post date:** [October 4, 2024, 9:30pm UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/7 "2024-10-04T21:30:53Z")

</div>

Thanks for the answers. That sounds a bit exaggerated to me, but I will also try the wasm approach out of curiosity and to learn.

---

<div class="post-metadata">

**Author:** ![rbole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbole/32/1334_2.png) [@rbole](https://forum.dfinity.org/u/rbole)\
**Post date:** [October 5, 2024, 1:47pm UTC](https://forum.dfinity.org/t/fetch-remote-candid-and-convert-it-to-javascript/35825/8 "2024-10-05T13:47:22Z")

</div>

Yep, the wasm approach works too and is super fast 😯.
