Skip to content

infoway-api/infoway-sdk-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Infoway SDK for Node.js / TypeScript

npm version Node.js License: MIT

English | 中文

Official Node.js/TypeScript SDK for the Infoway real-time financial data API.

API Documentation | Get API Key

Get your free API key at infoway.io -- 7-day free trial

Installation

npm install infoway-sdk
# or
yarn add infoway-sdk
# or
pnpm add infoway-sdk

Requirements: Node.js >= 18.0.0

Quick Start

REST API

import { InfowayClient, KlineType } from "infoway-sdk";

const client = new InfowayClient({ apiKey: "YOUR_API_KEY" });

// Stock trade data
const trades = await client.stock.getTrade("AAPL.US");
console.log(trades);

// Multiple symbols
const multiTrades = await client.stock.getTrade("AAPL.US,TSLA.US,GOOGL.US");

// Order book depth
const depth = await client.stock.getDepth("AAPL.US");

// K-line data
const klines = await client.stock.getKline("AAPL.US", KlineType.DAY, 100);

// Crypto data
const btc = await client.crypto.getTrade("BTCUSDT");
const ethKline = await client.crypto.getKline("ETHUSDT", KlineType.HOUR_1, 50);

// Market temperature
const temp = await client.market.getTemperature("HK,US");

// Market breadth
const breadth = await client.market.getBreadth("US");

// Stock fundamentals
const valuation = await client.stockInfo.getValuation("AAPL.US");
const ratings = await client.stockInfo.getRatings("AAPL.US");

// Plate/sector data
const industries = await client.plate.getIndustry("HK");
const concepts = await client.plate.getConcept("HK");

// Basic info
const symbols = await client.basic.getSymbols("US");
const tradingDays = await client.basic.getTradingDays("US");

Environment Variable

You can set INFOWAY_API_KEY instead of passing it directly:

export INFOWAY_API_KEY=your_api_key
const client = new InfowayClient(); // reads from INFOWAY_API_KEY

WebSocket Real-time Data

import { InfowayWebSocket, Business } from "infoway-sdk";

const ws = new InfowayWebSocket({
  apiKey: "YOUR_API_KEY",
  business: Business.STOCK,
});

ws.onTrade = (msg) => {
  console.log("Trade:", msg);
};

ws.onDepth = (msg) => {
  console.log("Depth:", msg);
};

ws.onKline = (msg) => {
  console.log("Kline:", msg);
};

ws.onDisconnect = () => {
  console.log("Disconnected, reconnecting...");
};

ws.onReconnect = () => {
  console.log("Reconnected!");
};

// Subscribe after connection is established
await ws.subscribeTrade("AAPL.US,TSLA.US");
await ws.subscribeDepth("AAPL.US");

// Start receiving data
await ws.connect();

// To close:
// await ws.close();

Crypto WebSocket

const ws = new InfowayWebSocket({
  apiKey: "YOUR_API_KEY",
  business: Business.CRYPTO,
});

ws.onTrade = (msg) => console.log("Crypto trade:", msg);
await ws.subscribeTrade("BTCUSDT,ETHUSDT");
await ws.connect();

API Reference

REST Clients

Client Prefix Description
client.stock stock HK, US, CN stock market data
client.crypto crypto Cryptocurrency data
client.japan japan Japan stock market data
client.india india India stock market data
client.common common Common market data
client.basic -- Symbols, trading days, hours
client.market -- Temperature, breadth, indexes
client.plate -- Industry/concept sectors
client.stockInfo -- Valuation, ratings, company info

Market Data Methods (stock/crypto/japan/india/common)

Method HTTP Endpoint
getTrade(codes) GET /{prefix}/batch_trade/{codes}
getDepth(codes) GET /{prefix}/batch_depth/{codes}
getKline(codes, klineType, count) POST /{prefix}/v2/batch_kline

KlineType Enum

Value Interval
KlineType.MIN_1 (1) 1 minute
KlineType.MIN_5 (2) 5 minutes
KlineType.MIN_15 (3) 15 minutes
KlineType.MIN_30 (4) 30 minutes
KlineType.HOUR_1 (5) 1 hour
KlineType.HOUR_2 (6) 2 hours
KlineType.HOUR_4 (7) 4 hours
KlineType.DAY (8) 1 day
KlineType.WEEK (9) 1 week
KlineType.MONTH (10) 1 month
KlineType.QUARTER (11) 1 quarter
KlineType.YEAR (12) 1 year

WebSocket Codes

Client → server:

Code Name Description
10000 SUB_TRADE Subscribe to trade data
10003 SUB_DEPTH Subscribe to depth data
10006 SUB_KLINE Subscribe to K-line data (payload data.arr=[{codes, type}])
10010 HEARTBEAT Heartbeat keepalive
11000 UNSUB_TRADE Unsubscribe trade data
11001 UNSUB_DEPTH Unsubscribe depth data
11002 UNSUB_KLINE Unsubscribe K-line data

Server → client:

Code Name Description
10001 SUB_TRADE_ACK Trade subscribe acknowledgement
10002 PUSH_TRADE Real-time trade push
10004 SUB_DEPTH_ACK Depth subscribe acknowledgement
10005 PUSH_DEPTH Real-time depth push
10007 SUB_KLINE_ACK K-line subscribe acknowledgement
10008 PUSH_KLINE Real-time K-line push
11010 UNSUB_ACK Unsubscribe acknowledgement

Error Handling

import { InfowayAPIError, InfowayAuthError, InfowayTimeoutError } from "infoway-sdk";

try {
  const data = await client.stock.getTrade("AAPL.US");
} catch (err) {
  if (err instanceof InfowayAuthError) {
    console.error("Authentication failed. Check your API key.");
  } else if (err instanceof InfowayTimeoutError) {
    console.error("Request timed out.");
  } else if (err instanceof InfowayAPIError) {
    console.error(`API error [${err.ret}]: ${err.msg}`);
  }
}

License

MIT

About

Official Node.js/TypeScript SDK for Infoway financial data API

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors