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
@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
@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:
Positional arguments (list of values):
- Mapped to the endpoint's required parameters in order
- Example:
command(:info, :l2_book, ["BTC"])callsL2Book.request("BTC")
Keyword options (keyword list):
- Passed directly to the endpoint's
request/1function - Example:
command(:info, :all_mids, dex: "hypurr")callsAllMids.request(dex: "hypurr")
- Passed directly to the endpoint's
No arguments (empty list or omitted):
- Calls the endpoint with no parameters
- Example:
command(:info, :all_mids)callsAllMids.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
@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
Hyperliquid.Error- On endpoint errorsRuntimeError- On resolution errors (invalid context, endpoint not found)