> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tenderly.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Web3 Action via CLI

> Get started with building Web3 Actions using the Tenderly CLI step by step.

In this guide, you’ll learn how to deploy Web3 Actions via the Tenderly CLI that watches for blocks as they're mined and simply outputs the block number.

### Prerequisites

You need to have npm and the Tenderly CLI installed and properly configured on your local machine. Check out the [Web3 Actions CLI Cheatsheet](/monitoring/web3-actions/references/cli-cheatsheet) for installation instructions.

### Initializing a Web3 Actions Project

Navigate to the folder on your system where you want to place your Web3 Action code and the following command to initialize a new Web3 Actions project:

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tenderly actions init
```

By default, the project will be configured to use TypeScript. If you prefer plain JavaScript, add the language flag and specify javascript:

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tenderly actions init --language javascript
```

When prompted, select an existing Tenderly project from your account or create a new one.

<Frame caption="Creating a new Tenderly project">
  <img src="https://mintcdn.com/tenderly/XsEZlaGXYskrtN68/images/Screenshot_2022-08-04_at_10.50.42%20(1).png?fit=max&auto=format&n=XsEZlaGXYskrtN68&q=85&s=5191970c6b2a63a0ee869429dbeaf725" alt="Creating a new Tenderly project" width="434" height="65" data-path="images/Screenshot_2022-08-04_at_10.50.42 (1).png" />
</Frame>

Next, input the name of the root directory where your code will be stored. If you want to leave the default actions, press Enter, or input a custom directory name.

<Frame caption="Adding a directory name">
  <img src="https://mintcdn.com/tenderly/XsEZlaGXYskrtN68/images/Screenshot_2022-08-04_at_10.51.55.png?fit=max&auto=format&n=XsEZlaGXYskrtN68&q=85&s=bec5b2d4ea93181226d4bd4fb4b4a86a" alt="Adding a directory name" width="553" height="55" data-path="images/Screenshot_2022-08-04_at_10.51.55.png" />
</Frame>

The folder structure of your project should look like this:

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
/actions
|--- example.ts
|--- package.json
|--- package-lock.json
|--- tsconfig.json
|--- /node_modules
      |--- @tenderly
      |--- /typescript
tenderly.yaml
```

### Building and deploying the Web3 Action

These are the steps we need to take to build and deploy a Web3 Action:

* Create the action function
* Configure the block trigger to run the function
* Deploy the code to Tenderly using the CLI

#### Creating an action function

Copy the following code into the **example.ts** file:

```tsx title="example.tsx" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}

export const blockHelloWorldFn: ActionFn = async (context: Context, event: Event) => {
  let blockEvent = event as BlockEvent;
  console.log('Block number is: ', blockEvent.blockNumber);
};
```

This defines and exports an action function. In the next step, we'll configure it to run after 10 blocks are mined.

**Notes:**

Notice that the function is asynchronous and accepts two arguments - a `Context` object and an `Event` object.

* `Context` object enables access to Storage and Secrets. You can read more about it here.
* The and the `event` argument is an object containing information about the external (on-chain) event that triggered the action. Because we want to run this action on a block-related event, the `event` argument will be of type `BlockEvent`. The `BlockEvent` object contains information about the block that was mined, such as the block number, the block hash, and the block timestamp.

#### Configuring the trigger in **tenderly.yaml** for a new action

**Step 1**: Replace the content of the `tenderly.yaml` file with the following:

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
account_id: ''
project_slug: ''
actions:
  username/project-slug:
    runtime: v1
    sources: actions
    specs:
      block-number-printer:
        description: Example Web3 Action that logs block number once it is mined.
        function: example:blockHelloWorldFn
        trigger:
          type: block
          block:
            network:
              - 1
            blocks: 10
```

**Step 2**: Replace the `username` and `project-slug` with your username and the slug of your project.

Check out this guide to learn how to [find the organization name, username, and project slug](/platform/account/projects/slug).

**Notes:**

The **tenderly.yaml** file contains specifications of triggers for each action in the project and links the trigger to an action function. Here, we're creating an action called `block-number-printer`.

* The `function` property is referencing the `blockHelloWorldFn` function from the **example.ts** file.
* This action will be triggered on each 10th block mined on network 1, as per the `trigger` property.

Learn more about [External Events and Trigger types](/monitoring/web3-actions/references/functions-events-triggers#external-events-and-trigger-types).

#### Building and deploying actions

Go back to terminal and run the following command to deploy all Web3 Actions you defined. After successful deployment, the actions appear in the Tenderly UI.

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tenderly actions deploy
```

<Note>
  **Zero downtime upgrade of Web3 Actions**\ To **upgrade** a Web3 Action, just edit the source code
  and/or **tenderly.yaml** and run `tenderly actions deploy.`
</Note>

### Before you go

Keep in mind:

* You must run the `deploy` command from the folder containing **tenderly.yaml** file.
* If you initialize actions for multiple Tenderly projects in the same directory, upon running `deploy` you will be asked to select a project. Only actions belonging to the selected project will be deployed.
* All actions belonging to one project are deployed simultaneously.
* Any file referenced from the JavaScript/TypeScript must be in the *actions root*. Do not reference files outside of this folder.

### Resources

* Learn more about [Web3 Actions Functions, Events, and Triggers](/monitoring/web3-actions/references/functions-events-triggers).
* Learn more about [Context, Storage, and Secrets](/monitoring/web3-actions/references/context).
* Learn more about [Project Structure and **tenderly.yaml** configuration](/monitoring/web3-actions/references/project-structure).
