Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

SDK Reference

The Clarinet JS SDK provides a comprehensive suite of helpers for testing and interacting with Clarity smart contracts. From simnet initialization to contract deployment, the SDK streamlines your entire testing workflow.

  • Initialize a simulated network: initSimnet

  • Manage contract state: getDataVar, getMapEntry

  • Call contract functions: callReadOnlyFn, callPublicFn

  • Transfer STX: transferSTX

  • Deploy contracts: deployContract

  • Mine blocks: mineBlock, mineEmptyBlock

  • Custom assertions: toBeOk, toBeErr

Installation

npm install @stacks/clarinet-sdk

Initialize simulated network

initSimnet

initSimnet initializes a simulated network for testing your smart contracts.

Usage:

initSimnet(manifestPath?: string): Promise<Simnet>
Parameter
Type
Description

manifestPath

string

Optional path to Clarinet.toml manifest file

Simnet properties

blockHeight

Returns the current block height of simnet.

deployer

Returns the default deployer address as defined in the project file.

You can also update the deployer:

currentEpoch

Returns the current epoch of simnet (e.g., 2.5 for Stacks 2.5).

Account management

getAccounts

getAccounts retrieves all configured Stacks addresses including wallets, deployers, and faucets.

Usage:

Asset balances

getAssetsMap

getAssetsMap retrieves asset balances for all addresses, including STX, fungible, and non-fungible tokens.

Usage:

Read contract state

getDataVar

getDataVar retrieves the value of a data variable from a contract.

Usage:

Parameter
Type
Description

contract

string

Contract identifier

dataVar

string

Name of the data variable

getMapEntry

getMapEntry retrieves a value from a contract map by its key. Like the node's map-entry endpoint, it returns (some value) for an existing entry and none otherwise, including when the map or contract does not exist.

Usage:

Parameter
Type
Description

contract

string

Contract identifier

mapName

string

Name of the map

mapKey

ClarityValue

Key to look up

Call contract functions

callReadOnlyFn

callReadOnlyFn calls read-only functions without mining a block.

Usage:

Parameter
Type
Description

contract

string

Contract identifier

method

string

Function name

args

ClarityValue[]

Function arguments

sender

string

Sender address

callPublicFn

callPublicFn calls public functions and mines a block.

Usage:

A returned (err ...) response does not throw. callPublicFn, callPrivateFn, deployContract and transferSTX throw when simnet rejects the transaction. They also throw when simnet includes the transaction but it fails with a runtime error or a post-condition abort. An included failure still consumes the sender's nonce and mines its block. To tell included failures from rejections, use mineBlock.

callPrivateFn

callPrivateFn calls private functions (testing only) and mines a block.

Usage:

Transfer STX

transferSTX transfers STX between addresses and mines a block. Like callPublicFn, it advances one block before executing, even when simnet rejects the transfer.

Self-transfers, zero-amount transfers, and transfers that exceed the sender's unlocked balance throw without consuming a nonce. Inside mineBlock, these transfers return a rejected outcome instead.

Usage:

Parameter
Type
Description

amount

number | bigint

Amount in microSTX

recipient

string

Recipient address

sender

string

Sender address

Deploy contracts

deployContract deploys a new contract to simnet and mines a block. A successful deployment returns (ok true), whatever the contract's final top-level expression evaluates to. To check what the contract did when it initialized, use read-only calls, getDataVar and events. To evaluate a snippet and get its value, use execute.

An unsupported clarityVersion throws instead of falling back to the default version. Simnet rejects a duplicate contract name without consuming a nonce.

Usage:

Parameter
Type
Description

name

string

Contract name

content

string

Clarity source code

options

object | null

Deployment options

sender

string

Deployer address

Block mining

mineBlock

mineBlock mines a block with multiple transactions. It advances exactly one block, and all entries execute at that height. It first mines any signed transactions queued with broadcastTransaction, then the entries in txs, in input order.

Usage:

Each result has one of three statuses. A failed entry does not stop the batch. Later entries still execute.

Status
Meaning

success

execution holds the parsed result, events and costs.

included_failure

Simnet included the transaction, but it failed. error describes the failure and kind is abort_by_response, abort_by_post_condition, runtime_error or analysis_error. execution is always present. The transaction consumes the nonce, and its payload writes roll back.

rejected

Simnet did not include the transaction. error gives the reason. Simnet commits no writes and no nonce changes.

Results for signed transactions also have a transaction property with the txid, fee and nonce.

mineEmptyBlock

mineEmptyBlock mines an empty block and increases block height.

Usage:

mineEmptyBlocks

mineEmptyBlocks mines multiple empty blocks.

Usage:

Signed transactions

broadcastTransaction

broadcastTransaction submits a signed, serialized Stacks transaction to the simnet's local mempool. It runs the node's mempool admission checks but does not execute the transaction, charge fees, consume nonces, or advance the chain. Call mineBlock() to mine queued transactions.

Usage:

On acceptance it returns { txid }. On rejection it returns { error, reason, reason_data?, txid? }.

Admission checks signatures, network and epoch rules, nonces, unlocked funds, transfer validity, duplicate deployments, and public-function access and argument types. Admission rejects transactions that call read-only, private or missing functions. The minimum fee is 1 micro-STX per serialized byte. Set fee and nonce explicitly so Stacks.js does not request them from a remote node.

Admission does not guarantee inclusion, because mineBlock checks the current state again. A transaction with a future nonce stays in the queue until mineBlock mines the transactions with the earlier nonces. To execute a signed transaction directly, without mempool admission, pass it to mineBlock as { broadcastTx: rawHex }.

Utility methods

execute

execute evaluates arbitrary Clarity code without deploying a contract. The result property holds the parsed Clarity value of the expression. If evaluation fails, execute throws, so wrap it in try/catch when you expect a failure.

Usage:

getContractsInterfaces

getContractsInterfaces returns contract interfaces with function signatures and storage.

Usage:

getContractSource

getContractSource retrieves the source code of a deployed contract.

Usage:

getContractAST

getContractAST returns the Abstract Syntax Tree of a contract.

Usage:

Custom matchers

The SDK provides Vitest matchers for Clarity value assertions.

Response matchers

toBeOk

Asserts that a response is (ok <value>).

toBeErr

Asserts that a response is (err <value>).

toBeSome

Asserts that a response is (some <value>).

toBeNone

Asserts that a response is (none).

Value matchers

toBeBool

Asserts a boolean value.

toBeInt

Asserts a signed integer value.

toBeUint

Asserts an unsigned integer value.

toBeAscii

Asserts a string-ascii value.

toBeUtf8

Asserts a string-utf8 value.

toBePrincipal

Asserts a principal value.

toBeBuff

Asserts a buffer value.

toBeList

Asserts a list of Clarity values.

toBeTuple

Asserts a tuple value.

Type checking

toHaveClarityType

Checks that a value has the expected Clarity type.

Event matchers

toContainEqual

Asserts that an events array contains a specific event. This is useful for checking transaction events.

Last updated

Was this helpful?