Migrating
This guide explains how to migrate your bindings generation from the previous tools like dfx or didc to @icp-sdk/bindgen.
From dfx (Rust canister)
Section titled “From dfx (Rust canister)”Before
Section titled “Before”If you were using dfx to generate bindings, your configuration was likely to be like this:
{ "canisters": { "hello-world-backend": { "candid": "src/hello-world-backend/hello-world-backend.did", "package": "hello-world-backend", "type": "rust" }, ... }}And you were generating bindings with the following command:
dfx generateThis was typically writing the bindings’ files in the src/declarations/hello-world-backend directory, unless you configured the canisters.hello-world-backend.declarations field.
Now, you can migrate to @icp-sdk/bindgen in CLI mode by running the following command (typically in a script in the package.json file of the frontend):
icp-bindgen --did-file ./src/hello-world-backend/hello-world-backend.did --out-dir ./src/hello-world-frontend/backendIn case you configured the
canisters.hello-world-backend.declarationsfield, you can pass thecanisters.hello-world-backend.declarations.outputvalue to the--out-dirflag. Note that in@icp-sdk/bindgen, there’s no way to avoid generating the TypeScript declarations.
This will generate the bindings in the src/hello-world-frontend/backend directory. See the Bindings Structure page for more information on the generated files.
The same can be applied for a canister configured as
type: customin thedfx.jsonfile.
From dfx (Motoko canister)
Section titled “From dfx (Motoko canister)”Before
Section titled “Before”If you were using dfx to generate bindings, your configuration was likely to be like this:
{ "canisters": { "hello-world-backend": { "main": "src/hello-world-backend/main.mo", "type": "motoko" }, ... }}And you were generating bindings with the following command:
dfx generateThis was typically writing the bindings’ files in the src/declarations/hello-world-backend directory, unless you configured the canisters.hello-world-backend.declarations field.
In Motoko, the Candid declaration file (.did) is generated by dfx at build time. It’s typically written in the .dfx/<network-name>/canisters/hello-world-backend/hello-world-backend.did file.
Now, you can migrate to @icp-sdk/bindgen in CLI mode by running the following commands (typically in a script in the package.json file of the frontend):
dfx build --check hello-world-backend # can be skipped if you already built the canistericp-bindgen --did-file ./.dfx/<network-name>/canisters/hello-world-backend/hello-world-backend.did --out-dir ./src/hello-world-frontend/backendDuring the build,
dfxinjects a$DFX_NETWORKenvironment variable that contains the network name, which you can use to construct the path to the.didfile.
In case you configured the
canisters.hello-world-backend.declarationsfield, you can pass thecanisters.hello-world-backend.declarations.outputvalue to the--out-dirflag. Note that in@icp-sdk/bindgen, there’s no way to avoid generating the TypeScript declarations.
This will generate the bindings in the src/hello-world-frontend/backend directory. See the Bindings Structure page for more information on the generated files.
For a more sophisticated approach, you can configure the canisters.hello-world-backend.declarations field to write the .did file to a directory of your choice and then use the --did-file flag to point to it. For example:
{ "canisters": { "hello-world-backend": { "main": "src/hello-world-backend/main.mo", "type": "motoko", "declarations": { "bindings": ["did"], "output": "candid" } }, ... }}And then you can run the following commands:
dfx generate --check hello-world-backendicp-bindgen --did-file ./candid/hello-world-backend.did --out-dir ./src/hello-world-frontend/backendFrom didc
Section titled “From didc”Before
Section titled “Before”If you were using didc to generate bindings, you were likely running the following commands:
didc bind --target js ./service.did > ./service.did.jsdidc bind --target ts ./service.did > ./service.did.d.tsNow, you can migrate to @icp-sdk/bindgen in CLI mode by running the following command:
icp-bindgen --did-file ./service.did --out-dir ./serviceThis will generate the bindings in the service directory. See the Bindings Structure page for more information on the generated files.
Note: There’s no way to avoid generating the TypeScript declarations.