Get Bitcoin Price Using Bybit API in Bash

Edited at 2024-04-06 15:17 UTC. With Bybit API v5 [1] it is possible to get public data (price, volume, last trading records) and also place own orders using API Key. All the next examples do not need API Key, because open public data is used here.

First, install jq for read json data and search in it to get only fields we need.

apt install jq

 

Bitcoin price in Spot trading from Bybit API. Ticker BTCUSDT info

This price is from latest finished orders.

echo BTSUSDT `curl -s -L -X GET 'https://api-testnet.bybit.com/v5/market/tickers?category=spot&symbol=BTCUSDT' | jq -r '.result.list[].lastPrice'`

BTSUSDT 61200

 

Bitcoin price from order book: asks and bids

This price is that people are ready to buy or sell.

R='\033[0;31m' G='\033[0;32m' N='\033[0m' d=`curl -s -L -X GET 'https://api-testnet.bybit.com/v5/market/tickers?category=spot&symbol=BTCUSDT' | jq -r '.result.list[]'`; echo -e BTCUSDT ${R}Sell${N}: `echo $d | jq -r '.ask1Price'` "\n"BTCUSDT $G Buy${N}: `echo $d | jq -r '.bid1Price'`

BTCUSDT Sell: 61200 
BTCUSDT  Buy: 61199.99

 

All available trading pairs

curl -s -L -X GET 'https://api-testnet.bybit.com/v5/market/tickers?category=spot' | jq -r '.result.list[].symbol'

DOGEETH
ACHUSDT
ETHWUSDT
PYUSDUSDT
MATICUSDT
MNTUSDT

...

There are 122 pairs now on 2024-04-06 15:18, previously were 266.

 

curl cannot verify certificates on Android

Also, I had a problem getting data via HTTPS on Android with curl:

curl 'https://api-testnet.bybit.com/public/linear/recent-trading-records?symbol=BTCUSDT&limit=1'

curl: (77) error setting certificate verify locations:
  CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs

First, I used option -k for curl does not check certificates. But then I fixed this with [2], need to install ca-certificates:

apt install ca-certificates

 

Links

[1] Bybit API https://bybit-exchange.github.io/docs/api-explorer/v5/market/tickers

[2] Curl error 77 problem with the SSL CA cert https://bobcares.com/blog/curl-error-77-problem-with-the-ssl-ca-cert/

Section
Category