Skip to content
/ ic-rust-nextjs Public template

A starter template for building decentralized applications on the Internet Computer using Next.js and Rust. Features a Rust backend for canister logic and a Next.js frontend for user interaction. Comes with comprehensive documentation for easy setup and deployment.

License

Notifications You must be signed in to change notification settings

b3hr4d/ic-rust-nextjs

Repository files navigation

Internet Computer - Rust + Next.js Template

A modern, full-stack template for building decentralized applications on the Internet Computer using Rust backend canisters and Next.js 16 frontend, powered by IC Reactor v3 for type-safe canister interactions.

Demo - Initial State

Demo - Greeting Response

πŸš€ Quick Start

Option 1: Use GitHub Template (Recommended)

Click the "Use this template" button at the top of this repository, then:

git clone https://github.com/YOUR_USERNAME/YOUR_NEW_REPO.git
cd YOUR_NEW_REPO
npm install

Option 2: Using degit (No Git History)

# Clone the template
npx degit b3hr4d/ic-rust-nextjs my-icp-app

# Navigate to project
cd my-icp-app

# Install dependencies
npm install

Option 3: Using Git Clone

# Clone with full history
git clone https://github.com/b3hr4d/ic-rust-nextjs.git my-icp-app
cd my-icp-app

# Remove original git history and start fresh
rm -rf .git
git init

# Install dependencies
npm install

πŸ’‘ Tip: After cloning, update the project name in package.json, dfx.json, and Cargo.toml to match your project.

✨ Features

  • πŸš€ Next.js 16 with React 19 & Turbopack
  • πŸ¦€ Rust Backend with IC CDK
  • ⚑ IC Reactor v3 - Type-safe canister interactions with TanStack Query caching
  • πŸ” Internet Identity - Built-in authentication support
  • 🎨 Modern Dark UI - Beautiful ICP-themed design

πŸ“ Project Structure

β”œβ”€β”€ backend/             # Rust canister code
β”‚   └── hello/           # Hello world canister
β”‚       β”œβ”€β”€ Cargo.toml   # Rust dependencies
β”‚       └── src/
β”‚           └── lib.rs   # Canister logic
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/      # React components (Login, Greeting)
β”‚   β”œβ”€β”€ declarations/    # Auto-generated canister interface
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”œβ”€β”€ reactor.ts   # IC Reactor setup (ClientManager + Reactor)
β”‚   β”‚   └── hooks.ts     # Typed React hooks for canister calls
β”‚   β”œβ”€β”€ pages/           # Next.js pages
β”‚   └── styles/          # CSS styles
β”œβ”€β”€ dfx.json             # Canister configuration
└── package.json

πŸ”§ Prerequisites

Before getting started, make sure you have the following installed:

  1. Node.js (v18+)
  2. DFINITY Canister SDK (dfx)
    sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
  3. Rust
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    rustup target add wasm32-unknown-unknown

πŸ› οΈ Installation & Setup

1. Install Dependencies

# Install Node.js dependencies
npm install

# Install Rust tools for canister development
npm run candid:install    # Install candid-extractor
npm run ic-wasm:install   # Install ic-wasm optimizer

2. Start Local Development

# Terminal 1: Start local IC replica
npm run dfx:start

# Terminal 2: Deploy canisters (including Internet Identity)
dfx deploy

# Terminal 3: Start Next.js development server
npm run dev

Open http://localhost:3000 to see the app.

πŸ”§ IC Reactor v3 Usage

This template uses IC Reactor v3's new patterns for type-safe canister interactions:

Reactor Setup (src/lib/reactor.ts)

import { ClientManager, Reactor } from "@ic-reactor/react"
import { QueryClient } from "@tanstack/react-query"

const queryClient = new QueryClient()
const clientManager = new ClientManager({ queryClient, withProcessEnv: true })

export const helloReactor = new Reactor<_SERVICE>({
  clientManager,
  idlFactory,
  canisterId
})

Generated Hooks (src/lib/hooks.ts)

import { createActorHooks, createAuthHooks } from "@ic-reactor/react"

// Authentication hooks
export const { useAuth, useAgentState } = createAuthHooks(clientManager)

// Canister hooks
export const { useActorQuery, useActorMutation } =
  createActorHooks(helloReactor)

Component Usage

import { useActorMutation } from "lib/hooks"

function Greeting() {
  const { mutate, data, isPending } = useActorMutation({
    functionName: "greet"
  })

  return (
    <button onClick={() => mutate(["World"])}>
      {isPending ? "Loading..." : data || "Click to greet"}
    </button>
  )
}

πŸ“¦ Available Scripts

Script Description
npm run dev Start Next.js development server
npm run build Build Next.js for production
npm run dfx:start Start local IC replica
npm run dfx:stop Stop local IC replica
dfx deploy Deploy all canisters
npm run dfx:generate Generate TypeScript declarations

🌐 Deploy to IC Mainnet

# Deploy to mainnet (requires cycles)
dfx deploy --network=ic

🎨 Customizing the Template

Rename Your Project

After cloning, update these files with your project name:

  1. package.json - Update name and description
  2. dfx.json - Rename canister from hello to your name
  3. backend/hello/Cargo.toml - Update package name
  4. src/declarations/ - Run npm run dfx:generate to regenerate

Add New Canisters

  1. Create a new directory in backend/your_canister/
  2. Add canister configuration to dfx.json
  3. Create a new Reactor in src/lib/reactor.ts
  4. Generate hooks with createActorHooks()

❓ Troubleshooting

Common Issues

dfx command not found

# Re-run the install script
sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"

Rust wasm32 target not found

rustup target add wasm32-unknown-unknown

Port 4943 already in use

npm run dfx:stop
npm run dfx:start

Candid generation fails

npm run candid:install
npm run dfx:generate

πŸ“š Resources

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

MIT License - see LICENSE for details.


Built with ❀️ for the Internet Computer community

About

A starter template for building decentralized applications on the Internet Computer using Next.js and Rust. Features a Rust backend for canister logic and a Next.js frontend for user interaction. Comes with comprehensive documentation for easy setup and deployment.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors 2

  •  
  •