import requests
API_KEY = 'YOUR_API_KEY'
response = requests.get(
'https://api.api-ninjas.com/v1/cryptoprice',
params={'symbol': 'BTCUSDT'},
headers={'X-Api-Key': API_KEY},
)
print(response.json())Prerequisites
- Python 3.7+ installed
- The
requestslibrary (pip install requests) - A free API Ninjas API key
Step 1: Get your API key
Sign up at api-ninjas.com/register.
Step 2: Make the API request
Symbols use the concatenated pair format — for example, BTCUSDT for Bitcoin quoted in Tether, or ETHBTC for Ether priced in Bitcoin.
import requests
API_KEY = 'YOUR_API_KEY'
response = requests.get(
'https://api.api-ninjas.com/v1/cryptoprice',
params={'symbol': 'BTCUSDT'},
headers={'X-Api-Key': API_KEY},
)
print(response.json())Step 3: Use the response
{
"symbol": "BTCUSDT",
"price": "67234.89000000",
"timestamp": 1717603200
}price is returned as a string to preserve precision — cast it to float for display or arithmetic.
Complete example
import os
import requests
API_KEY = os.environ['API_NINJAS_KEY']
URL = 'https://api.api-ninjas.com/v1/cryptoprice'
def get_crypto_price(symbol: str) -> dict:
response = requests.get(
URL,
params={'symbol': symbol},
headers={'X-Api-Key': API_KEY},
timeout=10,
)
response.raise_for_status()
return response.json()
if __name__ == '__main__':
btc = get_crypto_price('BTCUSDT')
eth = get_crypto_price('ETHUSDT')
print(f"BTC/USDT: ${float(btc['price']):>10,.2f}")
print(f"ETH/USDT: ${float(eth['price']):>10,.2f}")Fetching multiple pairs
symbols = ['BTCUSDT', 'ETHUSDT', 'SOLUSDT', 'DOGEUSDT', 'XRPUSDT']
for symbol in symbols:
data = get_crypto_price(symbol)
print(f"{symbol:10} ${float(data['price']):>12,.4f}")Polling for live price updates
For a simple ticker, poll the endpoint on a schedule:
import time
while True:
data = get_crypto_price('BTCUSDT')
print(f"BTC: ${float(data['price']):,.2f}")
time.sleep(60) # Poll once per minuteBe mindful of rate limits — poll every 60 seconds or longer for the free tier. For high-frequency data, use a WebSocket feed from an exchange.
Error handling
from requests.exceptions import HTTPError
try:
data = get_crypto_price('BTCUSDT')
except HTTPError as e:
if e.response.status_code == 400:
print('Invalid symbol — check the trading pair format')
elif e.response.status_code == 401:
print('Invalid or missing API key')
elif e.response.status_code == 429:
print('Rate limit exceeded')Related APIs
Use the Crypto Symbols API to list all supported trading pairs, or the Bitcoin API for Bitcoin-specific data.