Bitfinex API Go Library

repository·master·Indexed 43 days ago

https://github.com/bitfinexcom/bitfinex-api-go

A Golang reference implementation of the Bitfinex API, supporting REST (V1/V2) and Websocket interactions for trading Bitcoin, Ethereum, Ripple, and other assets. It includes services for managing orders, positions, wallets, and retrieving real-time ticker and candle data.

Tokens
10.1K
Snippets
43
Records
65
Agent score
52%

What's inside Bitfinex API Go

  1. Manage Authenticated Sessions

    master

    To perform private operations (like submitting orders), you must authenticate your connection.

    1. Use Credentials(key, secret) to assign your API credentials to a connection request.
    2. Use GetAuthenticatedSocket() to retrieve the socket used for authenticated requests. Note that due to rate limits, only one authenticated socket can be active at a time.
    3. You can check the current authentication status via client.Authentication (of type AuthState).

    Authentication states:

    • NoAuthentication (0)
    • PendingAuthentication (1)
    • SuccessfulAuthentication (2)
    • RejectedAuthentication (3)
  2. Verify data integrity using sequence numbers

    master

    If sequencing is enabled on the V2 Websocket API, every incoming packet includes:

    • Public sequence number: Increments on every packet.
    • Auth sequence number: Increments on every authenticated action (e.g., new orders). This is present in channel 0 packets.

    Because these values increase monotonically, you can use them to detect if any packets were missed or dropped during transmission.

  3. Handling Websocket order packets (`on`, `oc`, `ou`)

    master

    When monitoring orders via Websocket, the sequence of packets depends on how the order is filled:

    • on (Order New): Sent when an order is added to the book or partially filled. If an order is partially filled immediately, you will receive an on packet with a status like PARTIALLY FILLED....
    • ou (Order Update): Sent during updates (e.g., after a partial fill).
    • oc (Order Closed): Sent when an order is completed/closed.

    Note: If an order fills immediately upon submission, you may skip the on packet entirely and receive only an oc packet.

  4. Difference between `te` and `tu` trade messages

    master

    When receiving trade updates via Websocket:

    • te (Trade Executed): Sent immediately after a trade is matched and executed. Use this for real-time feeds.
    • tu (Trade Updated): Sent once the trade has completed processing. This message may be delayed during high load periods.
  5. Understand Bitfinex API rate limits

    master

    Websocket Limits

    • Connections: An account can create a maximum of 15 new connections every 5 minutes.
    • Channels: Each connection can subscribe to a maximum of 30 inbound data channels.
    • Multiplexing: This library automatically handles load balancing and multiplexing for channels, creating/destroying connections as needed to manage these limits.

    REST Limits

    • Base Limit: 1,000 orders per 5-minute interval (shared across all account API connections).
    • Scaling: The limit increases based on your 30-day USD trading volume using the formula: 1000 + (TOTAL_PAIRS_PLATFORM * 60 * 5) / (250000000 / USER_VOL_LAST_30d)
  6. Quickstart: Create an order via REST

    master

    To use the library, initialize a new client and provide your API credentials using .Credentials("API_KEY", "API_SEC"). You can then access various API services, such as Orders, to perform actions like submitting a new order.

    package main
    
    import (
        "fmt"
        "github.com/bitfinexcom/bitfinex-api-go/v2"
    )
    
    func main() {
        client := bitfinex.NewClient().Credentials("API_KEY", "API_SEC")
    	
        // create order
        response, err := client.Orders.SubmitOrder(&order.NewRequest{
            Symbol: "tBTCUSD",
            CID:    time.Now().Unix() / 1000,
            Amount: 0.02,
            Type: 1, // Note: Ensure Type matches the expected order.Type constant/enum
            Price:  5000,
        })
        if err != nil {
            panic(err)
        }
    }
  7. Initialize the Bitfinex Client

    master

    To interact with the Bitfinex API, you must first create a Client instance using NewClient(). The client manages all communication and provides access to various services (e.g., Orders, Ticker, Account). For requests requiring authentication, use the Auth(key, secret) method to set your API credentials.

    client := bitfinex.NewClient()
    client.Auth("YOUR_API_KEY", "YOUR_API_SECRET")