How to generate .did. files for an actor class?

In example/ directory I have dfx.json:

  "canisters": {
    "example_backend": {
      "main": "src/example_backend/",
      "type": "motoko"
    "example_frontend": {
      "dependencies": [
      "frontend": {
        "entrypoint": "src/example_frontend/src/index.html"
      "source": [
      "type": "assets"
  "defaults": {
    "build": {
      "args": "",
      "packtool": "vessel sources"
  "output_env_file": ".env",
  "version": 1

It generates .did. files for example_backend and example_frontend, but does not generate .did. files for these canisters that are created by example_backend from actor classes. How to generate these .did. files?

1 Like

I would guess you have to make the Motoko compiler do it for you. If you put the canister that your backend is deploying in a dfx project and dfx build it, then you’ll end up with the .did files. If you don’t want to go that route I guess you have to build the moc invocation yourself. You can find moc in the folder that dfx cache show points to, and IIRC if you build with verbose mode (-vv) you’ll get the moc invocations printed

1 Like

Is it possible to put the canister that your backend is deploying in a dfx project, but dfx deploy not to try to deploy that canister?

If there is no such feature, then it is a good proposal to add a boolean argument like dontDeploy to prevent dfx deploy to deploy the canister.

You can declare it as remote for the networks that you don’t want to deploy it on

How to declare it as remote?

What does it mean, remote?

Here’s an example of a remote canister. The intention behind the feature is that you can say that on certain networks you don’t want to deploy that canister. In this example, you only want to install the ledger if you’re running locally since there is no pre-installed ledger. On mainnet, you of course want to use the real ledger, so you don’t deploy your own

I tried to update declaration/index/index.did* files by the following command:

`dfx cache show`/moc `vessel sources` --idl -c src/index/

declaration/index/index.did* however were not updated. What is my error?

dfx generate ???

You seem to read my question not attentively.

I am asking for .did. files for an actor class, not for a canister.

sorry, not into motoko, i thought actor is treated like a single canister.

Actor is indeed treated as a single canister, but I ask for actor classes, not actors.

You recommend to use a remote canister for .did. generation. But an example of remote canister, that you pointed, refers to an already generated .did. file rather than generates it:

  "ledger": {
    "type": "custom",
    "candid": "",
    "wasm": "",
    "remote": {
      "id": {
        "ic": "ryjl3-tyaaa-aaaaa-aaaba-cai"
    "replica": {

Are you sure dfx does not build the .did files? For me dfx builds the .did file also for an actor class.

Not sure which commands you have tried. Maybe dfx generate? That command creates the .did file for me. With your dfx.json it would be in src/declarations/backend_example/backend_example.did. The only difference with an actor class is that there are service arguments, i.e. in the line
service : (...) -> the part (...) is non-empty. I would be interested in why it doesn’t do that for you.

Anyway, if dfx generate doesn’t work then another way is to use dfx build. That command does not deploy the canister. But it does create a canister id (an empty canister), i.e. it still needs a replica (dfx start) running in the background. So what you are looking for is dfx build --check. With the --check option it does not even create a canister id, hence you don’t need a replica running in the background. It builds .did and .wasm in .dfx/local/canisters/example_backend/. You can run dfx build --check example_backend to only build that one canister and not all that are defined in dfx.json.

Make it a canister of type Motoko. There you don’t need to specify a .did because moc can generate it for you