Your First Smart Contract
Welcome! NEAR accounts can store small apps known as smart contracts. In this quick tutorial, we will guide you in creating your first contract on the NEAR testnet!
Join us in creating a friendly auction contract, which allows users to place bids, track the highest bidder and claim tokens at the end of the auction.
Prefer an online IDE?
Want to jump right into the code without setting up a local dev environment?
Check out NEAR Playground for an easy-to-use online IDE with pre-configured templates.

Prerequisitesβ
Before starting, make sure to set up your development environment.
Working on Windows?
See our blog post getting started on NEAR using Windows for a step-by-step guide on how to set up WSL and your environment
- π¦ Rust
- π JavaScript
- π Python
# Install Rust: https://www.rust-lang.org/tools/install
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Contracts will be compiled to wasm, so we need to add the wasm target
rustup target add wasm32-unknown-unknown
# Install NEAR CLI-RS to deploy and interact with the contract
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/latest/download/near-cli-rs-installer.sh | sh
# Install cargo near to help building the contract
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh
# Install Node.js using nvm (more options in: https://nodejs.org/en/download)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
nvm install latest
# β οΈ For Mac Silicon users only, Rosetta is needed to compile contracts
# /usr/sbin/softwareupdate --install-rosetta --agree-to-license
# Install NEAR CLI to deploy and interact with the contract
npm install -g near-cli-rs@latest
Python quickstart tutorial is coming soon!
In the meantime, please check out the hello-near example.
Creating the Contractβ
Create a smart contract by using one of the scaffolding tools and following their instructions:
- π¦ Rust
- π JavaScript
- π Python
cargo near
Creating a project using cargo near new
npx create-near-app@latest
Creating a project using npx create-near-app@latest
When prompted to choose a template, select the basic Auction template to scaffold the auction contract
Python quickstart tutorial is coming soon!
In the meantime, please check out the hello-near example.
For this tutorial we chose to name the project auction, but feel free to use any name you prefer
The Contractβ
The auction smart contract allows users to place bids, track the highest bidder and claim tokens at the end of the auction.
Do not worry about the code just yet β for now, it is enough to know that the most relevant function is bid, which allows users to place bids by attaching NEAR tokens:
- π¦ Rust
- π JavaScript
- π Python
Loading...
Loading...
Python quickstart tutorial is coming soon!
In the meantime, please check out the hello-near example.
Besides bid, the contract exposes methods to initialize the auction (init), query the highest bidder (get_highest_bid), and claim tokens once the auction ends (claim).
Test the Contractβ
Lets make sure the contract is working as expected by running its tests. Simply run the test command, the contract will then be compiled and deployed to a local sandbox for testing:
- π¦ Rust
- π JavaScript
- π Python
cargo test
npm run test
Failing tests?
Make sure that you are using node v24 / 22 / 20, and that you have installed Rosetta if you have a Mac with Apple Silicon
Python quickstart tutorial is coming soon!
In the meantime, please check out the hello-near example.
Feel free to check the test files to see how they interact with the contract. In short, a local NEAR sandbox is created, the contract is deployed, and different methods are called to verify the expected behavior.
Build & Deploy the Contractβ
Now that we know the tests are passing, let us deploy the contract! First, we need to compile it into WebAssembly:
- π¦ Rust
- π JavaScript
- π Python
cargo near build non-reproducible-wasm
npm run build
Python quickstart tutorial is coming soon!
In the meantime, please check out the hello-near example.
Create an Accountβ
Let us now create a NEAR account where we will deploy the contract:
# Replace <contract-acc.testnet> with a nameΒ for your contract account
near create-account <contract-acc.testnet> --useFaucet
If you already have a testnet account and would like to use it instead, you can log in with the command near login.
Got an error on Windows?
When working on WSL - or any other headless Linux environment - you might encounter issues when trying to create an account as the cli tries to save the keys into the system's keychain.
In such cases, you can try the following command to create the account:
near account create-account sponsor-by-faucet-service <your-account-id.testnet> autogenerate-new-keypair save-to-legacy-keychain network-config testnet create
Deploy it!β
With the contract ready, we can now deploy it to the testnet account we created earlier:
- π¦ Rust
- π JavaScript
- π Python
near deploy <contract-acc.testnet> ./target/near/auction.wasm
near deploy <contract-acc.testnet> ./build/auction.wasm
Python quickstart tutorial is coming soon!
In the meantime, please check out the hello-near example.
Congrats! Your contract now lives in the NEAR testnet network.
Interacting with the Contractβ
To interact with your deployed smart contract, you can call its functions through the command line.
Initialize the Contractβ
Let us initialize the auction by setting when it ends and who receives the funds (the auctioneer):
# Get a timestamp for 5 minutes from now (in nanoseconds)
FIVE_MINUTES_FROM_NOW=$(( $(date +%s%N) + 5 * 60 * 1000000000 ))
# Initialize the auction
near call <contract-acc.testnet> init "{\"end_time\": \"$FIVE_MINUTES_FROM_NOW\", \"auctioneer\": \"influencer.testnet\"}" --useAccount <contract-acc.testnet>
Feel free to replace influencer.testnet with any valid testnet account β this is where the winning bid will be sent
Place a Bidβ
We can now place a bid in the auction by calling the bid method while attaching some NEAR deposit. On each bid, the highest bid and bidder information will be recorded in the contract's storage.
# Create a new account to place the bid
near create-account <bidder-account.testnet> --useFaucet
# Place a bid of 0.01 NEAR
near call <contract-acc.testnet> bid '{}' --deposit 0.01 --useAccount <bidder-account.testnet>
Note how in this case we are using the <bidder-account.testnet> account (remember to rename it!) to call the bid function, while attaching a deposit of 0.01 NEAR as our bid
Get Highest Bidβ
The get_highest_bid function only reads from the contract state, so it does not require a transaction or signature:
near view <contract-acc.testnet> get_highest_bid '{}'
Expected Output
{
"bidder": "<bidder-account.testnet>",
"amount": "10000000000000000000000"
}
Feel free to create as many bidder accounts as you want and place more bids to see how the highest bid changes!
Claimβ
After the auction ends, anyone can call the claim method, which will transfer the highest bid amount to the auctioneer and end the auction.
near call <contract-acc.testnet> claim '{}' --useAccount <contract-acc.testnet>
After the auction ends, the highest bidder can be determined by simply calling the get_highest_bid method again
Moving Forwardβ
Create a Frontend
Check the auction frontend tutorial to learn how to build a simple web app that interacts with the auction contract
Extend the Contract
Follow the auction NFT tutorial to award the highest bidder a Non-Fungible Token (NFT) and allow users to bid using Fungible Tokens (FT)
Learn More about the SDK
Check our Anatomy of a Contract page to understand the different components that make up a NEAR smart contract
Versioning for this article
At the time of this writing, this example works with the following versions:
- node:
22.18.0 - rustc:
1.86.0 - near-cli-rs:
0.22.0 - cargo-near:
0.16.1 - Python:
3.13 - near-sdk-py:
0.7.3 - uvx nearc:
0.9.2 - emscripten:
4.0.9(required for Python contracts)