Skip to main content
After your contract is ready, you can deploy it to the NEAR network for everyone to use. This guide shows you how to deploy using NEAR CLI, the recommended command-line tool for interacting with NEAR.

Prerequisites

Before deploying, ensure you have:

Step 1: Compile the Contract

First, compile your contract to WebAssembly:
cargo near build
This creates a WASM file in target/near/.
The compiled .wasm file is what gets deployed to the blockchain.

Step 2: Create an Account

For Testnet

Create a new testnet account using the faucet:
# Replace <your-account.testnet> with your desired name
near create-account <your-account.testnet> --useFaucet

For Mainnet

For mainnet, you’ll need:
  1. A wallet (e.g., MyNearWallet)
  2. Real NEAR tokens to create the account
  3. Log in via CLI:
near login
Mainnet Deployment ChecklistBefore deploying to mainnet:
  • ✅ Contract has been thoroughly tested
  • ✅ Contract has been audited (for high-value contracts)
  • ✅ Contract has been tested on testnet
  • ✅ You have sufficient NEAR for deployment and storage

Step 3: Deploy the Contract

Deploy your compiled contract to your account:
near deploy <your-account.testnet> ./target/near/contract.wasm
Network SelectionBy default, NEAR CLI uses testnet. To deploy to mainnet:
export NEAR_ENV=mainnet
near deploy <your-account.near> <path-to-wasm>

Deploy to Existing Account

If you already have an account:
# Login first
near login

# Deploy
near deploy <your-account.testnet> <path-to-wasm>
You can overwrite a contract by deploying another on top of it. The account’s logic will change, but the state will persist.

Step 4: Initialize the Contract

If your contract has an initialization method, call it after deployment:
near call <your-account.testnet> init '{"owner": "<your-account.testnet>"}' --useAccount <your-account.testnet>

Initialize During Deployment

You can initialize during deployment:
near deploy <your-account.testnet> <wasm-file> \
  --initFunction init \
  --initArgs '{"owner": "<your-account.testnet>"}'

Interacting with Deployed Contract

View Methods

View methods are read-only and free to call:
near view <your-account.testnet> get_greeting '{}'
Example output:
"Hello, World!"
View methods have a default gas limit of 200 TGas.

Call Methods

Call methods modify state and require gas:
near call <your-account.testnet> set_greeting '{"greeting": "Hello NEAR!"}' \
  --useAccount <your-account.testnet>

Call with Deposit

For payable functions, attach NEAR tokens:
near call <your-account.testnet> donate '{}' \
  --useAccount <your-account.testnet> \
  --deposit 5
Deposit amounts are in NEAR tokens (not yoctoNEAR).

Specify Gas

Manually specify gas for complex operations:
near call <your-account.testnet> complex_operation '{}' \
  --useAccount <your-account.testnet> \
  --gas 300000000000000

Deployment Costs

Storage Costs

Contracts pay for storage by locking NEAR tokens:
  • ~1Ⓝ per 100KB of data
  • Storage costs are locked, not spent
  • Deleting data releases locked tokens
A simple contract (~50KB):
  • Cost: ~0.5Ⓝ
  • Example: Hello World, basic counter

Check Contract Size

# Check WASM file size
ls -lh ./target/near/contract.wasm

Best Practices

Always deploy to testnet first:
  1. Test all functionality
  2. Have users try it out
  3. Fix any issues
  4. Then deploy to mainnet
Choose clear account names:
  • mytoken.near
  • marketplace.near
  • abc123.near
After deployment, document:
  • Contract methods and parameters
  • How to interact with it
  • Security considerations
  • Admin functions
Consider upgrade strategy:
  • How will you update the contract?
  • Do you need state migration?
  • Who can upgrade it?
See Contract Upgrades for details.
After deployment:
  • Monitor contract calls
  • Track errors and failures
  • Watch for unusual activity
  • Use indexers for analytics

Verifying Deployment

Check Contract Code

Verify the contract is deployed:
near view-state <your-account.testnet> --finality final

View Contract on Explorer

Visit the NEAR Explorer: You can see:
  • Contract code hash
  • Contract size
  • Transaction history
  • Contract calls

Common Issues

The account name is taken. Choose a different name:
near create-account different-name.testnet --useFaucet
Your account needs more NEAR tokens:
  • Testnet: Use the faucet again or request from the community
  • Mainnet: Add more NEAR to your account
This happens when:
  • Contract state structure changed
  • Need to migrate state
See Contract Upgrades for solutions.
If your contract requires initialization:
near call <contract> init '{"param": "value"}' --useAccount <account>
Or use --initFunction during deployment.

Environment Variables

Useful environment variables for deployment:
# Set network (testnet or mainnet)
export NEAR_ENV=testnet

# Set custom RPC endpoint
export NEAR_CLI_TESTNET_RPC_SERVER_URL=https://rpc.testnet.near.org

# Set custom node URL
export NODE_ENV=production

Deployment Checklist

Before deploying to mainnet:

Next Steps

Upgrade Contract

Learn how to update deployed contracts

Monitor Activity

Track contract usage and analytics

Build Frontend

Create a web interface for your contract

NEAR CLI

Learn more NEAR CLI commands

Build docs developers (and LLMs) love