Read Only Calls
Read-only function calls allow you to query data from smart contracts without creating a transaction. These calls are free, instant, and don't require wallet interaction.
Basic read-only call
Call a read-only function to get contract data without any transaction fees:
import { fetchCallReadOnlyFunction, cvToValue } from '@stacks/transactions';
const contractAddress = 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM';
const contractName = 'my-contract';
const functionName = 'get-balance';
const response = await fetchCallReadOnlyFunction({
contractAddress,
contractName,
functionName,
functionArgs: [],
senderAddress: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM',
});
const balance = cvToValue(response);
console.log('Balance:', balance);Passing function arguments
Most read-only functions require arguments. Use Clarity value builders to construct the appropriate types:
Handling response types
Read-only functions can return response types (ok/err). Check the response type to handle both success and error cases:
Using custom network
Specify a custom network URL for testnet or custom node connections:
Last updated
Was this helpful?
