.. _toast-examples:
Code Examples
=============
Code examples for the the Toast API endpoint calls that are used in stock checker service.
Authentication
--------------
`Toast API Authentication Endpoint `__
.. code-block:: python
async def get_toast_access_token() -> Optional[str]:
"""
get_toast_access_token() -> Optional[str]
Authenticates with Toast API and returns an access token.
"""
if not CLIENT_ID or not CLIENT_SECRET:
logger.error("CLIENT_ID or CLIENT_SECRET not configured")
return None
url = "https://ws-api.toasttab.com/authentication/v1/authentication/login"
payload = {
"clientId": CLIENT_ID,
"clientSecret": CLIENT_SECRET,
"userAccessType": "TOAST_MACHINE_CLIENT"
}
headers = {"Content-Type": "application/json"}
Get Menu Items
--------------
`Toast API Menu v2 Items Endpoint `__
.. code-block:: python
async def get_menu_items(access_token: str, restaurant_external_id: str) -> Dict[str, Dict[str, str]]:
"""
get_menu_items(access_token: str, restaurant_external_id: str) -> Dict[str, Dict[str, str]]
Fetches menu items from Toast API and returns a mapping of GUID to item info.
"""
url = "https://ws-api.toasttab.com/menus/v2/menus"
headers = {
"Authorization": f"Bearer {access_token}",
"Toast-Restaurant-External-ID": restaurant_external_id,
"Content-Type": "application/json"
}
Check Store Stock
-----------------
`Toast API Stock Enpoint `__
.. code-block:: python
async def check_store_stock(store_name: str, restaurant_external_id: str, access_token: str) -> Dict:
"""
check_store_stock(store_name: str, restaurant_external_id: str, access_token: str) -> Dict
Checks stock status for all tracked items in a specific store.
Returns OUT_OF_STOCK and QUANTITY items.
"""
# Log tracking mode for this check
if USE_GUID_TRACKING:
logger.debug(f"Tracking {len(TRACKED_ITEM_GUIDS)} items by GUID for {store_name}")
else:
logger.debug(f"Tracking items by name for {store_name}")
url = "https://ws-api.toasttab.com/stock/v1/inventory"
params = {"status": "OUT_OF_STOCK"}
headers = {
"Authorization": f"Bearer {access_token}",
"Toast-Restaurant-External-ID": restaurant_external_id,
"Content-Type": "application/json"
}