Skip to main content
Version: Next

Flow CLI Quickstart

The Flow CLI (Command Line Interface) is a set of tools that developers can use to interact with the Flow blockchain by managing accounts, sending transactions, deploying smart contracts, running the emulator, and more. This quickstart will get you familiar with its main concepts and functionality.

Installation

The first thing you'll need to do is install the Flow CLI. If you have homebrew installed you can run:


_10
brew install flow-cli

For other ways of installing, please refer to the installation guide.

Configuration

Let's first create a directory to setup a project we can work from.


_10
mkdir cli-quickstart

And then let's navigate to it:


_10
cd cli-quickstart

Let's first create a flow.json file for our project (we won't use it till a later step, but this is a good spot to learn it). The flow.json file is a configuration file used by the Flow CLI when interacting with the Flow blockchain. It helps manage various project-specific settings, such as network configurations, account details, contract names and source files, and deployment targets. To create one, run:


_10
flow init

If you look at flow.json now you'll see it's listed access nodes for networks and accounts. The default emulator-account create will come in handy when we want to run the emulator.

Running Scripts

On Flow, scripts are used to read from the chain. There is no state modification. In our case, we are going to read a greeting from a contract deployed to testnet called HelloWorld. (You can view the contract here)

Let's create a script file:


_10
touch script.cdc

Then in the script file, let's put the following code:


_10
import HelloWorld from 0x9dca641e9a4b691b
_10
_10
pub fun main(): String {
_10
return HelloWorld.greeting
_10
}

In the above example, 0x9dca641e9a4b691b is the address of the account on testnet that the HelloWorld contract has been deployed to. (Note: if you'll like to learn more about writing scripts, please read here).

To run the script, we'll run this from the CLI:


_10
flow scripts execute script.cdc --network testnet

You should see the result of the greeting.

Running the Contract Locally on Emulator

The Flow Emulator is a local, lightweight, and standalone version of the Flow blockchain. It's designed to provide developers with a local environment for testing and development purposes without having to interact with the mainnet or testnet. This makes it easier and faster to develop, test, and debug smart contracts and dApps.

In order to use it, let's catch our project configuration up with where we are at so far.

Let's create a local version of this contract for the emulator. Run:


_10
touch HelloWorld.cdc

Then let's put the contract into our project. Copy this to HelloWorld.cdc:


_12
pub contract HelloWorld {
_12
_12
pub var greeting: String
_12
_12
pub fun changeGreeting(newGreeting: String) {
_12
self.greeting = newGreeting
_12
}
_12
_12
init() {
_12
self.greeting = "Hello, World!"
_12
}
_12
}

The next thing we'll do is add a contracts section to our flow.json configuration that will describe our setup. We'll state where the file for the contract lives with source and then define aliases for the addresses of the deployed contracts.


_14
// flow.json
_14
_14
{
_14
...
_14
"contracts": {
_14
"HelloWorld": {
_14
"source": "HelloWorld.cdc",
_14
"aliases": {
_14
"testnet": "0x9dca641e9a4b691b"
_14
}
_14
}
_14
}
_14
...
_14
}

We're also going to change the imports of our script so that there are no hardcoded addresses. We'll let CLI figure out how to interact with what network based on our configuration.


_10
// script.cdc
_10
_10
import "HelloWorld"
_10
_10
pub fun main(): String {
_10
return HelloWorld.greeting
_10
}

Next, we'll add a deployments section to flow.json and define what account we'd like what contract deployed to and on what network. In this case, let's deploy the HelloWorld contract to the emulator network and on emulator-account provided.


_10
// flow.json
_10
_10
{
_10
...
_10
"deployments": {
_10
"emulator": {
_10
"emulator-account": ["HelloWorld"]
_10
}
_10
...
_10
}

Next let's run the emulator in a new terminal with:


_10
flow emulator start

And then deploy the contract with:


_10
flow project deploy

Now if we run the following script we should see the result of the script against our emulator deployed contract.


_10
flow scripts execute script.cdc --network emulator

Creating an Account and Running a Transaction

To change state on the Flow Blockchain, you need to run a transaction. Let's create a simple transaction file we can use to modify the greeting on the HelloWorld contract.

First, create file called transaction.cdc from the root of your project:


_10
touch transaction.cdc

Then copy the following code:


_12
import "HelloWorld"
_12
_12
transaction(greeting: String) {
_12
_12
prepare(acct: AuthAccount) {
_12
log(acct.address)
_12
}
_12
_12
execute {
_12
HelloWorld.changeGreeting(newGreeting: greeting)
_12
}
_12
}

This will log the account signing the transaction and call the changeGreeting method of the HelloWorld contract, and pass in the new greeting we want to set. (If you want to learn more about writing transactions, please read here).

But in order to run a transaction, an account needs to pay for it. And while we could run a transaction on emulator using the emulator-account, let's learn one more command for creating accounts.

The easiest way to create an account using CLI is by running (remember, your emulator should still be running at this point in another terminal):


_10
flow accounts create

Once that runs, select Emulator as the network and give your account the name emulator-tester. You'll now see this account in your flow.json.

To run a transaction with this new account, you can run the following:


_10
flow transactions send ./transaction.cdc "Hello, me" --signer emulator-tester --network emulator

You've just modified the state of the Flow Blockchain!

Next Steps

A great way to dive in deeper is to check out the scaffolds generated by the Flow CLI. They can serve as a great starting point for any project you're trying to create. See how to create a scaffold here.