Skip to main content
Reference engineering 5 min read Updated

well-known-skills-icp-cli-references-dfx-migration

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.

RemoveReplace 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-environmentNot 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:

  1. npm uninstall @dfinity/agent @dfinity/candid @dfinity/principal vite-plugin-environment
  2. npm install @icp-sdk/core@^6 then npm install -D @icp-sdk/bindgen@^0.4.0 — bindgen is a build-time tool, so it belongs in devDependencies. Pin core explicitly; the rest of the SDK peers ^6 (see references/binding-generation.md)
  3. Delete src/declarations/ (dfx-generated bindings)
  4. Add **/src/bindings/ to .gitignore
  5. Commit the .did file(s) used by bindgen
  6. Add icpBindgen() to vite.config.js (see references/binding-generation.md)
  7. Replace actor setup code: use safeGetCanisterEnv from @icp-sdk/core + createActor from generated bindings (see references/binding-generation.md)
  8. Remove process.env.CANISTER_ID_* references — use the ic_env cookie instead

Command mapping

Taskdfxicp
Create projectdfx new my_projecticp new my_project
Start local networkdfx start --backgroundicp network start -d
Stop local networkdfx stopicp network stop
Builddfx buildicp build
Deploy alldfx deployicp deploy
Deploy to mainnetdfx deploy --network icicp deploy -e ic
Call canisterdfx canister call X method '(args)'icp canister call X method '(args)'
Get canister IDdfx canister id Xicp canister status X --id-only
Canister statusdfx canister status Xicp canister status X
List canistersdfx canister lsicp canister list
Create identitydfx identity new my_idicp identity new my_id
Set default identitydfx identity use my_idicp identity default my_id
Show principaldfx identity get-principalicp identity principal
Export identitydfx identity export my_idicp identity export my_id
Delete identitydfx identity remove my_idicp identity delete my_id
Get account IDdfx ledger account-idicp identity account-id
Check ICP balancedfx ledger balanceicp token balance
Check cyclesdfx wallet balanceicp cycles balance

Configuration mapping

dfx.jsonicp.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-identity

Canister 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:

  1. Deleted files: dfx.json and canister_ids.json no longer exist
  2. Created files: icp.yaml exists. .icp/data/mappings/ic.ids.json exists and is committed (if project has mainnet canisters)
  3. .gitignore: contains .icp/cache/, does not contain .dfx
  4. No stale port references: search the codebase for 4943 — there should be zero matches
  5. No dfx env patterns: search for output_env_file, CANISTER_ID_, DFX_NETWORK — there should be zero matches in config and source files
  6. Frontend packages (if project has TypeScript bindings): @dfinity/agent is not in package.json, @icp-sdk/core and @icp-sdk/bindgen are. src/declarations/ is deleted, src/bindings/ is in .gitignore
  7. Candid files: .did files used by @icp-sdk/bindgen are committed
  8. Build succeeds: icp build completes without errors
  9. Config is correct: icp project show displays the expected expanded configuration
  10. README: references icp commands (not dfx), says “local network” (not “replica”), shows correct port
  11. No env.json fetches: frontend code uses safeGetCanisterEnv() from @icp-sdk/core, not fetch('/env.json') or process.env.CANISTER_ID_*
  12. No fetchRootKey() calls: root key comes from the ic_env cookie (canisterEnv?.IC_ROOT_KEY) — not from agent.fetchRootKey()
  13. No build-time env injection: plugins like vite-plugin-environment are removed — canister IDs and root key are resolved at runtime via ic_env
  14. createActor sets rootKey: prefer { agentOptions: { rootKey } }; a pre-built { agent } is fine if it comes from await HttpAgent.create(), not the deprecated new HttpAgent() (see createActor above)