View Source Hyperliquid.Api (hyperliquid v0.2.2)

Root API module providing unified access to all Hyperliquid endpoints.

This module offers a Redix-style command interface for dynamic endpoint invocation, along with direct access to context-specific modules.

Command Interface

The command/3 function provides dynamic endpoint invocation:

# Simple endpoint (no params)
{:ok, mids} = Hyperliquid.Api.command(:info, :all_mids)

# With positional arguments
{:ok, book} = Hyperliquid.Api.command(:info, :l2_book, ["BTC"])
{:ok, state} = Hyperliquid.Api.command(:info, :clearinghouse_state, ["0xabc..."])

# With keyword options
{:ok, mids} = Hyperliquid.Api.command(:info, :all_mids, dex: "hypurr")

Context Modules

For a more structured API with autocomplete and type safety, use context modules:

# Info endpoints
Hyperliquid.Api.Info.all_mids()
Hyperliquid.Api.Info.l2_book("BTC")
Hyperliquid.Api.Info.clearinghouse_state("0xabc...")

# Exchange endpoints (when migrated to DSL)
Hyperliquid.Api.Exchange.order(...)

Direct Endpoint Access

The original endpoint modules are still available and unchanged:

Hyperliquid.Api.Info.AllMids.request()
Hyperliquid.Api.Info.L2Book.request("BTC")

Discovering Endpoints

Use the Registry to discover available endpoints:

# List all endpoints
Hyperliquid.Api.Registry.list_endpoints()

# List by type
Hyperliquid.Api.Registry.list_by_type(:info)

# Get endpoint metadata
Hyperliquid.Api.Registry.get_endpoint_info("allMids")

Summary

Types

Arguments for endpoint invocation.

Supported API contexts.

Endpoint name in snake_case.

Functions

Execute an API endpoint command.

Execute an API endpoint command, raising on error.

Types

@type args() :: list()

Arguments for endpoint invocation.

Can be either:

  • List of positional arguments: ["BTC"], ["0xabc..."]
  • Keyword list of options: [dex: "hypurr"], [nSigFigs: 5]
  • Empty list for parameterless endpoints: []
@type context() :: :info | :exchange | :explorer | :stats

Supported API contexts.

@type endpoint_name() :: atom()

Endpoint name in snake_case.

Functions

Link to this function

command(context, endpoint_name, args \\ [])

View Source
@spec command(context(), endpoint_name(), args()) :: {:ok, term()} | {:error, term()}

Execute an API endpoint command.

This provides a Redix-style interface for dynamic endpoint invocation.

Parameters

  • context - The API context (:info, :exchange, :explorer, :stats)
  • endpoint - The endpoint name in snake_case (:all_mids, :l2_book, etc.)
  • args - Arguments to pass to the endpoint (default: [])

Argument Handling

Arguments can be provided in two formats:

  1. Positional arguments (list of values):

    • Mapped to the endpoint's required parameters in order
    • Example: command(:info, :l2_book, ["BTC"]) calls L2Book.request("BTC")
  2. Keyword options (keyword list):

    • Passed directly to the endpoint's request/1 function
    • Example: command(:info, :all_mids, dex: "hypurr") calls AllMids.request(dex: "hypurr")
  3. No arguments (empty list or omitted):

    • Calls the endpoint with no parameters
    • Example: command(:info, :all_mids) calls AllMids.request()

Returns

  • {:ok, result} - Successful endpoint call
  • {:error, reason} - Error from endpoint or resolution failure

Examples

# Parameterless endpoints
{:ok, mids} = Hyperliquid.Api.command(:info, :all_mids)
{:ok, meta} = Hyperliquid.Api.command(:info, :meta)

# Endpoints with required parameters
{:ok, book} = Hyperliquid.Api.command(:info, :l2_book, ["BTC"])
{:ok, state} = Hyperliquid.Api.command(:info, :clearinghouse_state, ["0xabc..."])

# Endpoints with optional parameters
{:ok, mids} = Hyperliquid.Api.command(:info, :all_mids, dex: "hypurr")
{:ok, book} = Hyperliquid.Api.command(:info, :l2_book, ["BTC"], nSigFigs: 5)

# Error handling
case Hyperliquid.Api.command(:info, :nonexistent) do
  {:ok, result} -> IO.inspect(result)
  {:error, :not_found} -> IO.puts("Endpoint not found")
  {:error, reason} -> IO.puts("Error: #{inspect(reason)}")
end
Link to this function

command!(context, endpoint_name, args \\ [])

View Source
@spec command!(context(), endpoint_name(), args()) :: term()

Execute an API endpoint command, raising on error.

Same as command/3 but raises on error instead of returning an error tuple.

Examples

mids = Hyperliquid.Api.command!(:info, :all_mids)
book = Hyperliquid.Api.command!(:info, :l2_book, ["BTC"])
state = Hyperliquid.Api.command!(:info, :clearinghouse_state, ["0xabc..."])

Raises