🏠 Getting StartedAPI Token Usage Guide

🔑 API Token Usage Guide

Each data type requires the use of corresponding dedicated Tokens for authentication and data access


📋 Token Classification

Data TypeToken TypeScope
StocksStock TokenStock K-line, latest quotes, latest trading data
CryptocurrenciesCrypto TokenCrypto K-line, latest trading data
ForexForex TokenForex K-line, latest quote data
IndicesIndex TokenIndex K-line, latest quotes, latest trading data

🔐 Token Usage Rules

One-to-One Usage Principle

Each data type requires the use of corresponding dedicated Token, cannot be used across types:

  • ✅ Stock data → Use Stock Token

  • ✅ Cryptocurrency data → Use Crypto Token

  • ✅ Forex data → Use Forex Token

  • ✅ Index data → Use Index Token

  • ❌ Stock data → Use Crypto Token (Invalid)

  • ❌ Forex data → Use Stock Token (Invalid)


📡 API Usage Scenarios

REST API Token Usage

Stock API Example

# Stock K-line data
curl -X GET "https://api.dataapi.io/v1/stock/kline?ticker=AAPL&klineType=1d&token=STOCK_TOKEN"
 
# Stock latest quote
curl -X GET "https://api.dataapi.io/v1/stock/latest_quote?ticker=AAPL&token=STOCK_TOKEN"

Cryptocurrency API Example

# Cryptocurrency K-line data
curl -X GET "https://api.dataapi.io/v1/crypto/kline?ticker=BTCUSDT&klineType=1d&token=CRYPTO_TOKEN"
 
# Cryptocurrency latest trade
curl -X GET "https://api.dataapi.io/v1/crypto/latest_trade?cryptoTicker=BTCUSDT&token=CRYPTO_TOKEN"

Forex API Example

# Forex K-line data
curl -X GET "https://api.dataapi.io/v1/fx/kline?ticker=GBP/USD&klineType=1d&token=FOREX_TOKEN"
 
# Forex latest quote
curl -X GET "https://api.dataapi.io/v1/fx/latest_quote?ticker=GBP/USD&token=FOREX_TOKEN"

Index API Example

# Index K-line data
curl -X GET "https://api.dataapi.io/v1/indices/kline?ticker=SPX&klineType=1d&token=INDEX_TOKEN"
 
# Index latest quote
curl -X GET "https://api.dataapi.io/v1/indices/latest_quote?ticker=SPX&token=INDEX_TOKEN"

WebSocket API Token Usage

Stock WebSocket Example

// Connect to Stock WebSocket
const ws = new WebSocket('wss://api.dataapi.io/v1/stock');
 
ws.onopen = function() {
  // Use Stock Token for authorization
  ws.send(JSON.stringify({
    action: "auth",
    params: {
      token: "STOCK_TOKEN"  // Must use dedicated Stock Token
    }
  }));
};

Other Data Types WebSocket

// Cryptocurrency WebSocket (Assumed)
// token: "CRYPTO_TOKEN"
 
// Forex WebSocket (Assumed)  
// token: "FOREX_TOKEN"
 
// Index WebSocket (Assumed)
// token: "INDEX_TOKEN"

⚠️ Important Notes

Token Security

  • 🔒 Confidentiality: Token is sensitive information, please keep it secure
  • 🔄 Regular Updates: Recommend regularly updating Tokens for security
  • 📝 Environment Variables: Use environment variables to store Tokens in code

Error Handling

Common error responses when using wrong Token type:

{
  "code": 401,
  "status": "error", 
  "message": "Invalid API token"
}
{
  "code": 403,
  "status": "error",
  "message": "Token type mismatch, please use the corresponding data type Token"
}

🚀 Best Practices

Code Example

// Recommended: Use environment variables to manage different types of Tokens
const TOKENS = {
  stock: process.env.STOCK_API_TOKEN,
  crypto: process.env.CRYPTO_API_TOKEN, 
  forex: process.env.FOREX_API_TOKEN,
  index: process.env.INDEX_API_TOKEN
};
 
// Select corresponding Token based on data type
function getToken(dataType) {
  return TOKENS[dataType];
}
 
// Usage example
const stockData = await fetch(`https://api.dataapi.io/v1/stock/kline?ticker=AAPL&token=${getToken('stock')}`);
const cryptoData = await fetch(`https://api.dataapi.io/v1/crypto/kline?ticker=BTCUSDT&token=${getToken('crypto')}`);