Example Code

In addition to our tutorials section, here are some quick references! You can also check out our Examples Repo, Awesome Dfinity, a curated set of open-source projects and examples from our community, or Hackathon Projects!

Motoko and Rust Examples

We first list the examples that are available in both Motoko and Rust:

  • Hello: the dapp equivalent of hello world, with a separate backend and frontend canister serving a web page.

Motoko Examples

Motoko Playground

Some (but not all) of the following Motoko examples are available via the Motoko Playground. The playground provides the simplest enviromnent for trying out pure Motoko examples without having to download and learn to use the SDK, but does not support applications with frontends.

Basic

This is collection of basic getting-started Motoko examples, which demonstrate basic concepts of the IC and the Motoko language.

  • actor_reference — IC management canister as an actor (reference).

  • counter — basic (stable) variable demo.

  • echo — basic query function demo.

  • factorial — basic factorial demo.

  • hello-world — basic hello world demo.

  • hello_cycles — Receive and transfer cycles and check your balance.

  • whoami — a simple canister that reports the Principal of its caller.

Intermediate

The intermediate examples demonstrate how to use some of the basic concepts in common use cases. The examples show how to build simple backend functionalities in Motoko.

  • classes — dynamic actor (class) instantiation.

  • pub-sub-- multiple canisters, with publisher-subscriber inter-canister calls.

  • quicksort — sorting an array, via Quick Sort, in Motoko.

  • simple-to-do — CRUD-like demo service, sans a front end; see also: phone-book and superheroes.

  • calc — more advanced version of counter demo.

Minimal front end.

These two examples show how to create simple frontends in JavaScript. Where the previous examples did not provide a frontend, these two examples provide both a frontend and a backend.

  • random_maze — random maze generation, with IC-based randomness.

  • cert_var — simple certified variable (a single 32-bit number), with client-side certificate validation.

Full stack.

These examples demonstrates how to build dapps on the IC, with frontends based on React and TypeScript.

  • life — Conway’s Game of Life, running in a Motoko Canister. Demonstrates upgrades among three versions and state migration using stable variables.

  • phone-book — CRUD-like demo service.

  • superheroes — a simple CRUD dapp.

  • Auth Client Demo - simple authentication with Internet Identity.

  • IC-Avatar - full-stack Motoko + React with more advanced authentication, image uploads, and optimistic updates.

Advanced Examples

Large examples contributed by the community and not necessarily up-to-date:

  • Reversi game, in Motoko src accompanying Blog post.

  • QR code generator in Motoko src accompanying Medium article.

  • IC Vault - end-to-end Encryption.

  • Certified asset canister in Motoko src forum post - demonstrating a larger application of Internet Computer certified variables.

Rust Examples

Beginner

This Rust example is similar to the intermediate Motoko pub-sub example.

Videos

These videos from our Community Conversations series cover an introduction to building dapps in Rust, and best practices for building dapps in Rust.

More Rust examples coming soon!

C

For examples of projects written in the C programming language for the Internet Computer, see C++ sample projects.

these examples are likely to bit-rot and may need updating and are provided for illustration only.

Wat (Wasm textual format)

Just for fun, we also have a simple example written in pure Wasm textual format: