Skip to main content
One of near-kit’s superpowers is that it abstracts the “Signer.” Whether a transaction is signed by a generic private key, a secure server-side keystore, or a user’s browser wallet, the code to build and send that transaction is identical. This allows you to write “Universal Business Logic”—functions that define what to do, without caring where they are running.

1. The Business Logic

First, write your logic as a standalone function. It should accept a Near instance and a signerId.
// src/features/buy-item.ts
import { Near } from "near-kit"

/**
 * Buys an item from the market.
 * This function works on the Server, Client, and in Tests!
 */
export async function buyItem(near: Near, signerId: string, itemId: string) {
  console.log(`Attempting to buy ${itemId} as ${signerId}...`)

  return await near
    .transaction(signerId)
    .functionCall(
      "market.near",
      "buy",
      { item_id: itemId },
      { attachedDeposit: "1 NEAR", gas: "50 Tgas" }
    )
    .send()
}

2. Injecting the Near Instance

Now, let’s see how to initialize the Near object for different environments.
For simple scripts, passing a raw private key is the easiest method.
import { Near } from "near-kit"
import { buyItem } from "./features/buy-item"

const near = new Near({
  network: "testnet",
  privateKey: process.env.ADMIN_KEY, // "ed25519:..."
  defaultSignerId: "admin.testnet",
})

await buyItem(near, "admin.testnet", "sword-1")

Summary

EnvironmentConfig OptionWho Signs?
ScriptprivateKey: "..."The provided key string.
BackendkeyStore: storeThe key matching signerId found in the store.
Frontendwallet: adapterThe user (via wallet popup).
Sandboxnetwork: sandboxThe root account key (in-memory).
Your business logic (buyItem) never needs to know which one is happening.