LLM Notice: This documentation site supports content negotiation for AI agents. Request any page with Accept: text/markdown or Accept: text/plain header to receive Markdown instead of HTML. Alternatively, append ?format=md to any URL. All markdown files are available at /md/ prefix paths. For all content in one file, visit /llms-full.txt
Skip to main content

Generating Cadence Boilerplate

The flow generate command provides a convenient way to create boilerplate template files for common Cadence code components. TO streamline the development process, this command automatically generates properly structured files with the correct syntax and organization.

Overview


_10
flow generate [command]

Aliases: generate, g

The generate command supports four main subcommands to create different types of Cadence files:

  • contract - Generate Cadence smart contract templates.
  • script - Generate Cadence script templates.
  • test - Generate Cadence test templates.
  • transaction - Generate Cadence transaction templates.

Generate contract

Creates a new Cadence smart contract with a basic template structure.

Usage


_10
flow generate contract <name> [flags]

Example


_10
flow generate contract HelloWorld

This command creates a file cadence/contracts/HelloWorld.cdc with the following content:


_10
access(all) contract HelloWorld {
_10
init() {}
_10
}

info

When you generate a contract, a test file that corresponds to that contracts is also created automatically (unless --skip-tests is used). For example, when you generate HelloWorld, the contract will also create cadence/tests/HelloWorld.test.cdc.

Flags

  • --dir string - Directory to generate files in (defaults to cadence/contracts/).
  • --skip-tests - Skip test file generation.
  • -h, --help - Help for contract command.

Generate transaction

Creates a new Cadence transaction with a basic template structure.

Use


_10
flow generate transaction <name> [flags]

Example


_10
flow generate transaction TransferTokens

This command creates a file cadence/transactions/TransferTokens.cdc with the following content:


_10
transaction() {
_10
prepare() {}
_10
_10
execute {}
_10
}

Flags

  • --dir string - Directory to generate files in (defaults to cadence/transactions/).
  • --skip-tests - Skip test file generation.
  • -h, --help - Help for transaction command.

Generate script

Creates a new Cadence script with a basic template structure.

Use


_10
flow generate script <name> [flags]

Example


_10
flow generate script GetBalance

This command creates a file cadence/scripts/GetBalance.cdc with the following content:


_10
access(all) fun main() {}

Flags

  • --dir string - Directory to generate files in (defaults to cadence/scripts/).
  • --skip-tests - Skip test file generation.
  • -h, --help - Help for script command.

Generate test

Creates a new Cadence test file with a basic template structure.

Use


_10
flow generate test <name> [flags]

Example


_10
flow generate test MyToken

This command creates a file cadence/tests/MyToken.test.cdc with a basic test structure.

After you generate a test, you can run it using flow test. For more information about how to write and run Cadence tests, see the Cadence Tests documentation.

Flags

  • --dir string - Directory to generate files in (defaults to cadence/tests/).
  • --skip-tests - Skip test file generation.
  • -h, --help - Help for test command.

Custom directory use

All generate commands support the --dir flag to specify a custom directory for the generated files. This is useful when your project requires a different organizational structure than the default.

Examples


_11
# Generate contract in a custom directory
_11
flow generate contract MyToken --dir=src/contracts
_11
_11
# Generate transaction in a custom directory
_11
flow generate transaction Transfer --dir=src/transactions
_11
_11
# Generate script in a custom directory
_11
flow generate script GetData --dir=src/scripts
_11
_11
# Generate test in a custom directory
_11
flow generate test MyToken --dir=src/tests

Project structure

When you use the default directories, the generate command creates the following structure:


_10
cadence/
_10
├── contracts/
_10
│ └── MyToken.cdc
_10
├── scripts/
_10
│ └── GetBalance.cdc
_10
├── transactions/
_10
│ └── TransferTokens.cdc
_10
└── tests/
_10
└── MyToken.test.cdc

The generate command is an essential tool to accelerate Flow development with standardized, well-structured boilerplate code for all common Cadence components.