RestClient
認証が必要なプライベート REST API クライアントです。https://api.bitbank.cc/v1 に接続します。
インスタンス生成
import { RestClient } from '@pokooo/bb-api';
const client = new RestClient({
key: process.env.BITBANK_API_KEY!,
secret: process.env.BITBANK_API_SECRET!,
});
API キーはソースコードに直接書かず、必ず環境変数を使用してください。
コンストラクタオプション
| オプション | 型 | デフォルト | 説明 |
|---|---|---|---|
key | string | — | API キー |
secret | string | — | API シークレット |
authMethod | 'time-window' | 'nonce' | 'time-window' | 署名方式('nonce' はレガシー) |
timeWindow | number | 5000 | タイムウィンドウ (ms)(time-window モード) |
throwExceptions | boolean | false | REST レスポンス失敗時に例外をスロー |
strict_param_validation | boolean | false | 署名前に未知のパラメータでエラー |
customSignMessageFn | (msg, secret) => Promise<string> | — | カスタム署名関数(Web Crypto 非対応環境向け) |
メソッド一覧
資産
getAssets()
保有資産の一覧を取得します。
const res = await client.getAssets();
// res.data.assets: Asset[]
注文
submitOrder(params)
注文を発注します。
import { PAIR, ORDER_SIDE, ORDER_TYPE } from '@pokooo/bb-api';
// 成行買い
const res = await client.submitOrder({
pair: PAIR.BTC_JPY,
amount: '0.001',
side: ORDER_SIDE.BUY,
type: ORDER_TYPE.MARKET,
});
// 指値売り
const res = await client.submitOrder({
pair: PAIR.BTC_JPY,
amount: '0.001',
price: '5000000',
side: ORDER_SIDE.SELL,
type: ORDER_TYPE.LIMIT,
});
// ストップ指値買い
const res = await client.submitOrder({
pair: PAIR.BTC_JPY,
amount: '0.001',
price: '5100000',
trigger_price: '5000000',
side: ORDER_SIDE.BUY,
type: ORDER_TYPE.STOP_LIMIT,
});
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
pair | PairString | Yes | 取引ペア |
amount | string | No | 注文数量 |
price | string | No | 指値価格(指値注文時) |
side | OrderSide | Yes | 'buy' または 'sell' |
position_side | PositionSide | No | 'long' または 'short'(証拠金取引) |
type | OrderType | Yes | 注文種別(下記参照) |
post_only | boolean | No | Post Only 注文 |
trigger_price | string | No | トリガー価格(ストップ注文時) |
ORDER_TYPE 定数:
| 定数 | 値 | 説明 |
|---|---|---|
ORDER_TYPE.LIMIT | 'limit' | 指値 |
ORDER_TYPE.MARKET | 'market' | 成行 |
ORDER_TYPE.STOP | 'stop' | ストップ成行 |
ORDER_TYPE.STOP_LIMIT | 'stop_limit' | ストップ指値 |
ORDER_TYPE.TAKE_PROFIT | 'take_profit' | 利食い |
ORDER_TYPE.STOP_LOSS | 'stop_loss' | 損切り |
ORDER_TYPE.LOSSCUT | 'losscut' | ロスカット(システム) |
ORDER_SIDE 定数:
| 定数 | 値 |
|---|---|
ORDER_SIDE.BUY | 'buy' |
ORDER_SIDE.SELL | 'sell' |
getOrder(params)
注文情報を取得します。
const res = await client.getOrder({
pair: PAIR.BTC_JPY,
order_id: 12345678,
});
// res.data: OrderResponse
cancelOrder(params)
注文をキャンセルします。
const res = await client.cancelOrder({
pair: PAIR.BTC_JPY,
order_id: 12345678,
});
cancelOrders(params)
複数の注文を一括キャンセルします。
const res = await client.cancelOrders({
pair: PAIR.BTC_JPY,
order_ids: [12345678, 12345679],
});
getOrdersInfo(params)
複数の注文情報を取得します。
const res = await client.getOrdersInfo({
pair: PAIR.BTC_JPY,
order_ids: [12345678, 12345679],
});
getActiveOrders(params?)
有効注文一覧を取得します。
// 全ペアの有効注文
const res = await client.getActiveOrders();
// BTC_JPY の有効注文(最新 10 件)
const res = await client.getActiveOrders({
pair: PAIR.BTC_JPY,
count: 10,
});
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
pair | PairString | No | 取引ペアで絞り込み |
count | number | No | 取得件数 |
from_id | number | No | 開始 order_id |
end_id | number | No | 終了 order_id |
since | number | No | 開始時刻 (Unix ms) |
end | number | No | 終了時刻 (Unix ms) |
取引履歴
getTradeHistory(params?)
約定履歴を取得します。
const res = await client.getTradeHistory({
pair: PAIR.BTC_JPY,
count: 20,
order: 'desc',
});
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
pair | PairString | No | 取引ペアで絞り込み |
count | number | No | 取得件数 |
order_id | number | No | 注文 ID で絞り込み |
since | number | No | 開始時刻 (Unix ms) |
end | number | No | 終了時刻 (Unix ms) |
order | 'asc' | 'desc' | No | ソート順 |
証拠金
getMarginStatus()
証拠金ステータスを取得します。
const res = await client.getMarginStatus();
// res.data: MarginStatusResponse
getMarginPositions()
証拠金ポジション一覧を取得します。
const res = await client.getMarginPositions();
// res.data: MarginPositionsResponse
入金
getDepositHistory(params?)
入金履歴を取得します。
const res = await client.getDepositHistory({
asset: 'btc',
count: 10,
});
getUnconfirmedDeposits()
未確認入金一覧を取得します。
const res = await client.getUnconfirmedDeposits();
getDepositOriginators()
入金元情報を取得します。
const res = await client.getDepositOriginators();
confirmDeposits(params)
入金を確認済みにします。
await client.confirmDeposits({
deposits: [
{ uuid: 'xxx', originator_uuid: 'yyy' },
],
});
confirmAllDeposits(params)
指定オリジネーターの全入金を確認済みにします。
await client.confirmAllDeposits({
originator_uuid: 'yyy',
});
出金
getWithdrawalAccount(params)
出金先口座情報を取得します。
const res = await client.getWithdrawalAccount({ asset: 'jpy' });
requestWithdrawal(params)
出金を申請します。
const res = await client.requestWithdrawal({
asset: 'jpy',
uuid: '出金先口座UUID',
amount: '10000',
otp_token: '123456', // Google Authenticator 等
});
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
asset | string | Yes | 通貨(例: 'jpy', 'btc') |
uuid | string | Yes | 出金先口座 UUID |
amount | string | Yes | 出金数量 |
otp_token | string | No | OTP トークン |
sms_token | string | No | SMS トークン |
getWithdrawalHistory(params?)
出金履歴を取得します。
const res = await client.getWithdrawalHistory({
asset: 'jpy',
count: 10,
});
取引所情報
getExchangeStatus()
取引所ステータスを取得します(認証不要だが RestClient に含まれています)。
const res = await client.getExchangeStatus();
// res.data: ExchangeStatusResponse
EXCHANGE_STATUS 定数:
| 定数 | 値 | 説明 |
|---|---|---|
EXCHANGE_STATUS.NORMAL | 'NORMAL' | 通常 |
EXCHANGE_STATUS.BUSY | 'BUSY' | 混雑 |
EXCHANGE_STATUS.VERY_BUSY | 'VERY_BUSY' | 非常に混雑 |
EXCHANGE_STATUS.HALT | 'HALT' | 停止 |
getAllPairsInfo()
全取引ペア情報を取得します。
const res = await client.getAllPairsInfo();
// res.data: AllPairsInfoResponse
プライベートストリーム
getPrivateStreamSubscribeInfo()
PubNub チャンネル・トークンを取得します(PrivateStreamClient が内部で使用)。
const res = await client.getPrivateStreamSubscribeInfo();
// res.data.pubnub_channel, res.data.pubnub_token
エラーハンドリング
デフォルト(throwExceptions: false)
const res = await client.getAssets();
if (res.success !== 1) {
console.error('API error:', res.data);
}
例外モード(throwExceptions: true)
const client = new RestClient({
key: process.env.BITBANK_API_KEY!,
secret: process.env.BITBANK_API_SECRET!,
throwExceptions: true,
});
try {
const res = await client.getAssets();
} catch (e) {
console.error(e);
}
エラーコード定数
import { BITBANK_ERROR_CODE } from '@pokooo/bb-api';
// BITBANK_ERROR_CODE.INVALID_API_KEY => 10000
// BITBANK_ERROR_CODE.INVALID_SIGNATURE => 10002
// BITBANK_ERROR_CODE.ORDER_NOT_FOUND => 50009