Skip to main content
Version: Current

Scripts

A script is executable Cadence code that queries the Flow network but does not modify it. Unlike Flow transactions, they don’t need signing and they can return a value. You can think of executing scripts as a read-only operation.

Scrips are executed on Access Nodes, Archive Nodes, or Execution Nodes.

Scripts are defined by following the Cadence code and we can only execute one at a time.


_10
pub fun main() {}

Scripts can return a typed value:


_10
pub fun main(): Int {
_10
return 1 + 2
_10
}

Scripts can call contract functions and query the state of a contract. To call a function on another contract, import it from its address and invoke the function:


_10
import World from 0x01
_10
_10
pub fun main(): String {
_10
return World.hello()
_10
}

Executing Scripts

You can execute a script by using the Flow CLI:


_10
flow scripts execute ./helloWorld.cdc

A user can define their own scripts or can use already defined scripts by the contract authors that can be found by using the FLIX service.

Scripts can be run against previous blocks, allowing you to query historic data from the Flow network. This is particularly useful for retrieving historical states of contracts or tracking changes over time.

Scripts are executed by being submitted to the Access Node APIs. Currently, there’s support for two APIs:

gRPC Script API

REST Script API

There are multiple SDKs implementing the above APIs for different languages:

Javascript SDK

Go SDK

Find a list of all SDKs here