near-kit
A clean, ergonomic Rust client for NEAR Protocol.
near-kit provides a fluent API for interacting with NEAR Protocol,
with a focus on developer experience and type safety. It's a ground-up
implementation with hand-rolled types based on actual NEAR RPC responses.
Quick Start
Add near-kit to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["rt-multi-thread", "macros"] }
Read-Only Operations
No credentials needed for querying blockchain state:
use *;
async
Transactions (Writes)
For state-changing operations, configure a signer:
use *;
async
Design Principles
- Single entry point — Everything flows through the [
Near] client - Configure once — Network and signer are set at client creation
- Type-safe but ergonomic — Accept both typed values and string parsing
- Explicit units — No ambiguous amounts; must specify
NEAR,yocto,Tgas, etc. - Progressive disclosure — Simple things are simple; advanced options available when needed
Core Types
| Type | Description |
|---|---|
[Near] |
Main client — the single entry point for all operations |
[AccountId] |
Validated NEAR account identifier |
[NearToken] |
Token amount with yoctoNEAR precision |
[Gas] |
Gas units for transactions |
[PublicKey] / [SecretKey] |
Ed25519 cryptographic keys |
[CryptoHash] |
32-byte SHA-256 hash (blocks, transactions) |
Working with Amounts
Typed Constructors (Recommended)
For compile-time safety, use the typed constructors:
use ;
// NEAR amounts
let five_near = near;
let half_near = millinear;
let one_yocto = yocto;
// Gas amounts
let gas = tgas; // 30 teragas
let more_gas = ggas; // 5 gigagas
String Parsing (Runtime Input)
For CLI arguments, config files, or user input:
use ;
// Parse NEAR amounts
let amount: NearToken = "5 NEAR".parse.unwrap;
let small: NearToken = "100 milliNEAR".parse.unwrap;
let tiny: NearToken = "1000 yocto".parse.unwrap;
// Parse gas
let gas: Gas = "30 Tgas".parse.unwrap;
Many builder methods also accept strings directly:
# use *;
# async
Query Builders
Read operations return query builders that can be customized before awaiting:
use *;
# async
Transaction Builders
Write operations return transaction builders for customization:
use *;
# async
Multi-Action Transactions
Chain multiple actions into a single atomic transaction:
use *;
# async
Token Helpers
Built-in support for fungible (NEP-141) and non-fungible (NEP-171) tokens:
use *;
# async
Typed Contract Interfaces
Use the #[near_kit::contract] macro for compile-time type safety:
use *;
use Serialize;
async
Signers
Several signer implementations are available:
| Signer | Use Case |
|---|---|
[InMemorySigner] |
Simple scripts with a private key |
[FileSigner] |
Load from ~/.near-credentials (near-cli compatible) |
[EnvSigner] |
CI/CD environments via NEAR_ACCOUNT_ID / NEAR_PRIVATE_KEY |
[RotatingSigner] |
High-throughput with multiple keys (avoids nonce collisions) |
[KeyringSigner] |
System keyring (macOS Keychain, etc.) — requires keyring feature |
use *;
#
Sandbox Testing
Enable the sandbox feature for local testing with near-sandbox:
[]
= { = "0.1", = ["sandbox"] }
= "0.3"
use *;
use Sandbox;
async
Feature Flags
| Feature | Description |
|---|---|
sandbox |
Integration with near-sandbox for local testing |
keyring |
System keyring signer (macOS Keychain, Windows Credential Manager, etc.) |
Error Handling
All operations return Result<T, near_kit::Error>. The [Error] type provides
detailed information about failures:
use *;
# async