Function Examples
This page explains how to add examples and code snippets to API and Python endpoints. Usage examples are defined in the router and are expected to provide working syntax, with descriptions for complex functions requiring many parameters. It is encouraged to include examples for every endpoint.
There are two models for defining examples, APIEx and PythonEx.
from openbb_core.app.model.example import APIEx, PythonEx
When a provider is not installed, its example - if any - will be excluded from openapi.json and Python docstrings.
APIEx
APIEx is more structured (and has less freedom) aiming to be language agnostic.
Each example can have a description and as many parameters as desired.
@router.command(
model="WorldNews",
examples=[
APIEx(parameters={}),
APIEx(parameters={"limit": 100}),
APIEx(
description="Get news on the specified dates.",
parameters={"start_date": "2024-02-01", "end_date": "2024-02-07"},
),
APIEx(
description="Display the headlines of the news.",
parameters={"display": "headline", "provider": "benzinga"},
),
APIEx(
description="Get news by topics.",
parameters={"topics": "finance", "provider": "benzinga"},
),
APIEx(
description="Get news by source using 'tingo' as provider.",
parameters={"provider": "tiingo", "source": "bloomberg"},
),
APIEx(
description="Filter aticles by term using 'biztoc' as provider.",
parameters={"provider": "biztoc", "term": "apple"},
),
],
)
PythonEx
PythonEx gives more freedom to create complex examples.
A description is required, and each item in the code list represents one line of code.
@router.command(
methods=["POST"],
include_in_schema=False,
examples=[
PythonEx(
description="Perform Ordinary Least Squares (OLS) regression.",
code=[
"stock_data = obb.equity.price.historical(symbol='TSLA', start_date='2023-01-01', provider='fmp').to_df()",
'obb.econometrics.ols_regression(data=stock_data, y_column="close", x_columns=["open", "high", "low"])',
],
)
],
)