Skip to main content

Hello, world!

This guide shows how to get started developing on ICP quickly by deploying your first 'Hello, world!' smart contract.

Install dependencies

Install dfx

Download and install the latest version of dfx. dfx is a CLI tool used for developing and deploying ICP smart contracts:

sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"

Install Node.js

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
nvm install 18

Start the local replica

dfx start

This command starts a local instance of the ICP replica using a single node testnet. The output will return a dashboard URL that can be used to view information about the local replica:

Running dfx start for version 0.15.2
Initialized replica.
Dashboard: http://localhost:35375/_/dashboard

While dfx start is running, you will not be able to run other commands in the terminal window. You can open another window, or start dfx in the background with the dfx start --background flag.

Create a new project

Use dfx new <project_name> to create a new project:

dfx new hello

You will be prompted to select a language that your backend canister will use:

? Select a backend language: ›  
❯ Motoko
Rust
TypeScript (Azle)
Python (Kybra)

Then, select a frontend framework for your frontend canister, or select 'No frontend canister':

  ? Select a frontend framework: ›  
❯ SvelteKit
React
Vue
Vanilla JS
No JS template
No frontend canister

Lastly, you can include extra features to be added to your project:

  ? Add extra features (space to select, enter to confirm) ›
⬚ Internet Identity
⬚ Bitcoin (Regtest)
⬚ Frontend tests

Smart contract code

Motoko is an Actor-based language and it represents the smart contract as an actor with various methods that the users and other smart contract can call.

This hello-world actor has a single function called greet. It is marked as query because it doesn't modify the state of the actor. The function accepts a name as input and returns a greetings text.

// src/hello_backend/main.mo
actor {
public query func greet(name : Text) : async Text {
return "Hello, " # name # "!";
};
};

Deploy locally

Deploy the project's canisters locally:

dfx deploy

This command performs several functions in the background:

  • It sets up a special cycle-wallet smart contract for storing cycles. All operations require payments in cycles. In your local testnet, this wallet comes prefilled with cycles.

  • It builds the smart contracts defined in the dfx.json file into Wasm binaries.

  • It creates smart contracts on the local testnet and saves their addresses in the file ./.dfx/local/canister_ids.json.

  • It installs the Wasm binaries onto the new smart contracts.

Call the smart contracts from the CLI

dfx canister call hello_backend greet world

Output:

("Hello, world!")

This call performs several functions in the background:

  • First, it looks up the smart contract's address (known as the canister id) using the smart contract's name hello_backend in the file ./.dfx/local/canister_ids.json.

  • Then, it looks up the smart contract's interface in the file src/declarations/hello_backend/hello_backend.did.

  • It looks up the signature of function greet in the interface.

  • It constructs a message for the smart contract's greet endpoint with the given name text as an argument.

  • It signs the message with the private key of the developer account.

  • It sends the message to the local testnet (replica).

  • Once the message has been executed, the result was decoded using the interface file and printed to the terminal window.

Alternatively, you can interact with the smart contract via the web browser if a frontend smart contract has been deployed. Learn more about using the frontend smart contract in the web browser

Next steps