Links

Make requests

The Ethereum network supports requests with HTTP or WebSockets. HTTP requires continual requests to the URL endpoint, whereas WebSockets maintains the connection. You can also make batch requests to the Ethereum network.

curl or wscat

Use curl to make the HTTPS requests and wscat for WebSocket requests.
Ensure that you replaceYOUR-API-KEY with a API key from your Infura dashboard.
HTTPS
WebSocket
curl -X POST \
-H "Content-Type: application/json" \
--data '{"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}' \
"https://mainnet.infura.io/v3/YOUR-API-KEY"
wscat -c wss://mainnet.infura.io/ws/v3/YOUR-API-KEY
> {"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}
In Windows Powershell, quotations in curl commands can behave differently than expected. We recommend using Postman on Windows systems.

Postman

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

Web3.js

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

Ethers

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

NodeJS

Save the following script to a file, e.g. index.js
const https = require('https');
const projectId = '<API-KEY>';
const data = JSON.stringify({
"jsonrpc":"2.0","method":"eth_blockNumber","params": [],"id":1
})
const options = {
host: 'mainnet.infura.io',
port: 443,
path: '/v3/' + projectId,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.write(data)
req.end()
In a terminal window, run the script with node index.js
statusCode: 200
{"jsonrpc":"2.0","id":1,"result":"0xe08e11"}

Go

  1. 1.
    Initialize a new go module: go mod init infura.
  2. 2.
    Install the go-ethereum dependency: go get github.com/ethereum/go-ethereum/rpc.
  3. 3.
    Save the following script to a file, e.g. infura.go.
package main
import (
"fmt"
"log"
"github.com/ethereum/go-ethereum/rpc"
)
type Block struct {
Number string
}
func main() {
client, err := rpc.Dial("https://mainnet.infura.io/v3/<API-KEY>")
if err != nil {
log.Fatalf("Could not connect to Infura: %v", err)
}
var lastBlock Block
err = client.Call(&lastBlock, "eth_getBlockByNumber", "latest", true)
if err != nil {
fmt.Println("Cannot get the latest block:", err)
return
}
fmt.Printf("Latest block: %v\n", lastBlock.Number)
}
In a terminal window, run the script with go run infura.go.
Output:
Latest block: 0x....

Python

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