Skip to main content

Hello, world!

Beginner
Getting started
Tutorial

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

Choosing a developer environment

To deploy a simple 'Hello, world!' smart contract, you can use a variety of developer environments, such as:

  • Native installation of tools on Linux, Mac, or Windows (using WSL) systems.

  • Cloud-based environments such as Gitpod or GitHub Codespaces.

Each developer environment uses dfx, a command-line tool that is used for developing and deploying smart contracts on ICP.

Which environment should you choose?

Native installation requires downloading and installing dfx and its dependencies manually. Using a native installation of dfx is better suited for large projects.

Gitpod and GitHub Codespaces are cloud-based developer environments that can be accessed via a web browser. The Gitpod and GitHub Codespace examples linked on this page come with the necessary tools and dependencies pre-installed. These environments use limited cloud resources and may not scale well for building large, complex projects.

Motoko variation

Open the Motoko variation in Gitpod

Rust variation

Open the Rust variation in Gitpod

Smart contract code

The default Hello, world! dapp uses dfx's default template which contains two canisters, hello_backend and hello_frontend.

The hello_frontend canister is used to store the dapp's frontend assets. This includes files such as HTML, CSS, JavaScript, React, images, and videos.

The hello_backend canister is used to store the dapp's functions and core logic.

Application architecture

It is important to note that a canister is capable of storing both the frontend assets and backend code. However, dfx by default uses a project template with a dedicated canister for the frontend since this allows any language to be used for the backend canister without needing to use a library for the assets storage API.

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 to the local environment

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