REST API Requests
Most REST API endpoints are for data retrieval and are defined as GET
requests, but some toolkit extensions require data to pass through the function. In these instances, it must be a POST
request.
Example
This example will use a GET request to fetch daily VIX data from the Cboe data extension, and then make a POST request which passes through the data to a technical analysis function.
First, start a development server.
uvicorn openbb_core.api.rest_api:app --reload
This example will use Python and the requests
library.
Fetch Some Data
import requests
get_url = "http://127.0.0.1:8000/api/v1/index/price/historical?provider=cboe&symbol=vix&interval=1d"
get_response = requests.get(get_url)
data_results = get_response.json()["results"]
data_results[-1]
{'date': '2023-11-17T00:00:00',
'open': 14.18,
'high': 14.19,
'low': 13.67,
'close': 13.79,
'volume': 0,
'calls_volume': None,
'puts_volume': None,
'total_options_volume': None}
Send a POST Request
Next, pass the data_results
to a function, using the json
field in the POST headers.
For this example, realized volatility cones, the default parameters assume the time series data is daily and that volatility should be annualized over 252 trading days.
import pandas as pd
post_url = "http://localhost:8000/api/v1/technical/cones"
post_response = requests.post(post_url, json=data_results)
ta_results = post_response.json()["results"]
pd.DataFrame.from_records(ta_results).head()
window | realized | min | lower_25% | median | upper_75% | max |
---|---|---|---|---|---|---|
3 | 0.396165 | 0.00701638 | 0.444709 | 0.72414 | 1.11563 | 8.47636 |
10 | 0.623199 | 0.190188 | 0.665584 | 0.852915 | 1.15491 | 4.83264 |
30 | 0.988435 | 0.332913 | 0.750007 | 0.921482 | 1.17072 | 2.98404 |
60 | 0.932594 | 0.47639 | 0.792548 | 0.964617 | 1.20171 | 2.35563 |
90 | 0.915137 | 0.551011 | 0.815229 | 0.965553 | 1.2128 | 2.04104 |
The output from the Fast API is a serialized version of this object, and these methods are lost on conversion. OBBject can be reconstructed to recover the helpers by importing the model and validating the data.
import requests
from openbb_core.app.model.obbject import OBBject
data = []
symbol="SPY"
url = f"http://127.0.0.1:8000/api/v1/equity/price/historical?provider=polygon&symbol={symbol}"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers, timeout=3)
if response.status_code == 200:
data = OBBject.model_validate(response.json())
data.to_df()