> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/blockchain/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/blockchain/bitfinex-api-shi-yong.md).

# Bitfinex API 使用

## Bitfinex API 使用

記得要先過Bitfinex KYC然後去申請一個API金鑰，並選擇適當權限。

![](https://3415061728-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DA5jITnh37VYXmj%2F-M0u4EflenDYR7clyEKz%2F-M0u4SoY6ius1UklUEq9%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202019-08-02%20%E4%B8%8A%E5%8D%8811.56.17.png?generation=1582596241640867\&alt=media)

## API 文件

<https://docs.bitfinex.com/v2/docs/getting-started>

## Websocket 監聽 BTC/USD 掛單

```javascript
const ws = require('ws')
const w = new ws('wss://api.bitfinex.com/ws/2')
const crypto = require('crypto-js')

// 以下填入自己的金鑰資訊
const apiKey = ''
const apiSecret = ''

const authNonce = Date.now() * 1000
const authPayload = 'AUTH' + authNonce
const authSig = crypto
  .HmacSHA384(authPayload, apiSecret)
  .toString(crypto.enc.Hex)

const payload = {
  apiKey,
  authSig,
  authNonce,
  authPayload,
  event: 'auth'
}

w.on('message', (msg) => {
  console.log(msg)
})

let msg = JSON.stringify({
  event: 'subscribe',
  channel: 'ticker',
  symbol: 'tBTCUSD'
})

w.on('open', () => {
  w.send(JSON.stringify(payload))
  w.send(msg)
})
```

## 注意事項

1. API 每分鐘不可超過請求次數 <https://docs.bitfinex.com/v2/docs/getting-started#section-rate-limits>
2. 不可讓多個 server 使用同一個API KEY呼叫 <https://docs.bitfinex.com/v2/docs/getting-started#section-multiple-clients>
3. Symbol 是什麼？ <https://docs.bitfinex.com/v2/docs/getting-started#section-what-is-a-symbol->
