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 methods differ to Ethereum — find them in the reference section.
View the full list of Linea methods in the MetaMask services documentation.
You must connect to an RPC endpoint when making calls to the Linea blockchain. Use one or more of the following options:
- Run your own node: Either run your own node by setting it up yourself, or use a node provider. We recommend running Linea Besu if you want to run a node yourself and interact with the blockchain.
- Connect to a private RPC endpoint: Connect to a blockchain infrastructure provider such as Infura or Alchemy. Multiple providers offer free tier access.
- Use a public endpoint: Public endpoints are free to use but are rate limited and not suitable for production environments.
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.
View the list of node providers 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
- yarn
npm init -y
yarn init -y
Node Fetch
-
In your project folder, install the
node-fetch
package:- npm
- yarn
npm i node-fetch
yarn add node-fetch
-
Create your JavaScript file and copy the following code:
index.jsconst 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)
}) -
Run the code using the following command:
node index.js
Axios
-
In your project folder, install the
axios
package:- npm
- yarn
npm i axios
yarn add axios
-
Create your JavaScript file and copy the following code:
index.jsconst 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)
}) -
Run the code using the following command:
node index.js
Viem
-
In your project folder, install the
viem
package:- npm
- yarn
npm i viem
yarn add viem
-
Create your JavaScript file and copy the following code:
index.jsconst { 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);
}); -
Run the code using the following command:
node index.js