Arbitrum API Examples

Real-world examples on how to query information from Arbitrum using modern Web3 libraries.

If you’re looking to start querying Arbitrum blockchain data, here’s the place to start! We’ll walk you through setting up Viem and Ethers.js with Alchemy and show you common use cases.

Getting Started with Arbitrum

This guide assumes you already have an Alchemy account and access to our Dashboard.

1. Create an Alchemy Key

To access Alchemy’s free node infrastructure, you need an API key to authenticate your requests.

You can create API keys from the dashboard. Check out this video on how to create an app, replacing the chain “Ethereum” with “Arbitrum”.

Or follow the written steps below:

First, navigate to the “create app” button in the “Apps” tab.

3584

Fill in the details under “Create App” to get your new key. You can also see apps you previously made and those made by your team here. Make sure to select the Arbitrum chain.

Pull existing keys by clicking on “View Key” for any app.

991

You can also pull existing API keys by hovering over “Apps” and selecting one. You can “View Key” here, as well as “Edit App” to whitelist specific domains, see several developer tools, and view analytics.

600

2. Install and set up Web3 libraries

To get started with Arbitrum, you can use either Viem or Ethers.js. Create a project and install your preferred library:

$mkdir your-project-name
>cd your-project-name
>npm init -y
>npm install viem

Next, create a file named index.js and add the following contents:

Replace demo with your Alchemy API key from the dashboard.

1import { createPublicClient, http } from 'viem'
2import { arbitrum } from 'viem/chains'
3
4const client = createPublicClient({
5 chain: arbitrum,
6 transport: http('https://arb-mainnet.g.alchemy.com/v2/demo') // Replace 'demo' with your API key
7})
8
9async function main() {
10 const latestBlock = await client.getBlockNumber()
11 console.log('The latest block number is', latestBlock)
12}
13
14main()

Unfamiliar with the async stuff? Check out this Medium post.

3. Run your dApp using Node

shell
$node index.js

You should now see the latest block number output in your console!

shell
$The latest block number is 11043912

Below, we’ll list a number of examples on how to make common requests on Arbitrum or EVM blockchains.

How to Get the Latest Block Number on Arbitrum

If you’re looking for the latest block on Arbitrum, you can use the following:

1async function main() {
2 const latestBlock = await client.getBlockNumber()
3 console.log('The latest block number is', latestBlock)
4}
5
6main()

How to Get a Block By Its Block Hash on Arbitrum

Every block on Arbitrum corresponds to a specific hash. If you’d like to look up a block by its hash, you can use the following code:

1async function main() {
2 const block = await client.getBlock({
3 blockHash: '0x92fc42b9642023f2ee2e88094df80ce87e15d91afa812fef383e6e5cd96e2ed3'
4 })
5 console.log(block)
6}
7
8main()

How to Get a Block By Its Block Number on Arbitrum

Every block on Arbitrum corresponds to a specific number. If you’d like to look up a block by its number, you can use the following code:

1async function main() {
2 const block = await client.getBlock({
3 blockNumber: 15221026n
4 })
5 console.log(block)
6}
7
8main()

How to Get Logs for an Arbitrum Transaction

Logs are essentially a published list of user-defined events that have happened on the blockchain during an Arbitrum transaction. You can learn more about them in Understanding Logs: Deep Dive into eth_getLogs.

Here’s an example on how to write a getLogs query on Arbitrum:

1async function main() {
2 const logs = await client.getLogs({
3 address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
4 topics: [
5 '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
6 ],
7 blockHash: '0x49664d1de6b3915d7e6fa297ff4b3d1c5328b8ecf2ff0eefb912a4dc5f6ad4a0',
8 })
9 console.log(logs)
10}
11
12main()

How to Make an Eth_Call on Arbitrum

An eth_call in Arbitrum is essentially a way to execute a message call immediately without creating a transaction on the blockchain. It can be used to query internal contract state, to execute validations coded into a contract or even to test what the effect of a transaction would be without running it live.

Here’s an example on how to write a call query on Arbitrum:

1async function main() {
2 const result = await client.call({
3 to: '0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41',
4 gas: 0x76c0n,
5 gasPrice: 0x9184e72a000n,
6 data: '0x3b3b57debf074faa138b72c65adbdcfb329847e4f2c04bde7f7dd7fcad5a52d2f395a558',
7 })
8 console.log(result)
9}
10
11main()

How to Get a Transaction by Its Hash on Arbitrum

Every transaction on Arbitrum corresponds to a specific hash. If you’d like to look up a transaction by its hash, you can use the following code:

1async function main() {
2 const tx = await client.getTransaction({
3 hash: '0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b'
4 })
5 console.log(tx)
6}
7
8main()

How to Get a Transaction Receipt on Arbitrum

Every transaction on Arbitrum has an associated receipt with metadata about the transaction, such as the gas used and logs printed during the transaction. If you’d like to look up a transaction’s receipt, you can use the following code:

1async function main() {
2 const txReceipt = await client.getTransactionReceipt({
3 hash: '0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b'
4 })
5 console.log(txReceipt)
6}
7
8main()

How to get a User’s Transaction Count on Arbitrum

The number of transactions a user has sent is particularly important when it comes to calculating the nonce for sending new transactions on Arbitrum. Without it, you’ll be unable to send new transactions because your nonce won’t be set correctly.

1async function main() {
2 const txCount = await client.getTransactionCount({
3 address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
4 })
5 console.log(txCount)
6}
7
8main()

How to Fetch Historical Transactions on Arbitrum

Oftentimes, you’ll want to look up a set of transactions on Arbitrum between a set of blocks, corresponding to a set of token types such as ERC20 or ERC721, or with certain attributes. Alchemy’s proprietary Transfers API allows you to do so in milliseconds, rather than searching every block on the blockchain.

Fetch
1async function main() {
2 const response = await fetch('https://arb-mainnet.g.alchemy.com/v2/demo', {
3 method: 'POST',
4 headers: {
5 'Content-Type': 'application/json',
6 },
7 body: JSON.stringify({
8 jsonrpc: '2.0',
9 id: 0,
10 method: 'alchemy_getAssetTransfers',
11 params: [{
12 fromBlock: '0x0',
13 toBlock: 'latest',
14 contractAddresses: ['0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d'],
15 excludeZeroValue: true,
16 category: ['erc721'],
17 }]
18 })
19 })
20 const data = await response.json()
21 console.log(data.result)
22}
23
24main()

How to Subscribe to New Blocks on Arbitrum

If you’d like to open a WebSocket subscription to send you a JSON object whenever a new block is published to the blockchain, you can set one up using the following syntax.

1import { createPublicClient, webSocket } from 'viem'
2import { arbitrum } from 'viem/chains'
3
4const client = createPublicClient({
5 chain: arbitrum,
6 transport: webSocket('wss://arb-mainnet.g.alchemy.com/v2/demo')
7})
8
9// Subscription for new blocks on Arbitrum
10const unsubscribe = client.watchBlocks({
11 onBlock: (block) => {
12 console.log('The latest block number is', block.number)
13 }
14})

How to Estimate the Gas of a Transaction on Arbitrum

Oftentimes, you’ll need to calculate how much gas a particular transaction will use on the blockchain to understand the maximum amount that you’ll pay on that transaction. This example will return the gas used by the specified transaction:

1import { parseEther } from 'viem'
2
3async function main() {
4 const gasEstimate = await client.estimateGas({
5 // Wrapped ETH address (adjust for Arbitrum if needed)
6 to: '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1',
7 // `function deposit() payable`
8 data: '0xd0e30db0',
9 // 1 ether
10 value: parseEther('1.0'),
11 })
12 console.log(gasEstimate)
13}
14
15main()

How to Get the Current Gas Price in Arbitrum

You can retrieve the current gas price in wei using this method:

1async function main() {
2 const gasPrice = await client.getGasPrice()
3 console.log(gasPrice)
4}
5
6main()