Skip to main content

Use Clarity Values

Some endpoints, like the read-only function contract call, require input to a serialized Clarity value. Other endpoints return serialized values that need to be deserialized.

The example shown below illustrates Clarity value usage in combination with the API.

The @stacks/transactions library supports typed contract calls and makes response value utilization much simpler.

import {
} from '@stacks/blockchain-api-client';
import { uintCV, UIntCV, cvToHex, hexToCV, ClarityType } from '@stacks/transactions';

(async () => {
const apiConfig: Configuration = new Configuration({
fetchApi: fetch,
// for mainnet, replace `testnet` with `mainnet`
basePath: '', // defaults to http://localhost:3999

const contractsApi: SmartContractsApiInterface = new SmartContractsApi(apiConfig);

const principal: string = 'ST000000000000000000002AMW42H';

// use most recent from: https://api.<mainnet/testnet>
const rewardCycle: UIntCV = uintCV(22);

// call a read-only function
const fnCall: ReadOnlyFunctionSuccessResponse = await contractsApi.callReadOnlyFunction({
contractAddress: principal,
contractName: 'pox',
functionName: 'is-pox-active',
readOnlyFunctionArgs: {
sender: principal,
arguments: [cvToHex(rewardCycle)],

status: fnCall.okay,
result: fnCall.result,
representation: hexToCV(fnCall.result).type === ClarityType.BoolTrue,