🐹
ChainRPC
  • Welcome to Chain-RPC Docs
  • Getting started
  • Networks
    • Ethereum
      • Concepts
        • Transaction types
        • Filters and subscriptions
      • How to
        • Authenticate requests
        • Make requests
        • Make batch requests
        • Choose a network
        • Subscribe to events
        • Interact with ERC-20 tokens
        • Add a network add-on
        • Secure a project
          • API key
          • API key secret
          • JWTs
          • Rate limits
          • Allowlists
        • Avoid rate limiting
      • JSON-RPC methods
        • eth_accounts
        • eth_blockNumber
        • eth_call
        • eth_chainId
        • eth_coinbase
        • eth_estimateGas
        • eth_feeHistory
        • eth_getBalance
        • eth_gasPrice
        • eth_getBlockByHash
        • eth_getBlockByNumber
        • eth_getBlockTransactionCountByHash
        • eth_getBlockTransactionCountByNumber
        • eth_getCode
        • eth_getLogs
        • eth_getStorageAt
        • eth_getTransactionByBlockHashAndIndex
        • eth_getTransactionByBlockNumberAndIndex
        • eth_getTransactionByHash
        • eth_getTransactionCount
        • eth_getTransactionReceipt
        • eth_getUncleByBlockHashAndIndex
        • eth_getUncleByBlockNumberAndIndex
        • eth_getUncleCountByBlockHash
        • eth_getUncleCountByBlockNumber
        • eth_getWork
        • eth_mining
        • eth_hashrate
        • eth_protocolVersion
        • eth_sendRawTransaction
        • eth_sendTransaction
        • eth_sign
        • eth_submitWork
        • eth_syncing
        • net_listening
        • net_peerCount
        • net_version
        • web3_clientVersion
        • Filter methods
          • eth_newFilter
          • eth_newBlockFilter
          • eth_newPendingTransactionFilter
          • eth_getFilterLogs
          • eth_getFilterChanges
          • eth_uninstallFilter
        • Subscription methods
          • eth_subscribe
          • eth_unsubscribe
    • Arbitrum
      • How to
        • Authenticate requests
        • Make requests
        • Choose a network
        • Secure a project
        • Get testnet ETH
      • JSON-RPC API methods
    • Aurora
      • How to
      • JSON-RPC API methods
    • Avalanche (C-Chain)
      • How to
      • JSON-RPC API methods
    • BNB Chain
      • How to
      • JSON-RPC API methods
    • Celo
      • How to
      • JSON-RPC API methods
    • Cube
      • How to
      • JSON-RPC API methods
    • Fantom
      • How to
      • JSON-RPC API methods
    • Gnosis
      • How to
      • JSON-RPC API methods
    • Heco
      • How to
      • JSON-RPC API methods
    • Klaytn
      • How to
      • JSON-RPC API methods
    • Optimism
      • How to
      • JSON-RPC API methods
    • OEC Chain
      • How to
      • JSON-RPC API methods
    • Polygon PoS
      • How to
      • JSON-RPC API methods
  • Custom APIs
    • Wellat SDK
    • DeX SDK
    • NFT SDK
Powered by GitBook
On this page
  • curl or wscat
  • Postman
  • Web3.js
  • Ethers
  • NodeJS
  • Go
  • Python
  1. Networks
  2. Ethereum
  3. How to

Make requests

PreviousAuthenticate requestsNextMake batch requests

Last updated 2 years ago

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 to make the HTTPS requests and for WebSocket requests.

Ensure that you replaceYOUR-API-KEY with a API key from your chainRPC dashboard.

curl -X POST \
-H "Content-Type: application/json" \
--data '{"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}' \
"https://eth.chainrpc.io/v3/YOUR-API-KEY"
wscat -c wss://eth.chainrpc.io/v3/YOUR-API-KEY
> {"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}

Postman

Call the JSON-RPC methods using .

Click the Run in Postman button below to fork the collection and make requests.

Web3.js

Save the following script to a file, e.g. index.js

var Web3 = require('web3');
var provider = 'https://eth.chainrpc.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://eth.chainrpc.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: 'eth.chainrpc.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. Initialize a new go module: go mod init infura.

  2. Install the go-ethereum dependency: go get github.com/ethereum/go-ethereum/rpc.

  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://eth.chainrpc.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://eth.chainrpc.io/v3/<API-KEY>'))
print ("Latest Ethereum block number", connection.eth.blockNumber)

Output looks like:

Latest Ethereum block number 14659569

Set the correct for your API key and network before running requests.

variables
curl
wscat
Postman
Ethereum API Postman collection