Make requests

curl or wscat

The Ethereum network supports requests with HTTP or WebSockets. HTTP requires continual requests to the URL endpoint, whereas WebSockets maintains the connection.
Use curl to make the HTTPS requests and wscat for WebSocket requests.
Ensure that you replaceYOUR-PROJECT-ID with a project ID from your Infura dashboard.
HTTPS
WebSocket
1
curl -X POST \
2
-H "Content-Type: application/json" \
3
--data '{"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}' \
4
"https://mainnet.infura.io/v3/YOUR-PROJECT-ID"
Copied!
1
wscat -c wss://mainnet.infura.io/ws/v3/YOUR-PROJECT-ID
2
> {"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}
Copied!

Postman

Call the JSON-RPC methods using Postman.
Click the Run in Postman button below to fork the collection and make requests.
Set the correct variables for your project ID and network before running requests.
Ethereum API Postman collection

Web3.js

Save the following script to a file, e.g. index.js
1
var Web3 = require('web3');
2
var provider = 'https://mainnet.infura.io/v3/<PROJECT-ID>';
3
var web3Provider = new Web3.providers.HttpProvider(provider);
4
var web3 = new Web3(web3Provider);
5
web3.eth.getBlockNumber().then((result) => {
6
console.log("Latest Ethereum Block is ",result);
7
});
Copied!
In a terminal window, run the script with node index.js
1
Latest Ethereum Block is 14659509
Copied!

Ethers

Save the following script to a file, e.g. index.js
1
var ethers = require('ethers');
2
var url = 'https://mainnet.infura.io/v3/<PROJECT-ID>';
3
var customHttpProvider = new ethers.providers.JsonRpcProvider(url);
4
customHttpProvider.getBlockNumber().then((result) => {
5
console.log("Current block number: " + result);
6
});
Copied!
In a terminal window, run the script with node index.js
1
Latest Ethereum Block is 14659509
Copied!

NodeJS

Save the following script to a file, e.g. index.js
1
const https = require('https');
2
const projectId = '<PROJECT-ID>';
3
const data = JSON.stringify({
4
"jsonrpc":"2.0","method":"eth_blockNumber","params": [],"id":1
5
})
6
const options = {
7
host: 'mainnet.infura.io',
8
port: 443,
9
path: '/v3/' + projectId,
10
method: 'POST',
11
headers: {
12
'Content-Type': 'application/json'
13
},
14
};
15
const req = https.request(options, res => {
16
console.log(`statusCode: ${res.statusCode}`)
17
18
res.on('data', d => {
19
process.stdout.write(d)
20
})
21
})
22
23
req.on('error', error => {
24
console.error(error)
25
})
26
27
req.write(data)
28
req.end()
Copied!
In a terminal window, run the script with node index.js
1
statusCode: 200
2
{"jsonrpc":"2.0","id":1,"result":"0xe08e11"}
Copied!

Go

1
package main
2
import (
3
"fmt"
4
"log"
5
"github.com/ethereum/go-ethereum/rpc"
6
)
7
type Block struct {
8
Number string
9
}
10
func main() {
11
client, err := rpc.Dial("https://mainnet.infura.io/v3/<PROJECT_ID>")
12
if err != nil {
13
log.Fatalf("Could not connect to Infura: %v", err)
14
}
15
var lastBlock Block
16
err = client.Call(&lastBlock, "eth_getBlockByNumber", "latest", true)
17
if err != nil {
18
fmt.Println("Cannot get the latest block:", err)
19
return
20
}
21
fmt.Printf("Latest block: %v\n", lastBlock.Number)
22
}
Copied!

Python

Run the following code with Python.
1
from web3 import Web3, HTTPProvider
2
connection = Web3(HTTPProvider('https://mainnet.infura.io/v3/<PROJECT-ID>'))
3
print ("Latest Ethereum block number", connection.eth.blockNumber)
Copied!
Output looks like:
1
Latest Ethereum block number 14659569
Copied!