Go SDK for Hyperliquid. Supports REST API and WebSocket connections for perpetuals and spot trading.
- REST API and WebSocket support
- Perpetuals and spot trading
- Type-safe responses with error handling
- Auto-reconnection for WebSocket
- Rate limiting (1200 requests/minute)
go get github.com/sxlgg/hyperliquid-sdk-gopackage main
import (
"fmt"
"log"
"github.com/sxlgg/hyperliquid-sdk-go/hyperliquid/api"
"github.com/sxlgg/hyperliquid-sdk-go/hyperliquid/api/info/perpetuals"
)
func main() {
client := hyperliquid.NewClient()
// Get perpetuals metadata
meta, err := perpetuals.GetMeta(client, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Assets: %d\n", len(meta.Universe))
// Get user account state
state, err := perpetuals.GetClearinghouseState(client, "0x...", nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Account Value: %s\n", state.MarginSummary.AccountValue)
}package main
import (
"encoding/json"
"fmt"
"log"
"github.com/sxlgg/hyperliquid-sdk-go/hyperliquid/websocket"
)
func main() {
client := hyperliquid_websocket.NewClient()
if err := client.Connect(); err != nil {
log.Fatal(err)
}
defer client.Disconnect()
// Handle price updates
client.SetHandler("allMids", func(data json.RawMessage) error {
var mids hyperliquid_websocket.AllMids
if err := json.Unmarshal(data, &mids); err != nil {
return err
}
for coin, price := range mids.Mids {
fmt.Printf("%s: $%s\n", coin, price)
}
return nil
})
// Subscribe to all mids
subscription := hyperliquid_websocket.NewAllMidsSubscription(nil)
if err := client.Subscribe(subscription); err != nil {
log.Fatal(err)
}
select {} // Keep running
}package main
import (
"log"
"github.com/sxlgg/hyperliquid-sdk-go/hyperliquid/api"
"github.com/sxlgg/hyperliquid-sdk-go/hyperliquid/api/exchange"
)
func main() {
client := hyperliquid.NewClient()
// Create limit order
order := exchange.CreateLimitOrder(
0, // ETH-USD
true, // Buy
"2000", // Price
"0.1", // Size
false, // Reduce only
"Gtc", // Time in force
nil, // Client order ID
)
// Place order (signature generation required)
signature := exchange.Signature{R: "0x...", S: "0x...", V: 27}
response, err := exchange.PlaceOrder(client, []exchange.Order{order}, "na", nil, 123456, signature, nil, nil)
if err != nil {
log.Fatal(err)
}
log.Printf("Order placed: %+v", response)
}Perpetuals (hyperliquid/api/info/perpetuals)
GetPerpDexs()- Get perpetual DEXsGetMeta()- Perpetuals metadataGetMetaAndAssetCtxs()- Metadata with asset contextsGetClearinghouseState()- Account stateGetUserFunding()- Funding historyGetUserNonFundingLedgerUpdates()- Non-funding ledger updatesGetFundingHistory()- Funding ratesGetPredictedFundings()- Predicted fundingGetPerpsAtOpenInterestCap()- Perps at open interest capGetPerpDeployAuctionStatus()- Perp deploy auction status
Spot (hyperliquid/api/info/spot)
GetSpotMeta()- Spot metadataGetSpotMetaAndAssetCtxs()- Spot metadata with contextsGetSpotClearinghouseState()- Token balancesGetSpotDeployState()- Spot deploy auction stateGetTokenDetails()- Token information
Orders (hyperliquid/api/exchange)
PlaceOrder()- Place ordersCancelOrders()- Cancel ordersCancelOrdersByCloid()- Cancel by client order IDScheduleCancel()- Schedule future cancelModifyOrder()- Modify orderBatchModifyOrders()- Batch modify
TWAP Orders
PlaceTwapOrder()- Place TWAP orderCancelTwapOrder()- Cancel TWAP order
Account
UpdateLeverage()- Update leverageUpdateIsolatedMargin()- Update marginUsdSend()- Send USDCSpotSend()- Send spot tokensWithdraw()- Withdraw funds
Utilities
ReserveRequestWeight()- Reserve API weight
Market Data
AllMidsSubscription- All asset pricesL2BookSubscription- Order bookTradesSubscription- Trade updatesCandleSubscription- Candlestick dataBboSubscription- Best bid/offerActiveAssetCtxSubscription- Active asset context
User Data
NotificationSubscription- User notificationsWebData2Subscription- User web dataOrderUpdatesSubscription- Order statusUserEventsSubscription- User eventsUserFillsSubscription- User fillsUserFundingsSubscription- Funding paymentsUserNonFundingLedgerUpdatesSubscription- Non-funding updatesActiveAssetDataSubscription- Active asset data
TWAP
UserTwapSliceFillsSubscription- TWAP slice fillsUserTwapHistorySubscription- TWAP history
// Custom HTTP client
config := hyperliquid.DefaultConfig().
WithBaseURL("https://api.hyperliquid.xyz").
WithTimeout(30 * time.Second)
client := hyperliquid.NewClientWithConfig(config)
// WebSocket options
wsClient := hyperliquid_websocket.NewClient(
hyperliquid_websocket.WithURL("wss://api.hyperliquid.xyz/ws"),
hyperliquid_websocket.WithReconnect(true),
)meta, err := perpetuals.GetMeta(client, nil)
if err != nil {
if perpErr, ok := err.(*perpetuals.PerpError); ok {
switch perpErr.Code {
case perpetuals.ErrCodeInvalidAddress:
// Handle invalid address
case perpetuals.ErrCodeRequestFailed:
// Handle request failure
}
}
}# Install dependencies
go mod tidy
# Run tests
go test ./...
# Run with coverage
go test -cover ./...- Replace
net/httpwithfasthttpfor better performance - Connection pooling for WebSocket reconnections
- Batch request optimization for high-frequency trading
- Optional proxy client to bypass 1200 requests/minute
MIT License - see LICENSE file.
Not officially affiliated with Hyperliquid. Use at your own risk.