dfx → icp Migration
Local network port change
dfx serves the local network on port 4943. icp-cli uses port 8000. When migrating, search the project for hardcoded references to 4943 (or localhost:4943) and update them to 8000. Better yet, use icp network status --json to get the api_url dynamically (see references/dev-server.md). Common locations to check:
- Vite/webpack proxy configs (e.g.,
vite.config.ts) - README documentation
- Test fixtures and scripts
Remove .env file and output_env_file
dfx generates a .env file with CANISTER_ID_* variables via output_env_file in dfx.json. icp-cli does not use .env files for canister IDs — remove output_env_file from config and delete any dfx-generated .env file. Also remove dfx-specific environment variables from .env files (e.g., DFX_NETWORK, NETWORK).
Replace code that reads canister IDs from environment variables or JSON files with the ic_env cookie pattern:
// Before (dfx): canister ID from env var or env.json
const backendId = process.env.CANISTER_ID_BACKEND;
// or: const { backend_canister_id } = await fetch('/env.json').then(r => r.json());
// After (icp-cli): canister ID from ic_env cookie
import { safeGetCanisterEnv } from "@icp-sdk/core/agent/canister-env";
const backendId = safeGetCanisterEnv()?.["PUBLIC_CANISTER_ID:backend"];
Note: safeGetCanisterEnv() also returns IC_ROOT_KEY (as a Uint8Array) — the ic_env cookie always carries the network’s root key, on local networks and mainnet alike. Passing it as the rootKey agent option replaces the need for agent.fetchRootKey().
Frontend package migration
Since @icp-sdk/bindgen generates code that depends on @icp-sdk/core, projects with TypeScript bindings must upgrade from @dfinity/* packages. This is not optional — dfx generate does not exist in icp-cli, and @icp-sdk/bindgen is the only supported way to generate bindings.
| Remove | Replace with |
|---|---|
@dfinity/agent | @icp-sdk/core |
@dfinity/candid | @icp-sdk/core |
@dfinity/principal | @icp-sdk/core |
dfx generate (declarations) | @icp-sdk/bindgen (Vite plugin or CLI) |
vite-plugin-environment | Not needed — use ic_env cookie |
src/declarations/ (generated by dfx) | src/bindings/ (generated by @icp-sdk/bindgen) |
createActor takes agent options. The @dfinity/agent pattern builds an agent and passes { agent }. With @icp-sdk/bindgen, pass { agentOptions } and the binding builds the agent (a pre-built { agent } still works and is used as-is). When calling the network that serves the page, set rootKey from the ic_env cookie — without it the agent defaults to the mainnet root key and local calls fail verification (for a page calling a different network, see references/binding-generation.md). Carry the identity over into agentOptions: without it, calls are anonymous. Do not carry new HttpAgent() over: the constructor is deprecated in @icp-sdk/core; use await HttpAgent.create() if you need your own agent.
// Before (@dfinity/agent): pass a pre-built agent
import { HttpAgent } from "@dfinity/agent";
const agent = new HttpAgent({ identity, host });
createActor(canisterId, { agent });
// After (@icp-sdk/bindgen): pass agent options — the binding creates the agent
import { safeGetCanisterEnv } from "@icp-sdk/core/agent/canister-env";
const canisterEnv = safeGetCanisterEnv();
createActor(canisterEnv?.["PUBLIC_CANISTER_ID:backend"], {
// No host: the agent's default resolves to the page origin on known gateway
// hosts (ic0.app, icp0.io, localhost, 127.0.0.1) and to https://icp-api.io
// everywhere else — including custom domains, which serve only the HTTP
// gateway, not /api/v2. Do not set host: window.location.origin.
// Keep the identity from the old agent; without it, calls are anonymous.
agentOptions: { identity, rootKey: canisterEnv?.IC_ROOT_KEY }
});
Steps:
npm uninstall @dfinity/agent @dfinity/candid @dfinity/principal vite-plugin-environmentnpm install @icp-sdk/core@^6thennpm install -D @icp-sdk/bindgen@^0.4.0— bindgen is a build-time tool, so it belongs indevDependencies. Pin core explicitly; the rest of the SDK peers^6(seereferences/binding-generation.md)- Delete
src/declarations/(dfx-generated bindings) - Add
**/src/bindings/to.gitignore - Commit the
.didfile(s) used by bindgen - Add
icpBindgen()tovite.config.js(seereferences/binding-generation.md) - Replace actor setup code: use
safeGetCanisterEnvfrom@icp-sdk/core+createActorfrom generated bindings (seereferences/binding-generation.md) - Remove
process.env.CANISTER_ID_*references — use theic_envcookie instead
Command mapping
| Task | dfx | icp |
|---|---|---|
| Create project | dfx new my_project | icp new my_project |
| Start local network | dfx start --background | icp network start -d |
| Stop local network | dfx stop | icp network stop |
| Build | dfx build | icp build |
| Deploy all | dfx deploy | icp deploy |
| Deploy to mainnet | dfx deploy --network ic | icp deploy -e ic |
| Call canister | dfx canister call X method '(args)' | icp canister call X method '(args)' |
| Get canister ID | dfx canister id X | icp canister status X --id-only |
| Canister status | dfx canister status X | icp canister status X |
| List canisters | dfx canister ls | icp canister list |
| Create identity | dfx identity new my_id | icp identity new my_id |
| Set default identity | dfx identity use my_id | icp identity default my_id |
| Show principal | dfx identity get-principal | icp identity principal |
| Export identity | dfx identity export my_id | icp identity export my_id |
| Delete identity | dfx identity remove my_id | icp identity delete my_id |
| Get account ID | dfx ledger account-id | icp identity account-id |
| Check ICP balance | dfx ledger balance | icp token balance |
| Check cycles | dfx wallet balance | icp cycles balance |
Configuration mapping
| dfx.json | icp.yaml |
|---|---|
"type": "rust" | recipe.type: "@dfinity/[email protected]" |
"type": "motoko" | recipe.type: "@dfinity/[email protected]" |
"type": "assets" | recipe.type: "@dfinity/[email protected]" (recommended) or @dfinity/[email protected] (legacy). Not a drop-in: static-site is a different canister using _headers/_redirects (not .ic-assets.json5), and switching a deployed canister needs a reinstall — see the static-site skill’s migration guide. |
"package": "X" | recipe.configuration.package: X — optional as of @dfinity/[email protected], which defaults it to the canister name. Drop it when the Cargo [package] name already matches. |
"candid": "X" | recipe.configuration.candid: X |
"main": "X" | Not recipe.configuration.main. The @dfinity/motoko@v5+ recipe compiles via mops build, so main moves to mops.toml as [canisters.<canister-name>] main = "X". The [canisters.\<name\>] key must exactly match the canister name in icp.yaml. |
"source": ["dist"] | recipe.configuration.dir: dist |
"dependencies": [...] | Not needed — use Canister Environment Variables |
"output_env_file": ".env" | Not needed — use ic_env cookie |
dfx generate | @icp-sdk/bindgen Vite plugin |
--network ic | -e ic |
Custom Motoko builds
If the project uses a custom build script instead of the @dfinity/motoko recipe (e.g., for special compiler flags), replace $(dfx cache show)/moc with mops:
# Before (dfx): compiler from dfx cache
MOC="$(dfx cache show)/moc"
# After (icp-cli): compiler from mops toolchain
MOC="$(mops toolchain bin moc)"
# For package resolution, replace dfx-generated package flags:
SOURCES="$(mops sources)"
Ensure mops.toml is findable from the build working directory (project root for inline canisters, canister directory for path-based canisters — see pitfall #15 in SKILL.md) with the compiler version pinned in [toolchain].
Identity migration
# Export from dfx, import to icp-cli
dfx identity export my-identity > /tmp/my-identity.pem
icp identity import my-identity --from-pem /tmp/my-identity.pem
rm /tmp/my-identity.pem
# Verify principals match
dfx identity get-principal --identity my-identity
icp identity principal --identity my-identityCanister ID migration
If you have existing mainnet canisters managed by dfx, migrate the IDs from canister_ids.json to icp-cli’s mapping file:
# Get IDs from dfx
dfx canister id frontend --network ic
dfx canister id backend --network ic
# Create mapping file for icp-cli
mkdir -p .icp/data/mappings
cat > .icp/data/mappings/ic.ids.json << 'EOF'
{
"frontend": "xxxxx-xxxxx-xxxxx-xxxxx-cai",
"backend": "yyyyy-yyyyy-yyyyy-yyyyy-cai"
}
EOF
# Delete the dfx canister ID file — icp-cli uses .icp/data/mappings/ instead
rm -f canister_ids.json
# Commit to version control
git add .icp/data/Post-Migration Verification
After migrating a project from dfx to icp-cli, verify the following:
- Deleted files:
dfx.jsonandcanister_ids.jsonno longer exist - Created files:
icp.yamlexists..icp/data/mappings/ic.ids.jsonexists and is committed (if project has mainnet canisters) .gitignore: contains.icp/cache/, does not contain.dfx- No stale port references: search the codebase for
4943— there should be zero matches - No dfx env patterns: search for
output_env_file,CANISTER_ID_,DFX_NETWORK— there should be zero matches in config and source files - Frontend packages (if project has TypeScript bindings):
@dfinity/agentis not inpackage.json,@icp-sdk/coreand@icp-sdk/bindgenare.src/declarations/is deleted,src/bindings/is in.gitignore - Candid files:
.didfiles used by@icp-sdk/bindgenare committed - Build succeeds:
icp buildcompletes without errors - Config is correct:
icp project showdisplays the expected expanded configuration - README: references
icpcommands (notdfx), says “local network” (not “replica”), shows correct port - No
env.jsonfetches: frontend code usessafeGetCanisterEnv()from@icp-sdk/core, notfetch('/env.json')orprocess.env.CANISTER_ID_* - No
fetchRootKey()calls: root key comes from theic_envcookie (canisterEnv?.IC_ROOT_KEY) — not fromagent.fetchRootKey() - No build-time env injection: plugins like
vite-plugin-environmentare removed — canister IDs and root key are resolved at runtime viaic_env createActorsetsrootKey: prefer{ agentOptions: { rootKey } }; a pre-built{ agent }is fine if it comes fromawait HttpAgent.create(), not the deprecatednew HttpAgent()(seecreateActorabove)