view
Endpoint#
POST /v1/view
Request Body#
{
"function": "0x1::coin::balance",
"type_arguments": ["0x1::aptos_coin::AptosCoin"],
"arguments": ["0x1"]
}
Response#
[
"0x... or value depending on function"
]
Code Examples#
curl -s -X POST "https://api-aptos-mainnet.n.dwellir.com/YOUR_API_KEY/v1/view" \
-H "Content-Type: application/json" \
-d '{"function":"0x1::coin::balance","type_arguments":["0x1::aptos_coin::AptosCoin"],"arguments":["0x1"]}'
Python example:
result = client.view_function(
"0x1::coin::balance",
["0x1::aptos_coin::AptosCoin"],
["0x1"]
)
print(f"Balance: {result[0]}")
TypeScript example:
const result = await aptos.view({
payload: {
function: "0x1::coin::balance",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: ["0x1"]
}
});
Use Cases#
View functions enable efficient read-only blockchain queries:
-
Balance Queries: Check account balances without transaction overhead or gas costs.
-
State Inspection: Read contract state variables and computed values efficiently.
-
Validation Checks: Verify conditions before transaction submission to prevent failed transactions.
-
UI Data Fetching: Power application UIs with real-time blockchain data without polling full resources.
-
Price Feeds: Query DEX prices, oracle data, or computed values for display or trading logic.
-
Permission Checks: Verify user permissions or capabilities before allowing operations.
Best Practices#
Function Visibility: Only functions marked public fun or public entry fun with #[view] attribute can be called via this endpoint.
Gas-Free: View functions don't consume gas or require signatures, making them ideal for frequent queries.
Type Arguments: Provide full type paths including generics for functions requiring type parameters.
Argument Encoding: Arguments are passed as JSON values and automatically BCS-encoded by the API.
Caching: View function results reflect current state. Cache appropriately based on how frequently the underlying state changes.
Ledger Version: Optionally specify a ledger_version query parameter to query historical state at specific points in time.
Performance Considerations#
View functions execute directly in the Move VM without consensus or state commitment overhead. Response times are typically 20-100ms depending on function complexity.
Simple reads complete in 20-50ms, while complex computations may take 100-200ms. View functions are significantly faster than full transaction simulation for read operations.
For frequently accessed data, consider implementing client-side caching with TTLs based on block time (4-5 seconds on mainnet).