# Direct price

## What is this endpoint for?

This endpoint returns a price calculation for a specific pair by aggregating prices from our Trade Data.&#x20;

Read the full methodology [here](https://25446524.fs1.hubspotusercontent-eu1.net/hubfs/25446524/Factsheets/Kaiko%20Pricing%20Services%20Methodology.pdf).&#x20;

{% hint style="info" %}
If a null value is returned, it means there are not enough trades to calculate a direct price. In this case, use [synthetic-price](https://docs.kaiko.com/rest-api/analytics-solutions/kaiko-fair-market-value/established-assets/synthetic-price "mention") instead.
{% endhint %}

### Endpoint

{% code overflow="wrap" %}

```http
https://<eu|us>.market-api.kaiko.io/v2/data/trades.v1/robust_pair_price/{base_asset}/{quote_asset}
```

{% endcode %}

### Path parameters

<table><thead><tr><th width="203">Parameter	</th><th width="106">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>base_asset</code></td><td>Yes</td><td>The desired base asset <code>code</code>.<br><br>See <a data-mention href="../../../data-feeds/reference-data/basic-tier/asset-codes">asset-codes</a></td></tr><tr><td><code>quote_asset</code></td><td>Yes</td><td>The desired quote asset <code>code</code>.<br><br>See <a data-mention href="../../../data-feeds/reference-data/basic-tier/asset-codes">asset-codes</a></td></tr><tr><td><code>sort</code></td><td>No</td><td>Return the data in ascending (<code>asc</code>) or descending (<code>desc</code>) order. Default is <code>desc</code>.</td></tr></tbody></table>

### Query parameters

<table><thead><tr><th width="203">Parameter	</th><th width="106">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>interval</code></td><td>Yes</td><td>The interval parameter is suffixed with <code>s</code>, <code>m</code>, <code>h</code> or <code>d</code> to specify seconds, minutes, hours or days, respectively.<br><br>Any arbitrary value between one second and one day can be used, as long as it sums up to a maximum of 1 day. The suffixes are <code>s</code> (second), <code>m</code> (minute), <code>h</code> (hour) and <code>d</code> (day).<br><br> Default <code>24h</code>.</td></tr><tr><td><code>start_time</code></td><td>No</td><td>Starting time in ISO 8601 (inclusive).<br><br><em>Automatically included in continuation tokens.</em></td></tr><tr><td><code>end_time</code></td><td>No</td><td>Ending time in ISO 8601 (exclusive)<br><br><em>Automatically included in continuation tokens.</em></td></tr><tr><td><code>page_size</code></td><td>No</td><td><p>See <a data-mention href="../../../general/getting-started/pagination">pagination</a><br><br><strong>Page size limits differ by the <code>interval</code> selected:</strong></p><ul><li>Less than or equal to <code>1m</code><br> Default: <code>10</code>, Max: <code>100</code></li><li><code>1m</code> to <code>1h</code><br>Default: <code>4</code>, Max: <code>10</code></li><li>More than <code>1h</code><br>Default: <code>1</code>, Max: <code>4</code></li></ul><p><em>Automatically included in continuation tokens.</em></p></td></tr><tr><td><code>sort</code></td><td>No</td><td>Return the data in ascending (<code>asc</code>) or descending (<code>desc</code>) order. Default is <code>desc</code>.</td></tr><tr><td><code>include_exchanges</code></td><td>No</td><td>List of exchanges' <code>code</code> to include in the calculation.<br><br><a data-mention href="../../../data-feeds/reference-data/basic-tier/exchange-codes">exchange-codes</a><br><br>Default is all exchanges.<br><br><em>Automatically included in continuation tokens.</em></td></tr><tr><td><code>exclude_exchanges</code></td><td>No</td><td>List of exchanges' <code>code</code> to exclude in the calculation.<br><br><a data-mention href="broken-reference">Broken link</a><br><br><em>Automatically included in continuation tokens.</em></td></tr><tr><td><code>extrapolate_missing_values</code></td><td>No</td><td>When <code>true</code>, if there are any <code>null</code> (missing) prices for the calculation, they will be filled in using the last available price from the window requested. This is useful for assets that don't have a lot of trades or for data that is collected very frequently.<br><br>However, if the parameter is set to <code>true</code> and no prices were available in that window, a <code>null</code> value will still be returned.</td></tr></tbody></table>

### Fields

<table><thead><tr><th width="283">Field</th><th>Description</th></tr></thead><tbody><tr><td><code>timestamp</code></td><td>Timestamp at which the interval begins.</td></tr><tr><td><code>price</code></td><td><a href="https://hal.science/hal-04017151v1/document">RWM Robust Weighted Median</a>. <br><br><code>null</code> when no trades reported, except if <code>extrapolate_missing_values</code> is <code>true</code></td></tr><tr><td><code>volume</code></td><td><p>Total volume in base asset traded in the interval. <br></p><p><code>0</code> when no trades are reported, except if <code>extrapolate_missing_values</code> is <code>true</code>.</p></td></tr><tr><td><code>count</code></td><td>Total amount of trades reported during the interval. <br><br><code>0</code> when no trades are reported, except if <code>extrapolate_missing_values</code> is <code>true</code>.</td></tr><tr><td><code>extrapolate_missing_values</code></td><td><code>true</code> if the value has been extrapolated from the last computed value available, <code>false</code> if not.</td></tr></tbody></table>

### Request examples

{% tabs %}
{% tab title="cURL" %}
{% code overflow="wrap" %}

```url
curl --compressed -H 'Accept: application/json' -H 'X-Api-Key: <client-api-key>' \
  'https://us.market-api.kaiko.io/v2/data/trades.v1/robust_pair_price/btc/eth?interval=1m&extrapolate_missing_values=true&start_time=2023-05-03T00:01:00.000Z&end_time=2023-05-04T00:00:00.000Z'
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code overflow="wrap" %}

```python
##### 1. Import dependencies #####
import requests
import pandas as pd

##### 2. Choose the value of the query's parameters #####
# ---- Required parameters ---- #
base_asset = "btc"
quote_asset = "usd"
interval = "1h"

# ---- Optional parameters ---- #
sort = "desc"
page_size = 4
start_time= "2023-01-01T00:00:00Z"
end_time= "2023-01-01T23:59:59Z"
include_exchanges = None
exclude_exchanges = None
extrapolate_missing_values = None

# ---- API key configuration ---- #
api_key = "YOUR_API_KEY"

##### 3. Get the data #####
# ---- Function to run an API call ---- # 
# Get the data in a dataframe --------- # 

def get_kaiko_data(api_key: str, base_asset: str, quote_asset: str, interval: str, start_time: str, end_time: str, sort: str, page_size: int, include_exchanges: list = None, exclude_exchanges: list = None, extrapolate_missing_values: bool = None):
    headers = {'Accept': 'application/json', 'X-Api-Key': api_key}
    
    url = f'https://us.market-api.kaiko.io/v2/data/trades.v1/robust_pair_price/{base_asset}/{quote_asset}'
    params = {
        "start_time": start_time,
        "end_time": end_time,
        "sort": sort,
        "page_size": page_size,
        "interval": interval,
        "include_exchanges": include_exchanges,
        "exclude_exchanges": exclude_exchanges,
        "extrapolate_missing_values": extrapolate_missing_values
    }

    try:
        res = requests.get(url, headers=headers, params=params)
        res.raise_for_status() 
        data = res.json()
        if 'data' not in data:
            print("No data returned.")
            return pd.DataFrame() 
        df = pd.DataFrame(data['data'])

           # Handle pagination with continuation token
        while 'next_url' in data:
            next_url = data['next_url']
            if next_url is None:
                break
            res = requests.get(next_url, headers=headers)
            res.raise_for_status()
            data = res.json()
            if 'data' in data:
                df = pd.concat([df, pd.DataFrame(data['data'])], ignore_index=True)
        return df

    except requests.exceptions.RequestException as e:
        print(f"API request error: {e}")
        return pd.DataFrame() 

# ---- Get the data ---- #
df = get_kaiko_data(api_key=api_key, base_asset=base_asset, quote_asset=quote_asset, interval=interval, start_time=start_time, end_time=end_time, sort=sort, page_size=page_size, include_exchanges=include_exchanges, exclude_exchanges=exclude_exchanges, extrapolate_missing_values=extrapolate_missing_values)
print (df)
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Response example

```json
{
  "query": {
    "start_time": "2023-05-03T23:50:00Z",
    "end_time": "2023-05-04T00:00:00Z",
    "base_asset": "btc",
    "quote_asset": "eth",
    "interval": "1m",
    "sort": "desc",
    "sources": false,
    "page_size": 10,
    "include_exchanges": [],
    "exclude_exchanges": [],
    "request_time": "2024-07-11T07:39:11.34695219Z",
    "data_version": "v1",
    "commodity": "trades",
    "extrapolate_missing_values": true,
    "instruments": [
      "bbsp:spot:eth-btc",
      "bfly:spot:eth-btc",
      "bfnx:spot:eth-btc",
      "bgon:spot:eth-btc",
      "binc:spot:eth-btc",
      "bnus:spot:eth-btc",
      "bull:spot:eth-btc",
      "cbse:spot:eth-btc",
      "cnex:spot:eth-btc",
      "kcon:spot:eth-btc",
      "krkn:spot:eth-btc",
      "lmax:spot:eth-btc",
      "okex:spot:eth-btc",
      "stmp:spot:eth-btc",
      "yobt:spot:eth-btc"
    ]
  },
  "time": "2024-07-11T07:39:11.430101692Z",
  "timestamp": 1720683551,
  "data": [
    {
      "timestamp": 1683158340000,
      "price": "15.236109727862226",
      "volume": "10.661977267122458",
      "count": 263,
      "extrapolate_missing_values": false
    },
    {
      "timestamp": 1683158280000,
      "price": "15.226897227981592",
      "volume": "26.42011153624422",
      "count": 184,
      "extrapolate_missing_values": false
    },
		/*...*/
	],  
	"result": "success",
  "continuation_token": "V7Dxo9XotwyC1qQtT6Dkaq3fhY8jFqzmgjwkALFWdZQ4JHWoUQFrDaTw8Zc4yCY4Cf863uPBY4phumdqcjoL4imnx5amnLJCZP3rr7dDBw2EC33kpYtsRPsmx1sVW2tfp5pUh72fP9gYrhHzzQpGAz5PKFFwiTuHT921xT1ajG8EV9aRibXxs69PLGwnfH6WD5iw4SAc58c7ZF8PafYgb34APyYC1",
  "next_url": "https://us.market-api.kaiko.io/v2/data/trades.v1/robust_pair_price/btc/eth?continuation_token=V7Dxo9XotwyC1qQtT6Dkaq3fhY8jFqzmgjwkALFWdZQ4JHWoUQFrDaTw8Zc4yCY4Cf863uPBY4phumdqcjoL4imnx5amnLJCZP3rr7dDBw2EC33kpYtsRPsmx1sVW2tfp5pUh72fP9gYrhHzzQpGAz5PKFFwiTuHT921xT1ajG8EV9aRibXxs69PLGwnfH6WD5iw4SAc58c7ZF8PafYgb34APyYC1",
  "access": {
    "access_range": {
      "start_timestamp": 1262995200000,
      "end_timestamp": 2186006399000
    },
    "data_range": {
      "start_timestamp": null,
      "end_timestamp": null
    }
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.kaiko.com/rest-api/analytics-solutions/kaiko-fair-market-value/established-assets/direct-price.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
