# Set up Alchemy with Viem

> Learn how to send a blockchain request via a script using Alchemy.

> For the complete documentation index, see [llms.txt](/docs/llms.txt).

> **Don't have an API key?** [Start here](/docs/create-an-api-key) before setting up your project.

### What is Viem?

Viem is a set of modules for JavaScript or TypeScript that helps you interface with Ethereum or Ethereum Virtual Machine (EVM) chains.

You'll use it in this guide, configuring it with your Alchemy API URL, so you can read and write to the blockchain of your choice.

If you're new to Viem, check out [our 60 minute video diving into it](https://youtu.be/P9oUqVsHBkA) and the [Viem docs](https://viem.sh/docs/introduction).

### 🔧 Prerequisites

1. **Node.js**
   * Required to run JavaScript/TypeScript outside the browser.
   * Install **Node.js 18 or later** (viem requires modern Node features).
   * Download from: [https://nodejs.org/](https://nodejs.org/)

2. **npm** (comes with Node.js)
   * Default package manager bundled with Node.
   * Automatically installed when you install Node.js.

<Steps>
  <Step title="Project setup and installation">
    From your [command line](https://www.computerhope.com/jargon/c/commandi.htm), create a new project directory and install Viem:

    <CodeGroup>
      ```shell npm
      # Create and enter project directory
      mkdir project && cd project
      # Initialize a new Node.js project
      npm init -y
      # Install viem
      npm install viem
      ```

      ```shell pnpm
      # Create and enter project directory
      mkdir project && cd project
      # Initialize a new Node.js project
      pnpm init
      # Install viem
      pnpm add viem
      ```

      ```shell yarn
      # Create and enter project directory
      mkdir project && cd project
      # Initialize a new Node.js project
      yarn init -y
      # Install viem
      yarn add viem
      ```
    </CodeGroup>
  </Step>

  <Step title="Create script">
    Create a file named `index.js` and add the following contents, replacing `demo` with your Alchemy API key:

    <CodeGroup>
      ```javascript index.js
      import { createPublicClient, http } from "viem";
      import { mainnet } from "viem/chains";

      const client = createPublicClient({
        chain: mainnet,
        transport: http("https://eth-mainnet.g.alchemy.com/v2/demo"),
      });

      async function getLatestBlockNumber() {
        const blockNumber = await client.getBlockNumber();
        console.log("Latest block number:", blockNumber);
      }

      getLatestBlockNumber();
      ```
    </CodeGroup>

    <Info>
      You can find the documentation of viem's [getBlockNumber](https://viem.sh/docs/actions/public/getBlockNumber) which is configured to call out to [eth\_blockNumber](/docs/node/ethereum/ethereum-api-endpoints/eth-block-number) on the Alchemy API.
    </Info>
  </Step>

  <Step title="Run it">
    <CodeGroup>
      ```shell shell
      node index.js
      ```
    </CodeGroup>

    This should return the latest block number in your console:

    <CodeGroup>
      ```shell shell
      The latest block number is 11043912
      ```
    </CodeGroup>
  </Step>
</Steps>

You've written your first web3 script using Alchemy and sent your first request to your Alchemy API endpoint.

You can visit your [request logs](https://dashboard.alchemy.com/logs) in the Alchemy Dashboard to see the full details of the request.

## Next Steps

So, what's next for you?

* Want to build an application using the latest and greatest web3 login experience? Check out [getting started with our wallets](docs/wallets/react/quickstart).
* Need the industry best APIs to access blockchain data (tokens, transfers, nfts, prices and more)? Jump on over to our [data APIs](docs/reference/data-overview).
* Want to find all the methods available to you on our node API? Check out the [documentation here](docs/reference/node-api-overview).
* Need to learn more about crypto? We've got you covered at [Alchemy University](https://university.alchemy.com/).