Optimism API
Below you will find the standard Optimism JSON-RPC calls that are compatible with Alchemy.
IMPORTANT: In order to deploy to Optimism, users have to apply to be whitelisted. Click here for more information from the Optimism documentation, and use this form to apply.
For more information on the Optimism, JSON-RPC check out the Optimism Wiki.
HINT: Most JSON-RPC methods in Optimistic Ethereum are identical to the corresponding methods in the Ethereum JSON-RPC API. However, a few JSON-RPC methods have been added or changed to better fit the needs of Optimistic Ethereum.

Mainnet vs. Testnet

There are two networks on Optimism: Mainnet and Kovan testnet. The endpoints are as follows:
  • Mainnet: https://opt-mainnet.g.alchemy.com/v2/your-api-key
  • Kovan: https://opt-kovan.g.alchemy.com/v2/your-api-key
Currently, Alchemy does not support pending transactions for Optimism websockets.

​
πŸ“¦
Retrieving Blocks

Calls related to retrieving blocks and block information.

eth_blockNumber

Returns the number of the most recent block.

Parameters

none

Returns

QUANTITY - integer of the current block number the client is on.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemy.com/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemy.com/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_blockNumber",
7
"params":[],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": "0x41881"
5
}
Copied!

eth_getBlockByHash

Currently, Optimistic Ethereum blocks only include a single transaction. If you query eth_getBlockByNumber or eth_getBlockByHash, you should expect to only see one transaction.
Returns information about a block by hash.

Parameters

  • DATA, 32 Bytes - Hash of a block.
  • Boolean - If true it returns the full transaction objects, if false it returns only the hashes of the transactions.
1
params: [
2
'0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae',
3
true
4
]
Copied!

Returns

Object - A block object with the following fields, or null when no block was found:
  • number: QUANTITY - the block number. null when its pending block.
  • hash: DATA, 32 Bytes - hash of the block. null when its pending block.
  • parentHash: DATA, 32 Bytes - hash of the parent block.
  • nonce: DATA, 8 Bytes - hash of the generated proof-of-work. null when its pending block.
  • sha3Uncles: DATA, 32 Bytes - SHA3 of the uncles data in the block.
  • logsBloom: DATA, 256 Bytes - the bloom filter for the logs of the block. null when its pending block.
  • transactionsRoot: DATA, 32 Bytes - the root of the transaction trie of the block.
  • stateRoot: DATA, 32 Bytes - the root of the final state trie of the block.
  • receiptsRoot: DATA, 32 Bytes - the root of the receipts trie of the block.
  • miner: DATA, 20 Bytes - the address of the beneficiary to whom the mining rewards were given.
  • difficulty: QUANTITY - integer of the difficulty for this block.
  • totalDifficulty: QUANTITY - integer of the total difficulty of the chain until this block.
  • extraData: DATA - the "extra data" field of this block.
  • size: QUANTITY - integer the size of this block in bytes.
  • gasLimit: QUANTITY - the maximum gas allowed in this block.
  • gasUsed: QUANTITY - the total used gas by all transactions in this block.
  • timestamp: QUANTITY - the unix timestamp for when the block was collated.
  • transactions: Array - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
  • uncles: Array - Array of uncle hashes.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemy.com/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getBlockByHash","params":["0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae", true],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemy.com/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getBlockByHash",
7
"params":["0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae", true],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": {
5
"difficulty": "0x2d50ba175407",
6
"extraData": "0xe4b883e5bda9e7a59ee4bb99e9b1bc",
7
"gasLimit": "0x47e7c4",
8
"gasUsed": "0x5208",
9
"hash": "0xc0f4906fea23cf6f3cce98cb44e8e1449e455b28d684dfa9ff65426495584de6",
10
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
11
"miner": "0x61c808d82a3ac53231750dadc13c777b59310bd9",
12
"mixHash": "0xc38853328f753c455edaa4dfc6f62a435e05061beac136c13dbdcd0ff38e5f40",
13
"nonce": "0x3b05c6d5524209f1",
14
"number": "0x1e8480",
15
"parentHash": "0x57ebf07eb9ed1137d41447020a25e51d30a0c272b5896571499c82c33ecb7288",
16
"receiptsRoot": "0x84aea4a7aad5c5899bd5cfc7f309cc379009d30179316a2a7baa4a2ea4a438ac",
17
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
18
"size": "0x28a",
19
"stateRoot": "0x96dbad955b166f5119793815c36f11ffa909859bbfeb64b735cca37cbf10bef1",
20
"timestamp": "0x57a1118a",
21
"totalDifficulty": "0x262c34a6fd1268f6c",
22
"transactions": [
23
"0xc55e2b90168af6972193c1f86fa4d7d7b31a29c156665d15b9cd48618b5177ef"
24
],
25
"transactionsRoot": "0xb31f174d27b99cdae8e746bd138a01ce60d8dd7b224f7c60845914def05ecc58",
26
"uncles": []
27
}
28
}
Copied!

eth_getBlockByNumber

Currently, Optimistic Ethereum blocks only include a single transaction. If you query eth_getBlockByNumber or eth_getBlockByHash, you should expect to only see one transaction.
Returns information about a block by block number.

Parameters

  • QUANTITY|TAG - integer of a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
  • Boolean - If true it returns the full transaction objects, if false only the hashes of the transactions.
1
params: [
2
'0x1b4',
3
true
4
]
Copied!

Returns

Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemy.com/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x1b4", true],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemy.com/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getBlockByNumber",
7
"params":["0x1b4", true],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": {
5
"difficulty": "0x2",
6
"extraData": "0xd98301090a846765746889676f312e31352e3133856c696e7578000000000000138670a98dc89f978cb46c35cf92c6308bc63660af42e30e6639e401e11da77c693dde6324c8ec5ed641ecc2f82b8d747d6ffcd7f4e03c7694dde6b12f5b252000",
7
"gasLimit": "0xa7d8c0",
8
"gasUsed": "0x379e70",
9
"hash": "0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae",
10
"logsBloom": "0x00000000000000000000000000400100000000000000000000040000000000000000000000000000000000900002000100000000000000000000000000000000000000000001000000000008104000000000000000000000000000400000000000000000000000000000000000000000200000000040000000000010000000000008000000040000000000000000000000000000002000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000002000100000000000000000000000000020000000000000000000000000000000000200008000000000000000000000000000000000000000000000000",
11
"miner": "0x0000000000000000000000000000000000000000",
12
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
13
"nonce": "0x0000000000000000",
14
"number": "0x1b4",
15
"parentHash": "0x17433dcb86d75391996efcef54b00846f1375551009328c98e2f17da793c9414",
16
"receiptsRoot": "0x437d8dec027b1798878814d484fcf1cfb315f0b401767f150613e7ce1bb67d8c",
17
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
18
"size": "0x2cc",
19
"stateRoot": "0x86830e056ff5841c615b0a3f1d0dac42bd0e27ca1206593b25f6d358c3c7fe24",
20
"timestamp": "0x60d34b60",
21
"totalDifficulty": "0x369",
22
"transactions": [
23
{
24
"blockHash": "0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae",
25
"blockNumber": "0x1b4",
26
"from": "0x3b179dcfc5faa677044c27dce958e4bc0ad696a6",
27
"gas": "0x11cbbdc",
28
"gasPrice": "0x0",
29
"hash": "0x2642e960d3150244e298d52b5b0f024782253e6d0b2c9a01dd4858f7b4665a3f",
30
"input": "0xd294f093",
31
"nonce": "0xa2",
32
"to": "0x4a16a42407aa491564643e1dfc1fd50af29794ef",
33
"transactionIndex": "0x0",
34
"value": "0x0",
35
"v": "0x38",
36
"r": "0x6fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2bee",
37
"s": "0xe804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583",
38
"queueOrigin": "sequencer",
39
"txType": "",
40
"l1TxOrigin": null,
41
"l1BlockNumber": "0xc1a65c",
42
"l1Timestamp": "0x60d34b60",
43
"index": "0x1b3",
44
"queueIndex": null,
45
"rawTransaction": "0xf86681a28084011cbbdc944a16a42407aa491564643e1dfc1fd50af29794ef8084d294f09338a06fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2beea00e804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583"
46
}
47
],
48
"transactionsRoot": "0x874e61dfee4625c52bf64a90805f60c03cfd37eb35f43f46fa1f755b80a12223",
49
"uncles": []
50
}
51
}
Copied!

​
🧾
Reading Transactions

Calls for reading transactions.
NOTE: On Optimism, there is one L2 block mined for each L2 transaction.

eth_getTransactionByHash

Returns the information about a transaction requested by transaction hash. In the response object, blockHash, blockNumber, and transactionIndex are null when the transaction is pending.

Parameters

DATA, 32 Bytes - hash of a transaction
1
params: [
2
"0x2642e960d3150244e298d52b5b0f024782253e6d0b2c9a01dd4858f7b4665a3f"
3
]
Copied!

Returns

Object - A transaction object, or null when no transaction was found:
  • blockHash: DATA, 32 Bytes - hash of the block where this transaction was in. null when its pending.
  • blockNumber: QUANTITY - block number where this transaction was in. null when it's pending.
  • from: DATA, 20 Bytes - address of the sender.
  • gas: QUANTITY - gas provided by the sender.
  • gasPrice: QUANTITY - gas price provided by the sender in Wei.
  • hash: DATA, 32 Bytes - hash of the transaction.
  • input: DATA - the data send along with the transaction.
  • nonce: QUANTITY - the number of transactions made by the sender prior to this one.
  • to: DATA, 20 Bytes - address of the receiver. null when it's a contract creation transaction.
  • transactionIndex: QUANTITY - integer of the transactions index position in the block. null when its pending.
  • value: QUANTITY - value transferred in Wei.
  • v: QUANTITY - ECDSA recovery id
  • r: DATA, 32 Bytes - ECDSA signature r
  • s: DATA, 32 Bytes - ECDSA signature s
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemy.com/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d'{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0x2642e960d3150244e298d52b5b0f024782253e6d0b2c9a01dd4858f7b4665a3f"],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemy.com/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getTransactionByHash",
7
"params":["0x2642e960d3150244e298d52b5b0f024782253e6d0b2c9a01dd4858f7b4665a3f"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": {
5
"blockHash": "0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae",
6
"blockNumber": "0x1b4",
7
"from": "0x3b179dcfc5faa677044c27dce958e4bc0ad696a6",
8
"gas": "0x11cbbdc",
9
"gasPrice": "0x0",
10
"hash": "0x2642e960d3150244e298d52b5b0f024782253e6d0b2c9a01dd4858f7b4665a3f",
11
"input": "0xd294f093",
12
"nonce": "0xa2",
13
"to": "0x4a16a42407aa491564643e1dfc1fd50af29794ef",
14
"transactionIndex": "0x0",
15
"value": "0x0",
16
"v": "0x38",
17
"r": "0x6fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2bee",
18
"s": "0xe804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583",
19
"queueOrigin": "sequencer",
20
"txType": "",
21
"l1TxOrigin": null,
22
"l1BlockNumber": "0xc1a65c",
23
"l1Timestamp": "0x60d34b60",
24
"index": "0x1b3",
25
"queueIndex": null,
26
"rawTransaction": "0xf86681a28084011cbbdc944a16a42407aa491564643e1dfc1fd50af29794ef8084d294f09338a06fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2beea00e804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583"
27
}
28
}
Copied!

eth_getTransactionCount

Returns the number of transactions sent from an address.

Parameters

  • DATA, 20 Bytes - address.
  • QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
1
params: [
2
'0xd44fd1c1fe3f3f0d93a8867af4041ab231783fcb',
3
'latest' // state at the latest block
4
]
Copied!

Returns

QUANTITY - integer of the number of transactions send from this address.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemy.com/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xd44fd1c1fe3f3f0d93a8867af4041ab231783fcb","latest"],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemy.com/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getTransactionCount",
7
"params":["0xd44fd1c1fe3f3f0d93a8867af4041ab231783fcb","latest"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": "0x2fcf"
5
}
Copied!

eth_getTransactionReceipt

Returns the receipt of a transaction by transaction hash.
This can also be used to track the status of a transaction, since the result will be null until the transaction is mined. However, unlike eth_getTransactionByHash , which returns null for unknown transactions, and a non-null response with 3 null fields for a pending transaction, eth_getTransactionReceipt returns null for both pending and unknown transactions.
This call is also commonly used to get the contract address for a contract creation tx.
Note: The receipt is not available for pending transactions.

Parameters

DATA, 32 Bytes - hash of a transaction
1
params: [
2
'0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad'
3
]
Copied!

Returns

Object - A transaction receipt object, or null when no receipt was found:
  • transactionHash: DATA, 32 Bytes - hash of the transaction.
  • transactionIndex: QUANTITY - integer of the transactions index position in the block.
  • blockHash: DATA, 32 Bytes - hash of the block where this transaction was in.
  • blockNumber: QUANTITY - block number where this transaction was in.
  • from: DATA, 20 Bytes - address of the sender.
  • to: DATA, 20 Bytes - address of the receiver. null when its a contract creation transaction.
  • cumulativeGasUsed: QUANTITY - The total amount of gas used when this transaction was executed in the block.
  • gasUsed: QUANTITY - The amount of gas used by this specific transaction alone.
  • contractAddress: DATA, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwise null.
  • logs: Array - Array of log objects, which this transaction generated.
  • logsBloom: DATA, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs.
It also returns either:
  • root : DATA 32 bytes of post-transaction stateroot (pre Byzantium)
  • status: QUANTITY either 1 (success) or 0 (failure)
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad"],"id":0}
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getTransactionReceipt",
7
"params":["0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": {
5
"blockHash": "0x7cf4df93eb84897abd5b913e0e8625d1158f7661759c22d4b5995702e576398b",
6
"blockNumber": "0x41fe0",
7
"contractAddress": null,
8
"cumulativeGasUsed": "0x169e81",
9
"from": "0xd44fd1c1fe3f3f0d93a8867af4041ab231783fcb",
10
"gasUsed": "0x169e81",
11
"logs": [
12
{
13
"address": "0x4200000000000000000000000000000000000006",
14
"topics": [
15
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
16
"0x000000000000000000000000d44fd1c1fe3f3f0d93a8867af4041ab231783fcb",
17
"0x0000000000000000000000004200000000000000000000000000000000000011"
18
],
19
"data": "0x0000000000000000000000000000000000000000000000000001f4d05e595b80",
20
"blockNumber": "0x41fe0",
21
"transactionHash": "0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad",
22
"transactionIndex": "0x0",
23
"blockHash": "0x7cf4df93eb84897abd5b913e0e8625d1158f7661759c22d4b5995702e576398b",
24
"logIndex": "0x0",
25
"removed": false
26
},
27
{
28
"address": "0x2e4a187732166a0282e52527b931c96146ac7c58",
29
"topics": [
30
"0x92e98423f8adac6e64d0608e519fd1cefb861498385c6dee70d58fc926ddc68c",
31
"0x00000000000000000000000000000000000000000000000000000000384d73c0",
32
"0x0000000000000000000000000000000000000000000000000000000000001314",
33
"0x000000000000000000000000d44fd1c1fe3f3f0d93a8867af4041ab231783fcb"
34
],
35
"data": "0x",
36
"blockNumber": "0x41fe0",
37
"transactionHash": "0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad",
38
"transactionIndex": "0x0",
39
"blockHash": "0x7cf4df93eb84897abd5b913e0e8625d1158f7661759c22d4b5995702e576398b",
40
"logIndex": "0x1",
41
"removed": false
42
},
43
{
44
"address": "0x2e4a187732166a0282e52527b931c96146ac7c58",
45
"topics": [
46
"0x0559884fd3a460db3073b7fc896cc77986f16e378210ded43186175bf646fc5f",
47
"0x0000000000000000000000000000000000000000000000000000000038538e40",
48
"0x0000000000000000000000000000000000000000000000000000000000001314"
49
],
50
"data": "0x000000000000000000000000000000000000000000000000000000006108559e",
51
"blockNumber": "0x41fe0",
52
"transactionHash": "0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad",
53
"transactionIndex": "0x0",
54
"blockHash": "0x7cf4df93eb84897abd5b913e0e8625d1158f7661759c22d4b5995702e576398b",
55
"logIndex": "0x2",
56
"removed": false
57
},
58
{
59
"address": "0x2e4a187732166a0282e52527b931c96146ac7c58",
60
"topics": [
61
"0xfe25c73e3b9089fac37d55c4c7efcba6f04af04cebd2fc4d6d7dbb07e1e5234f",
62
"0x00000000000000000000000000000000000000000000000e34ac9e997f1d0000"
63
],
64
"data": "0x",
65
"blockNumber": "0x41fe0",
66
"transactionHash": "0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad",
67
"transactionIndex": "0x0",
68
"blockHash": "0x7cf4df93eb84897abd5b913e0e8625d1158f7661759c22d4b5995702e576398b",
69
"logIndex": "0x3",
70
"removed": false
71
}
72
],
73
"logsBloom": "0x000000000000000000000000000000a0000000000000000000042000008000000000400000004000000000100000004100000000000200000040000000000000100000000000000004000008002000000000000000000000000000400000000000000000080000000000002020040000400000000400000000000010100000000000000000000000000000000000000000000480002000000000000000000000000000000000000000000008000000000000000000000010000000000000000000400002000000000000000000000008000000002000000000000000000000000000000000001008000000000000000000000000000000000000000000000000",
74
"status": "0x1",
75
"to": "0x2e4a187732166a0282e52527b931c96146ac7c58",
76
"transactionHash": "0x09947eaa4920d9d1a5b443c22e3cda1a14baa6b1f8607559de38ac05ff99e5ad",
77
"transactionIndex": "0x0"
78
}
79
}
Copied!

eth_getBlockTransactionCountByHash

Returns the number of transactions in a block matching the given block hash.

Parameters

  • DATA, 32 Bytes - hash of a block.
    1
    params: [
    2
    '0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae'
    3
    ]
    Copied!

Returns

  • QUANTITY - integer of the number of transactions in this block.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByHash","params":["0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae"],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getBlockTransactionCountByHash",
7
"params":["0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": "0x1"
5
}
Copied!

eth_getBlockTransactionCountByNumber

Returns the number of transactions in a block matching the given block number.

Parameters

  • QUANTITY|TAG - integer of a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
1
params: [
2
'latest',
3
]
Copied!

Returns

  • QUANTITY - integer of the number of transactions in this block.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["latest"],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getBlockTransactionCountByNumber",
7
"params":["latest"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": "0x1"
5
}
Copied!

eth_getTransactionByBlockHashAndIndex

Returns information about a transaction by block hash and transaction index position.

Parameters

DATA, 32 Bytes - hash of a block.
QUANTITY - integer of the transaction index position.
1
params: [
2
'0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae',
3
'0x0' // 0
4
]
Copied!

Returns

Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getTransactionByBlockHashAndIndex","params":["0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae", "0x0"],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getTransactionByBlockHashAndIndex",
7
"params":["0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae", "0x0"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": {
5
"blockHash": "0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae",
6
"blockNumber": "0x1b4",
7
"from": "0x3b179dcfc5faa677044c27dce958e4bc0ad696a6",
8
"gas": "0x11cbbdc",
9
"gasPrice": "0x0",
10
"hash": "0x2642e960d3150244e298d52b5b0f024782253e6d0b2c9a01dd4858f7b4665a3f",
11
"input": "0xd294f093",
12
"nonce": "0xa2",
13
"to": "0x4a16a42407aa491564643e1dfc1fd50af29794ef",
14
"transactionIndex": "0x0",
15
"value": "0x0",
16
"v": "0x38",
17
"r": "0x6fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2bee",
18
"s": "0xe804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583",
19
"queueOrigin": "sequencer",
20
"txType": "",
21
"l1TxOrigin": null,
22
"l1BlockNumber": "0xc1a65c",
23
"l1Timestamp": "0x60d34b60",
24
"index": "0x1b3",
25
"queueIndex": null,
26
"rawTransaction": "0xf86681a28084011cbbdc944a16a42407aa491564643e1dfc1fd50af29794ef8084d294f09338a06fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2beea00e804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583"
27
}
28
}
Copied!

eth_getTransactionByBlockNumberAndIndex

Returns information about a transaction by block number and transaction index position.

Parameters

  • QUANTITY|TAG - a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
  • QUANTITY - the transaction index position.
1
params: [
2
'latest', // 668
3
'0x0' // 0
4
]
Copied!

Returns

​
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getTransactionByBlockNumberAndIndex","params":["latest", "0x0"],"id":0}'
Copied!
1
URL: https://eth-mainnet.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getTransactionByBlockNumberAndIndex",
7
"params":["latest", "0x0"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": {
5
"blockHash": "0x02b853cf50bc1c335b70790f93d5a390a35a166bea9c895e685cc866e4961cae",
6
"blockNumber": "0x1b4",
7
"from": "0x3b179dcfc5faa677044c27dce958e4bc0ad696a6",
8
"gas": "0x11cbbdc",
9
"gasPrice": "0x0",
10
"hash": "0x2642e960d3150244e298d52b5b0f024782253e6d0b2c9a01dd4858f7b4665a3f",
11
"input": "0xd294f093",
12
"nonce": "0xa2",
13
"to": "0x4a16a42407aa491564643e1dfc1fd50af29794ef",
14
"transactionIndex": "0x0",
15
"value": "0x0",
16
"v": "0x38",
17
"r": "0x6fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2bee",
18
"s": "0xe804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583",
19
"queueOrigin": "sequencer",
20
"txType": "",
21
"l1TxOrigin": null,
22
"l1BlockNumber": "0xc1a65c",
23
"l1Timestamp": "0x60d34b60",
24
"index": "0x1b3",
25
"queueIndex": null,
26
"rawTransaction": "0xf86681a28084011cbbdc944a16a42407aa491564643e1dfc1fd50af29794ef8084d294f09338a06fca94073a0cf3381978662d46cf890602d3e9ccf6a31e4b69e8ecbd995e2beea00e804161a2b56a37ca1f6f4c4b8bce926587afa0d9b1acc5165e6556c959d583"
27
}
28
}
Copied!

​
✍
Writing Transactions

Call to write to the blockchain.

eth_sendRawTransaction

Creates a new message call transaction or a contract creation for signed transactions.
Alchemy does not store keys, so transactions sent via Alchemy must be signed ahead of time using another provider like ethers (via eth_signTransaction) and sent with eth_sendRawTransaction.
NOTE: Writing data on Optimism is handled by its sequencers; for more information on specifics, please refer to the Optimism docs.

Parameters

DATA, The signed transaction data.
1
params: ["0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"]
Copied!

Returns

DATA, 32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available.
Use eth_getTransactionReceipt to get the contract address after the transaction was mined when you created a contract.
Note: Since eth_sendRawTransaction is a request used for writing to the blockchain and changes its state, it is impossible to execute the same request twice. This means if you were to copy the example given below you will not get the expected response.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"],"id":1}'
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_sendRawTransaction",
7
"params":["0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"],
8
"id":1
9
}
Copied!
Result
1
{
2
"id":1,
3
"jsonrpc": "2.0",
4
"result": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"
5
}
Copied!

​
πŸ“‚
Account Information

Calls to get information about an account.

eth_getBalance

Returns the balance of the account of a given address.

Parameters

  1. 1.
    DATA, 20 Bytes - address to check for balance.
  2. 2.
    QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
1
params: [
2
'0xf69d0bbc95db6287ef02f19e5b2789972f776c2f',
3
'latest'
4
]
Copied!

Returns

QUANTITY - integer of the current balance for the given address in wei.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xf69d0bbc95db6287ef02f19e5b2789972f776c2f", "latest"],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getBalance",
7
"params":["0xf69d0bbc95db6287ef02f19e5b2789972f776c2f", "latest"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": "0x206c36e81d47c480"
5
}
Copied!

eth_getCode

Returns code at a given address. This method can be used to distinguish between contract addresses and wallet addresses.

Parameters

  • DATA, 20 Bytes - address.
  • QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
1
params: [
2
'0xf69d0bbc95db6287ef02f19e5b2789972f776c2f',
3
'latest'
4
]
Copied!

Returns

  • DATA - the code from the given address.
Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_getCode","params":["0xf69d0bbc95db6287ef02f19e5b2789972f776c2f", "latest"],"id":0}'
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getCode",
7
"params":["0xf69d0bbc95db6287ef02f19e5b2789972f776c2f", "latest"],
8
"id":0
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 0,
4
"result": "0x6080604052600436106100295760003560e01c80630900f010146100c8578063aaf10f4214610117575b600080610034610159565b6001600160a01b0316600036604051808383808284378083019250505092505050600060405180830381855a610068610405565b50505050509150503d806000811461009c576040513d603f01601f191681016040523d815291503d6000602084013e6100a1565b606091505b509150915081156100b457805160208201f35b8051602082016100c26104f2565b50505050005b5a6100d161055d565b80156100e5576000806100e26104f2565b50505b5061011560048036036020811015610105576000806101026104f2565b50505b50356001600160a01b03166101bf565b005b5a61012061055d565b8015610134576000806101316104f2565b50505b5061013d610159565b6040516001600160a01b03909116815260200160405180910390f35b6000807f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc6101856105b7565b9050600061019282610269565b90506001600160a01b0381166101b3576003602160991b01925050506101bc565b91506101bc9050565b90565b6101c7610270565b6001600160a01b03165a6101d9610603565b6001600160a01b0316146102275760405162461bcd60e51b815260040180806020018281038252603381526020018061069860339139604001915050604051809103906102246104f2565b50505b610230816102dd565b806001600160a01b03167fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b60405160405180910390a250565b805b919050565b6000806102af604051602401604051601f1981830301815260409190915263996d79a560e01b6020820180516001600160e01b0316909117905261031a565b905060208101815160208110156102ce576000806102cb6104f2565b50505b81019080805194505050505090565b60006102e8826103f9565b9050807f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc610314610649565b50505050565b6060600080600b602160991b01846040518082805190602001908083835b602083106103575780518252601f199092019160209182019101610338565b6001836020036101000a038019825116818451168082178552505050505050905001915050600060405180830381855a61038f610405565b50505050509150503d80600081146103c3576040513d603f01601f191681016040523d815291503d6000602084013e6103c8565b606091505b509092509050600182151514156103e257915061026b9050565b8051602082016103f06104f2565b50505050919050565b6001600160a01b031690565b63ffe73914598160e01b8152610438565b80808311156104225750815b92915050565b8080831015610422575090919050565b836004820152846024820152606060448201528660648201526084810160005b88811015610470578088015182820152602001610458565b506060828960a40184336000905af158600e01573d6000803e3d6000fd5b3d6001141558600a015760016000f35b815160408301513d6000853e8b8b82606087013350600060045af150596104c58d3d610428565b8c016104d18187610416565b5b828110156104e657600081526020016104d2565b50929c50505050505050565b632a2a7adb598160e01b8152600481016020815285602082015260005b8681101561052a57808601518282016040015260200161050f565b506020828760640184336000905af158600e01573d6000803e3d6000fd5b3d6001141558600a015760016000f35b505050565b63a8c4c5ec598160e01b8152602081600483336000905af158600e01573d6000803e3d6000fd5b3d6001141558600a015760016000f35b8051935060005b60408110156105b25760008282015260200161059b565b505050565b6303daa959598160e01b8152836004820152602081602483336000905af158600e01573d6000803e3d6000fd5b3d6001141558600a015760016000f35b8051600082529350602061059b565b6373509064598160e01b8152602081600483336000905af158600e01573d6000803e3d6000fd5b3d6001141558600a015760016000f35b8051600082529350602061059b565b6322bd64c0598160e01b8152836004820152846024820152600081604483336000905af158600e01573d6000803e3d6000fd5b3d6001141558600a015760016000f35b60008152602061059b56fe454f41732063616e206f6e6c792075706772616465207468656972206f776e20454f4120696d706c656d656e746174696f6e2e"
5
}
Copied!

eth_getStorageAt

Returns the value from a storage position at a given address, or in other words, returns the state of the contract's storage, which may not be exposed via the contract's methods.

Parameters

  • DATA, 20 Bytes - address of the storage.
  • QUANTITY - integer of the position in the storage.
  • QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.

Returns

  • DATA - the value at this storage position.

Example

Calculating the correct position depends on the storage to retrieve. Consider the following contract deployed at 0x295a70b2de5e3953354a6a8344e616ed314d7251 by address 0x391694e7e0b0cce554cb130d723a9d27458f9298.
1
contract Storage {
2
uint pos0;
3
mapping(address => uint) pos1;
4
​
5
function Storage() {
6
pos0 = 1234;
7
pos1[msg.sender] = 5678;
8
}
9
}
Copied!
Retrieving the value of pos0 is straight forward:
Request
Curl
Postman
1
curl https://eth-mainnet.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0", "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x0", "latest"], "id": 1}'
Copied!
1
URL: https://eth-mainnet.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body:
4
{
5
"jsonrpc":"2.0",
6
"method":"eth_getStorageAt",
7
"params":["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x0", "latest"],
8
"id":1
9
}
Copied!
Result
1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": "0x0000000000000000000000000000000000000000000000000000000000000000"
5
}
Copied!

eth_accounts

Returns a list of addresses owned by client.
Since Alchemy does not store keys, this will always return empty.

Parameters

none

Returns

Array of DATA, 20 Bytes - addresses owned by the client.

​Example​

Request
Curl
Postman
1
curl https://opt-mainnet.g.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}'
Copied!
1
URL: https://opt-mainnet.g.alchemyapi.io/v2/your-api-key
2
RequestType: POST
3
Body: