Make requests

curl or wscat

The BSC 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 BSC network.

Use curl to make the HTTPS requests and wscat for WebSocket requests.

curl -X POST \
-H "Content-Type: application/json" \
--data '{"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}' \
"https://bsc-mainnet.paralinker.com/api/v1/API-KEY"

Web3.js

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

var Web3 = require('web3');
var provider = 'https://bsc-mainnet.paralinker.com/api/v1/<API-KEY>';
var web3Provider = new Web3.providers.HttpProvider(provider);
var web3 = new Web3(web3Provider);
web3.eth.getBlockNumber().then((result) => {
  console.log("Latest BSC 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://bsc-mainnet.paralinker.com/api/v1/<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: 'bsc-mainnet.paralinker.com',
    port: 443,
    path: 'api/v1/' + 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 bsc.

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

  3. Save the following script to a file, e.g. bsc.go.

package main

import (
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/rpc"
)

type Block struct {
    Number string
}

func main() {
    client, err := rpc.Dial("https://bsc-mainnet.paralinker.com/api/v1/<API-KEY>")
    if err != nil {
        log.Fatalf("Could not connect to Mainnet: %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 bsc.go.

Output:

Latest block: 0x....

Python

Run the following code with Python.

from web3 import Web3, HTTPProvider
connection = Web3(HTTPProvider('https://bsc-mainnet.paralinker.com/api/v1/<API-KEY>'))
print ("Latest Ethereum block number", connection.eth.blockNumber)

Output looks like:

Latest Ethereum block number 14659569

Last updated