Source to get .did file from any canister

I would suggest using @ic-reactor, a library I’m currently developing. You can easily use the CandidAdapter feature to get the candid definition as an idlFactory, which can be used directly to create an actor:

// 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);
}

This approach is similar to the one used by candid-ui.

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

npm install @ic-reactor/parser

Then, initialize the wasm parser after creating the CandidAdapter:

adapter.initializeParser();

Additionally, if you are using React, you might want to try @ic-reactor/react. 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. As you can see, it does not make a call to convert candid into JavaScript.

2 Likes