getPrice

open suspend fun getPrice(request: ZeroXPriceRequest, zeroXApiKey: String? = null): Result<ZeroXPriceResponse>

Retrieves a price quote without transaction data.

This method fetches a price quote for a swap without generating transaction data. This is useful for:

  • Checking swap prices before committing to a transaction

  • Displaying price information to users

  • Comparing prices across different swap sources

Unlike getQuote(), this method does not return transaction data. The response includes price information, fees breakdown, and liquidity availability.

The ZeroX API key can be configured in two ways:

  1. Portal Dashboard (Recommended): Configure your ZeroX API key in the Portal Dashboard. The SDK will use it automatically when zeroXApiKey is null.

  2. Per-Request Override: Provide the API key in the zeroXApiKey parameter to override the Dashboard configuration for this specific request.

Return

A Result containing ZeroXPriceResponse with the price data, including buy/sell amounts, price, gas estimates, fees breakdown, and liquidity availability. The response can be accessed via result.getOrNull()?.data?.rawResponse?.buyAmount for the buy amount, or result.getOrNull()?.data?.rawResponse?.gas for gas estimates.

Parameters

request

The price request parameters containing chain ID, tokens, amounts, and optional swap configuration. Use ZeroXPriceRequest.create for convenience, which handles Boolean to String conversion for sellEntireBalance.

  • Note: The chainId in the request is included in the request body.

zeroXApiKey

Optional ZeroX API key to override the one configured in Portal Dashboard.

  • If null: The SDK will use the API key configured in the Portal Dashboard.

  • If provided: This API key will be used for this request, overriding the Dashboard configuration.

  • The API key is used to authenticate requests to the ZeroX API service and is included in the request body.

Throws

if the URL cannot be constructed, or network/decoding errors if the request fails.