# Source to get .did file from any canister

**URL:** <https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294>\
**Category:** Developers\
**Tags:** Discussing, TypeScript\
**Created:** [February 5, 2024, 3:49am UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294 "2024-02-05T03:49:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![baolongt](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@baolongt](https://forum.dfinity.org/u/baolongt)\
**Post date:** [February 5, 2024, 3:49am UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/1 "2024-02-05T03:49:10Z")

</div>

I’m deploying some service outside of Dfinity canister. Any with JS agent and did file I can make few interesting thing with app outside of canister. For every canister I used icscan to get the did file but this morning I found that there is some canister like Openchat’s canister cannot get the did file from icscan.

So my question is what is mechanism to public the did file in icscan and is that any way to get the did file with these private canisters?

---

<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:** [February 5, 2024, 8:13am UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/2 "2024-02-05T08:13:00Z")

</div>

If canisters expose their .did file then the convention is to do it in the `candid:service` metadata section. You can fetch that section with `dfx canister --ic metadata <canister id> candid:service`

---

<div class="post-metadata">

**Author:** ![jaesharma](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jaesharma/32/7297_2.png) [@jaesharma](https://forum.dfinity.org/u/jaesharma)\
**Post date:** [June 26, 2024, 4:56pm UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/3 "2024-06-26T16:56:03Z")

</div>

How can we do the same programatically? can we fetch the candid of canister using javascript?

---

<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:** [June 26, 2024, 7:26pm UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/4 "2024-06-26T19:26:35Z")

</div>

Yes, there’s a utility method in agent-js to fetch the candid:

```js
import { fetchCandid } from "@dfinity/agent";

const agent = new HttpAgent()
const did = await fetchCandid(CANISTER_ID, agent);

```

Keep in mind this will be a string containing the did file contents. If you want to use this to e.g. create an actor, you’d still need to convert this into js/ts definition files.

---

<div class="post-metadata">

**Author:** ![jaesharma](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jaesharma/32/7297_2.png) [@jaesharma](https://forum.dfinity.org/u/jaesharma)\
**Post date:** [June 27, 2024, 2:49am UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/5 "2024-06-27T02:49:58Z")

</div>

to convert did into js/ts i found, we can get it from this canister:

```auto
import { IDL } from "@dfinity/candid";
import { Actor, HttpAgent } from "@dfinity/agent";

export const candidToJS = async (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 agent = new HttpAgent({ host: "https://icp-api.io" });

  const didjs = Actor.createActor(didjs_interface, {
    agent,
    canisterId: candidCanister,
  });
  const js: any = await didjs.did_to_js(candid_source);
  if (Array.isArray(js) && js.length === 0) {
    return undefined;
  }
  return js[0];
};

```

do we have any other offline solution? i mean where we generate didtojs on our end and don’t have to make any call to other service or canister.

---

<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:** [June 27, 2024, 8:10am UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/6 "2024-06-27T08:10:55Z")

</div>

Running the following in WASM is the current solution I’m working with: [candid/rust/candid\_parser at master · dfinity/candid · GitHub](https://github.com/dfinity/candid/tree/master/rust/candid_parser)

---

<div class="post-metadata">

**Author:** ![b3hr4d](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/b3hr4d/32/9755_2.png) [@b3hr4d](https://forum.dfinity.org/u/b3hr4d)\
**Post date:** [June 28, 2024, 6:29pm UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/7 "2024-06-28T18:29:36Z")

</div>

> [@jaesharma](#):
>
> Can we fetch the candid of a canister using JavaScript?

I would suggest using `@ic-reactor`, a library I’m currently developing. You can easily use the [CandidAdapter](https://b3pay.github.io/ic-reactor/functions/core.createCandidAdapter.html) feature to get the candid definition as an `idlFactory`, which can be used directly to create an actor:

```typescript
// adapter.ts
import { createAgentManager, createCandidAdapter } from "@ic-reactor/core";

export const agentManager = createAgentManager({ withProcessEnv: true });

export const adapter = createCandidAdapter({ agentManager });

export async function getCandidDefinition(canisterId: string) {
  return adapter.getCandidDefinition(canisterId);
}

```

> [@jaesharma](#):
>
> To convert DID into JS/TS, I found that we can get it from this canister:

This approach is similar to the one used by [candid-ui](https://github.com/dfinity/candid/tree/master/tools/ui).

> [@jaesharma](#):
>
> Do we have any other offline solutions?

Yes, there is also an offline solution using the wasm mentioned by @sea-snake. You just need to install:

```shell
npm install @ic-reactor/parser

```

Then, initialize the wasm parser after creating the `CandidAdapter`:

```typescript
adapter.initializeParser();

```

Additionally, if you are using React, you might want to try [@ic-reactor/react](https://forum.dfinity.org/t/introduction-ic-reactor-react-for-react-developers/27930/1). It has a built-in adapting actor feature that only requires you to specify the `canisterId` to interact with it!

I have developed this library and it is used extensively in the creation of B3Forge. Please also give a shot to the [B3Forge/playground](https://sehgq-cqaaa-aaaap-ahc4q-cai.icp0.io/playground). As you can see, it does not make a call to convert candid into JavaScript.

---

<div class="post-metadata">

**Author:** ![jaesharma](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jaesharma/32/7297_2.png) [@jaesharma](https://forum.dfinity.org/u/jaesharma)\
**Post date:** [June 29, 2024, 12:12am UTC](https://forum.dfinity.org/t/source-to-get-did-file-from-any-canister/27294/8 "2024-06-29T00:12:09Z")

</div>

ok i’ll check these out. thanks for the help.
