Skip to main content
Learn the fundamentals of NEAR smart contract development through these hands-on tutorials. Each tutorial builds on core concepts to help you master contract development.

Available tutorials

Hello NEAR

A simple contract that stores and retrieves a greeting message. Perfect for beginners.

Guest Book

Learn to store and retrieve data with a guest book contract that records messages.

Counter

Build a counter contract to understand state management and function calls.

Status Message

Create a social status message contract with collections and data structures.

What you’ll learn

  • How to structure a NEAR smart contract
  • Initializing contract state
  • Using the #[near] macro (Rust) or decorators (JavaScript)
  • Defining contract methods
  • Storing and retrieving data
  • Working with contract storage
  • Understanding storage costs
  • Managing state efficiently
  • View functions (read-only, no gas cost)
  • Change functions (modify state, require gas)
  • Payable functions (accept NEAR deposits)
  • Private functions (internal use only)
  • Writing unit tests
  • Testing with NEAR Workspaces
  • Deploying to testnet
  • Interacting with deployed contracts

Getting started

1

Set up your development environment

Install the required tools:
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install cargo-near
cargo install cargo-near

# Install NEAR CLI
curl --proto '=https' --tlsv1.2 -LsSf \
  https://github.com/near/near-cli-rs/releases/latest/download/near-cli-rs-installer.sh \
  | sh
2

Clone an example

Start with the Hello NEAR example:
git clone https://github.com/near-examples/hello-near-examples
cd hello-near-examples
3

Build and test

# Build the contract
cargo near build

# Run tests
cargo test
4

Deploy and interact

# Deploy to testnet
near contract deploy <your-account>.testnet \
  use-file ./target/near/contract.wasm \
  without-init-call \
  network-config testnet

# Call a method
near contract call-function as-transaction <your-account>.testnet \
  set_greeting json-args '{"greeting":"Hello!"}' \
  prepaid-gas '30 TeraGas' \
  attached-deposit '0 NEAR' \
  network-config testnet

Example: Hello NEAR contract

use near_sdk::{near, env, log};

#[near(contract_state)]
pub struct Contract {
    greeting: String,
}

impl Default for Contract {
    fn default() -> Self {
        Self {
            greeting: "Hello".to_string(),
        }
    }
}

#[near]
impl Contract {
    #[init]
    pub fn new(greeting: String) -> Self {
        Self { greeting }
    }
    
    pub fn get_greeting(&self) -> String {
        self.greeting.clone()
    }
    
    pub fn set_greeting(&mut self, greeting: String) {
        log!("Saving greeting: {}", greeting);
        self.greeting = greeting;
    }
}

Next steps

Cross-contract calls

Learn how contracts can interact with each other

Testing guide

Deep dive into testing smart contracts

Security best practices

Secure your smart contracts

NFT tutorial

Build a complete NFT contract

Build docs developers (and LLMs) love