A
A
Ankr Docs
Search…
Ethereum Based Examples
NOTE: The following examples are based on calls to Binance Smart Chain and are for reference only.

API Endpoint Examples

Curl
Go
Javascript
Python
1
# basic authentication
2
$ curl -H "Content-Type: application/json" -u "username:password" https://apis.ankr.com/xxxxx/xxxxx/binance/full/main -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
3
4
# basic authentication way two
5
$ curl -H "Content-Type: application/json" https://username:[email protected]/xxxxx/xxxxx/binance/full/main -d '{"jsonrpc":"2.0","method":"chain_geeth_blockNumbertBlock","params":[],"id":1}'
6
7
8
# token
9
$ curl -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' https://apis.ankr.com/xxxxx/xxxxx/binance/full/main
Copied!
1
package main
2
3
import (
4
"context"
5
"fmt"
6
"github.com/ethereum/go-ethereum/ethclient"
7
)
8
9
10
func main() {
11
const url_basic = "https://username:[email protected]/xxxxx/xxxxx/binance/full/main" // basic authentication
12
const url_token = "https://apis.ankr.com/xxxxx/xxxxx/binance/full/main" // token
13
14
rpcClient,err := ethclient.Dial("choose url_basic or url_token by your created type")
15
16
if err != nil {
17
panic(err)
18
}
19
20
blockNumber, err := rpcClient.BlockNumber(context.Background())
21
22
if err != nil {
23
panic(err)
24
}
25
26
fmt.Println(blockNumber)
27
}
Copied!
1
const Web3 = require('web3');
2
3
4
let url_basic = 'https://username:[email protected]/xxxxx/xxxxx/binance/full/main' // basic authentication
5
let url_token = 'https://apis.ankr.com/xxxxx/xxxxx/binance/full/main' // token
6
7
const web3 = new Web3(new Web3.providers.HttpProvider('choose url_basic or url_token by your created type'));
8
9
web3.eth.getBlockNumber((error, blockNumber) => {
10
if(!error){
11
console.log(blockNumber);
12
}else{
13
console.log(error);
14
}
15
})
16
Copied!
1
from web3 import Web3
2
3
4
def test_block_number(self):
5
url_basic = 'https://username:[email protected]/xxxxx/xxxxx/binance/full/main' # basic authentication
6
url_token = 'https://apis.ankr.com/xxxxx/xxxxx/binance/full/main' # token
7
8
web3 = Web3(HTTPProvider('choose url_basic or url_token by your created type'))
9
print(web3.eth.block_number)
Copied!
The easiest way to test out your WebSocket endpoint is to install a command line tool such as wscat
Use wscat to send Curl requests as in the Curl example below

Websocket Endpoint Examples

Curl (wscat)
Go
Javascript (Ethers)
Javascript (WS)
Python
Untitled
1
# basic authentication
2
$ wscat -c wss://username:[email protected]/wss/xxxxx/xxxxx/binance/full/main
3
4
# token
5
$ wscat -c wss://apis.ankr.com/wss/xxxxx/xxxxx/binance/full/main
6
7
wait connected...
8
9
# subscribe
10
> {"jsonrpc": "2.0", "id": 1, "method": "eth_subscribe", "params": ["newHeads"]}
11
12
# unsubscribe
13
> {"jsonrpc": "2.0", "id": 2, "method": "eth_unsubscribe", "params": ["The result value returned after successful subscription"]}
Copied!
1
package main
2
3
import (
4
"context"
5
"fmt"
6
"github.com/ethereum/go-ethereum/core/types"
7
"github.com/ethereum/go-ethereum/ethclient"
8
"time"
9
)
10
11
12
func main() {
13
const url_basic = "wss://username:[email protected]/wss/xxxxx/xxxxx/binance/full/main" // basic authentication
14
const url_token = "wss://apis.ankr.com/wss/xxxxx/xxxxx/binance/full/main" // token
15
16
client, err := ethclient.Dial(`choose url_basic or url_token by your created type`)
17
18
if err != nil {
19
panic(err)
20
}
21
22
ch := make(chan *types.Header, 1024)
23
sub, err := client.SubscribeNewHead(context.Background(), ch)
24
25
if err != nil {
26
panic(err)
27
}
28
29
fmt.Println("---subscribe-----")
30
31
go func() {
32
time.Sleep(10 * time.Second)
33
fmt.Println("---unsubscribe-----")
34
sub.Unsubscribe()
35
}()
36
37
go func() {
38
for c := range ch {
39
fmt.Println(c.Number)
40
}
41
}()
42
43
<-sub.Err()
44
45
}
Copied!
1
const ethers = require("ethers");
2
3
const url_basic = 'wss://username:[email protected]/wss/xxxxx/xxxxx/binance/full/main' // basic authentication
4
const url_token = 'wss://apis.ankr.com/wss/xxxxx/xxxxx/binance/full/main' // token
5
6
const init = function () {
7
8
const wsProvider = new ethers.providers.WebSocketProvider('choose url_basic or url_token by your created type');
9
10
wsProvider.on("pending", (tx) => {
11
console.log("tx", tx)
12
setTimeout(function () {
13
wsProvider.destroy()
14
}, 1000);
15
16
});
17
18
};
19
20
init();
Copied!
1
const WebSocket = require('ws');
2
3
const url_basic = 'wss://username:[email protected]/wss/xxxxx/xxxxx/binance/full/main' // basic authentication
4
const url_token = 'wss://[email protected]/wss/xxxxx/xxxxx/binance/full/main' // token
5
6
const request = '{"jsonrpc": "2.0", "id": 1, "method": "eth_subscribe", "params": ["newPendingTransactions"]}';
7
8
const ws = new WebSocket('choose url_basic or url_token by your created type');
9
10
ws.on('open', function open() {
11
ws.send(request);
12
});
13
14
let count = 0;
15
16
ws.on('message', function incoming(data) {
17
const res = JSON.parse(data)
18
console.log(JSON.stringify(res));
19
if (++count === 5) {
20
process.exit(0);
21
}
22
});
Copied!
1
import asyncio
2
import json
3
import time
4
5
import websockets
6
7
8
def create_req_body(r_id, method, params):
9
data = {
10
"jsonrpc": "2.0",
11
"id": r_id,
12
"method": method,
13
"params": params
14
}
15
return json.dumps(data)
16
17
18
async def do_wss(url):
19
req_json = create_req_body(1, "eth_subscribe", ["newHeads"])
20
async with websockets.connect(url) as wss:
21
await wss.send(req_json)
22
subscription_response = await wss.recv()
23
print(subscription_response)
24
subscribe_result = json.loads(subscription_response)['result']
25
req_json = create_req_body(2, "eth_unsubscribe", [subscribe_result])
26
27
start_time = int(time.time())
28
end_time = start_time + 5
29
while end_time > int(time.time()):
30
recv_data = await wss.recv()
31
print(recv_data)
32
print('\n')
33
await wss.send(req_json)
34
unsubscription_response = await wss.recv()
35
print(unsubscription_response)
36
37
38
class TestWSS:
39
40
def test_wss(self):
41
url_basic = 'wss://username:[email protected]/wss/xxxxx/xxxxx/binance/full/main' # basic authentication
42
url_token = 'wss://apis.ankr.com/wss/xxxxx/xxxxx/binance/full/main' # token
43
asyncio.get_event_loop().run_until_complete(do_wss('choose url_basic or url_token by your created type'))
Copied!
1
Copied!
Last modified 30d ago