Skip to main content

Use the Linea API

Linea supports the standard Ethereum JSON-RPC API methods, meaning the developer experience is identical to building on Ethereum itself. However, some Linea-specific methods, and method implementations differ to Ethereum.

View the full list of Linea methods in the

You must connect to an RPC endpoint when making calls to the Linea blockchain. Use one or more of the following options:

Make calls​

The following examples call the Linea API methods using an Infura endpoint, however you can substitute the endpoint with whichever endpoint you prefer.

In the examples, replace <YOUR-API-KEY> with your actual Infura API key.

if you require an endpoint. :::

cURL​

Run the curl command in a terminal:

curl https://linea-mainnet.infura.io/v3/<YOUR-API-KEY> \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 1}'

Node (JavaScript)​

The following examples use various JavaScript libraries to make calls to the Linea blockchain.

Prerequisites​

Install npm or yarn as the package manager. Then, in your project folder, initialise your new project:

npm init -y

Node Fetch​

  1. In your project folder, install the node-fetch package:

    npm i node-fetch
  2. Create your JavaScript file and copy the following code:

    index.js
    const fetch = require("node-fetch");

    fetch("https://linea-mainnet.infura.io/v3/<YOUR-API-KEY>", {
    method: "POST",
    headers: {
    "Content-Type": "application/json",
    },
    body: JSON.stringify({
    jsonrpc: "2.0",
    method: "eth_blockNumber",
    params: [],
    id: 1,
    }),
    })
    .then((response) => response.json())
    .then((data) => {
    console.log(data);
    })
    .catch((error) => {
    console.error(error);
    });
  3. Run the code using the following command:

    node index.js

Axios​

  1. In your project folder, install the axios package:

    npm i axios
  2. Create your JavaScript file and copy the following code:

    index.js
    const axios = require("axios");

    axios
    .post("https://linea-mainnet.infura.io/v3/<YOUR-API-KEY>", {
    jsonrpc: "2.0",
    method: "eth_blockNumber",
    params: [],
    id: 1,
    })
    .then((response) => {
    console.log(response.data);
    })
    .catch((error) => {
    console.error(error);
    });
  3. Run the code using the following command:

    node index.js

Viem​

  1. In your project folder, install the viem package:

    npm i viem
  2. Create your JavaScript file and copy the following code:

    index.js
    const { createClient, http } = require("viem");

    const client = createClient({
    transport: http("https://linea-mainnet.infura.io/v3/<YOUR-API-KEY>"),
    });

    client
    .request({
    method: "eth_blockNumber",
    params: [],
    })
    .then((blockNumber) => {
    console.log(parseInt(blockNumber, 16)); // Convert hex to decimal
    })
    .catch((error) => {
    console.error(error);
    });
  3. Run the code using the following command:

    node index.js