<!-- Canonical: https://docs.linea.build/network/how-to/deploy-smart-contract/hardhat -->

> For the complete Linea documentation index, see [llms.txt](/llms.txt).
> Agents can fetch this page as Markdown at [https://docs.linea.build/network/how-to/deploy-smart-contract/hardhat.md](https://docs.linea.build/network/how-to/deploy-smart-contract/hardhat.md).

# Hardhat

In this quickstart, we'll create a basic [Hardhat](https://hardhat.org/) project. Here's a video walkthrough:

## Prerequisites

Before you begin, ensure you set up your environment using [Hardhat's recommended instructions](https://hardhat.org/tutorial/setting-up-the-environment#2.-setting-up-the-environment).

## Create a Hardhat project

To create an empty Hardhat project:

1.  Create your project directory:

```bash
mkdir linea-tutorial; cd linea-tutorial
```

2.  Initialize your Node.js project:

```bash
npm init
```

3.  Install Hardhat:

```bash
npm install --save-dev hardhat
```

4.  Initialize the Hardhat project:

```bash
npx hardhat init
```

    In the menu that appears, select **Create a JavaScript project** and press **Enter**. Accept all the default values in the questionnaire.

You should now have a sample contract that deploys and tests a `Lock` contract that locks funds for a set time.

note

The default script in `ignition/modules/Lock.js` locks 1 GWEI in the contract upon deployment, you can modify this value in the script.

## Deploy the contract

You can use the public endpoints or node provider (such as Infura) to deploy your contract to the Linea mainnet or testnets.

Public endpoints are rate limited and not meant for production systems

We recommend using a node provider such as Infura. [Sign up for an Infura account](https://docs.metamask.io/developer-tools/dashboard/) to generate an API key. In the dashboard, enable the Linea endpoints you want to use with that key.

The sample project already includes the deployment script. To deploy on Linea, you'll just need to make a few modifications to the `hardhat.config.js` file:

1.  Create a `.env` file in the root folder with your wallet's private key and Infura API (if using Infura).

```text
PRIVATE_KEY=YOUR_PRIVATE_KEY
INFURA_API_KEY=INFURA_API_KEY
```

    warning

    Please do not check your keys into source control. Please add `.env` to your `.gitignore`

2.  Download and install `dotenv`

```text
npm i -D dotenv
```

3.  Add Linea to your `hardhat.config.js` file. The following example shows how to add an Infura endpoint and public endpoint to the configuration file.

    -   Infura
    -   Public endpoint

```javascript
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { PRIVATE_KEY, INFURA_API_KEY } = process.env;

module.exports = {
  solidity: "0.8.19",
  networks: {
    linea_sepolia: {
      url: `https://linea-sepolia.infura.io/v3/${INFURA_API_KEY}`,
      accounts: [PRIVATE_KEY],
    },
    linea_mainnet: {
      url: `https://linea-mainnet.infura.io/v3/${INFURA_API_KEY}`,
      accounts: [PRIVATE_KEY],
    },
  },
};
```

```javascript
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { PRIVATE_KEY } = process.env;

module.exports = {
  solidity: "0.8.24",
  networks: {
    linea_sepolia: {
      url: `https://rpc.sepolia.linea.build/`,
      accounts: [PRIVATE_KEY],
    },
    linea_mainnet: {
      url: `https://rpc.linea.build/`,
      accounts: [PRIVATE_KEY],
    },
  },
};
```

4.  Deploy your contract. Replace `NETWORK` with a relevant network defined in the `hardhat.config.js` file. For example `linea_mainnet`.

```bash
npx hardhat ignition deploy ./ignition/modules/Lock.js --network NETWORK
```

Your output should look something like this:

```bash
Deployed Addresses

LockModule#Lock - 0x2c73d6f093A2032D3371bFB9a29f7cE666080c4A
```

Next, you can optionally [verify your contract on the network](/network/how-to/verify-smart-contract/hardhat).
