Files
beanflows/notebooks/02_api.ipynb
2026-02-04 22:24:55 +01:00

1116 lines
52 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid character '📈' (U+1F4C8) (3606104645.py, line 1)",
"output_type": "error",
"traceback": [
" \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m1. 📈 Market Prices & Volume\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid character '📈' (U+1F4C8)\n"
]
}
],
"source": [
"\t1. 📈 Market Prices & Volume\n",
"\t\t○ Yahoo Finance API → Futures Prices # via yfinance or yahoo_fin\n",
"\t\t○ Alpha Vantage API → Market Data # https://www.alphavantage.co/documentation/ - key: H4RIQ01UTWVVVVN3\n",
"\n",
"\n",
"\t2. 🌱 Supply & Demand Reports\n",
"\t\t○ USDA API → WASDE Report, Crop Production # https://www.ers.usda.gov/developer/data-apis - key: n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\n",
"\t\t○ FAO API → Global Coffee Production # https://data.apps.fao.org/gismgr/web/v2/#/workspaces - key: a5961a8c8cc9852265754a4e414d7faa7bf1789e7d49245001be2e75e7e5d908cd29eb3eaf63d782\n",
"\n",
"\n",
"\t3. 💵 Macroeconomic Indicators\n",
"\t\t○ FRED API → Interest rates, Inflation, Employment # https://fredaccount.stlouisfed.org/ - key: bc80b84ca1d2bb8283b065bd0455d84b\n",
"\t\t○ EIA API → Fuel/Oil Prices for Transportation # https://www.eia.gov/opendata/ - key: e0PMDw5AvTblYgotvAIB7aPyNcS2X2n5BZ4aayfB\n",
"\n",
"\n",
"\t4. 📝 Options Positioning & Futures Sentiment\n",
"\t\t○ CFTC COT Data → Commitment of Traders Report (CFTC) # cot_reports or Socrata Open Data Api https://dev.socrata.com/foundry/publicreporting.cftc.gov/6dca-aqww\n",
"\t\t○ Barchart API → Options Sentiment & Futures Positioning (Free Tier Available but need to sign up)\n",
"\t\t○ CBOE → Put/Call Ratios (via Web Scraping) # Barchart API alternatively\n",
"\n",
"\n",
"\t5. ⛅ Weather Data (Affects Coffee Crop Yields)\n",
"\t\t○ OpenWeatherMap API → Real-Time Weather for Commodity Regions # https://openweathermap.org/ - key: b781d014d69dca442c50ce4cc64fbbca\n",
"\t\t○ NOAA API → Historical Climate Trends #https://www.ncdc.noaa.gov/cdo-web/ - key: fcYPiwZurnEAGcrudtMNXhIavzfHNdRU\n",
"\n",
"\n",
"\t6. 📦 Coffee Supply Chain Data\n",
"\t\t○ ICE US Futures Data → Warehouse Stocks (via Web Scraping) # via USDA / EIA / Quandl / Barchart\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'pkg_resources'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01malpha_vantage\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mtimeseries\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m TimeSeries\n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mfredapi\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Fred\n\u001b[32m----> \u001b[39m\u001b[32m6\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpyowm\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mowm\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m OWM\n\u001b[32m 7\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mdatetime\u001b[39;00m\n\u001b[32m 8\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mjson\u001b[39;00m\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\ziran\\Desktop\\Test\\materia\\.venv\\Lib\\site-packages\\pyowm\\__init__.py:4\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m#!/usr/bin/env python\u001b[39;00m\n\u001b[32m 2\u001b[39m \u001b[38;5;66;03m# -*- coding: utf-8 -*-\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m4\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpyowm\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mowm\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m OWM\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\ziran\\Desktop\\Test\\materia\\.venv\\Lib\\site-packages\\pyowm\\owm.py:14\u001b[39m\n\u001b[32m 12\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpyowm\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01muvindexapi30\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m uvindex_manager\n\u001b[32m 13\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpyowm\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mutils\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m config \u001b[38;5;28;01mas\u001b[39;00m cfg\n\u001b[32m---> \u001b[39m\u001b[32m14\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpyowm\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mcommons\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m cityidregistry\n\u001b[32m 15\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpyowm\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mweatherapi25\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m weather_manager\n\u001b[32m 18\u001b[39m \u001b[38;5;28;01mclass\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mOWM\u001b[39;00m:\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\ziran\\Desktop\\Test\\materia\\.venv\\Lib\\site-packages\\pyowm\\commons\\cityidregistry.py:7\u001b[39m\n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01msqlite3\u001b[39;00m\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtempfile\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpkg_resources\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m resource_filename\n\u001b[32m 8\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpyowm\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mweatherapi25\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mlocation\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Location\n\u001b[32m 10\u001b[39m CITY_ID_DB_PATH = \u001b[33m'\u001b[39m\u001b[33mcityids/cities.db.bz2\u001b[39m\u001b[33m'\u001b[39m\n",
"\u001b[31mModuleNotFoundError\u001b[39m: No module named 'pkg_resources'"
]
}
],
"source": [
"#Example Code for API Requests\n",
"import requests\n",
"import yfinance as yf\n",
"from alpha_vantage.timeseries import TimeSeries\n",
"from fredapi import Fred\n",
"from pyowm.owm import OWM\n",
"import datetime\n",
"import json\n",
"\n",
"# --- 1. 📈 Market Prices & Volume --------------------------------------------\n",
"\n",
"print(\"\\n--- Market Prices: Coffee & Cocoa Futures (Yahoo Finance) ---\")\n",
"\n",
"coffee = yf.Ticker(\"KC=F\") # Coffee C futures\n",
"cocoa = yf.Ticker(\"CC=F\") # Cocoa futures\n",
"\n",
"coffee_data = coffee.history(period=\"5d\")[['Close', 'Volume']]\n",
"cocoa_data = cocoa.history(period=\"5d\")[['Close', 'Volume']]\n",
"\n",
"print(\"Coffee Futures:\\n\", coffee_data)\n",
"print(\"Cocoa Futures:\\n\", cocoa_data)\n",
"\n",
"print(\"\\n--- Alpha Vantage Market Data (USD Index as proxy) ---\")\n",
"av_key = 'H4RIQ01UTWVVVVN3'\n",
"ts = TimeSeries(key=av_key, output_format='pandas')\n",
"data, meta = ts.get_intraday(symbol='USD', interval='15min', outputsize='compact')\n",
"print(data.head())\n",
"\n",
"# --- 2. 🌱 Supply & Demand Reports --------------------------------------------\n",
"\n",
"print(\"\\n--- USDA: Coffee & Cocoa Production Reports ---\")\n",
"\n",
"usda_key = \"n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
"usda_url = f\"https://api.nal.usda.gov/fdc/v1/foods/search?query=coffee&api_key={usda_key}\"\n",
"\n",
"response = requests.get(usda_url)\n",
"if response.status_code == 200:\n",
" print(\"USDA Coffee Report:\\n\", json.dumps(response.json(), indent=2))\n",
"else:\n",
" print(\"USDA API error:\", response.status_code)\n",
"\n",
"# --- FAO API Call (Coffee Production Dataset) ---\n",
"# FAO is complex — below is a real coffee dataset pull from their FAOSTAT bulk API\n",
"\n",
"print(\"\\n--- FAO: Global Coffee Production Data ---\")\n",
"\n",
"fao_dataset_url = \"http://fenixservices.fao.org/faostat/api/v1/en/data/QC?area=all&item=Coffee%2C%20green&element=Production%20Quantity&year=2021&format=JSON\"\n",
"\n",
"response = requests.get(fao_dataset_url)\n",
"if response.status_code == 200:\n",
" coffee_fao_data = response.json().get('data', [])[:5]\n",
" print(\"FAO Coffee Production (2021):\\n\", coffee_fao_data)\n",
"else:\n",
" print(\"FAO API error:\", response.status_code)\n",
"\n",
"# --- 3. 💵 Macroeconomic Indicators --------------------------------------------\n",
"\n",
"print(\"\\n--- FRED: Interest Rates & Inflation ---\")\n",
"\n",
"fred = Fred(api_key=\"bc80b84ca1d2bb8283b065bd0455d84b\")\n",
"fed_rate = fred.get_series('FEDFUNDS').tail()\n",
"inflation = fred.get_series('CPIAUCSL').tail()\n",
"\n",
"print(\"Fed Funds Rate:\\n\", fed_rate)\n",
"print(\"Inflation (CPI):\\n\", inflation)\n",
"\n",
"print(\"\\n--- EIA: WTI Crude Oil Prices (Transportation Cost Proxy) ---\")\n",
"\n",
"eia_key = \"e0PMDw5AvTblYgotvAIB7aPyNcS2X2n5BZ4aayfB\"\n",
"eia_url = f\"https://api.eia.gov/series/?api_key={eia_key}&series_id=PET.RWTC.D\"\n",
"\n",
"response = requests.get(eia_url)\n",
"if response.status_code == 200:\n",
" print(\"EIA Crude Prices:\\n\", json.dumps(response.json()['series'][0]['data'][:5], indent=2))\n",
"else:\n",
" print(\"EIA API error:\", response.status_code)\n",
"\n",
"# --- 4. 📝 Futures Sentiment & COT Positioning --------------------------------\n",
"\n",
"print(\"\\n--- CFTC: Commitment of Traders Report for Coffee ---\")\n",
"\n",
"cot_url = \"https://publicreporting.cftc.gov/resource/6dca-aqww.json?$limit=5&commodity=COFFEE\"\n",
"response = requests.get(cot_url)\n",
"if response.status_code == 200:\n",
" print(\"COT Coffee Report:\\n\", response.json())\n",
"else:\n",
" print(\"CFTC COT API error:\", response.status_code)\n",
"\n",
"# --- 5. ⛅ Weather Data for Key Growing Regions --------------------------------\n",
"\n",
"print(\"\\n--- Weather: Real-Time Conditions for Coffee/Cocoa Regions ---\")\n",
"\n",
"owm_key = 'b781d014d69dca442c50ce4cc64fbbca'\n",
"owm = OWM(owm_key)\n",
"mgr = owm.weather_manager()\n",
"\n",
"regions = {\n",
" \"Sao Paulo, Brazil (Coffee)\": \"Sao Paulo,BR\",\n",
" \"Addis Ababa, Ethiopia (Coffee)\": \"Addis Ababa,ET\",\n",
" \"Abidjan, Ivory Coast (Cocoa)\": \"Abidjan,CI\",\n",
" \"Accra, Ghana (Cocoa)\": \"Accra,GH\"\n",
"}\n",
"\n",
"for region_name, location in regions.items():\n",
" weather = mgr.weather_at_place(location).weather\n",
" temp = weather.temperature('celsius')\n",
" print(f\"\\nWeather in {region_name}:\")\n",
" print(f\"Temperature: {temp['temp']}°C, Humidity: {weather.humidity}%, Condition: {weather.detailed_status}\")\n",
"\n",
"# --- 6. 📦 Supply Chain / Warehouse Stocks ------------------------------------\n",
"\n",
"print(\"\\n--- Supply Chain Note: Warehouse stocks usually require web scraping or paid APIs (ICE, USDA GAIN, Barchart).\")\n",
"\n",
"# Placeholder for warehouse stock via scraping or subscription APIs like Quandl or ICE\n",
"# Alternative: USDA GAIN reports: https://apps.fas.usda.gov/gainsearch/\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" <th>3</th>\n",
" <th>4</th>\n",
" <th>5</th>\n",
" <th>6</th>\n",
" <th>7</th>\n",
" <th>8</th>\n",
" <th>9</th>\n",
" <th>10</th>\n",
" <th>11</th>\n",
" <th>12</th>\n",
" <th>13</th>\n",
" <th>14</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" <td>{'commodityCode': '0440000', 'countryCode': '0...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 1 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 2 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 3 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 4 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 5 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 6 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 7 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 8 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 9 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 10 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 11 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 12 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 13 \\\n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... \n",
"\n",
" 14 \n",
"0 {'commodityCode': '0440000', 'countryCode': '0... \n",
"1 {'commodityCode': '0440000', 'countryCode': '0... \n",
"2 {'commodityCode': '0440000', 'countryCode': '0... \n",
"3 {'commodityCode': '0440000', 'countryCode': '0... \n",
"4 {'commodityCode': '0440000', 'countryCode': '0... \n",
"5 {'commodityCode': '0440000', 'countryCode': '0... \n",
"6 {'commodityCode': '0440000', 'countryCode': '0... \n",
"7 {'commodityCode': '0440000', 'countryCode': '0... \n",
"8 {'commodityCode': '0440000', 'countryCode': '0... \n",
"9 {'commodityCode': '0440000', 'countryCode': '0... \n",
"10 {'commodityCode': '0440000', 'countryCode': '0... "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import requests\n",
"import pandas as pd\n",
"\n",
"# Define the commodity code for corn\n",
"commodity_code = '0440000'\n",
"\n",
"# Define the years of interest\n",
"years = range(2010, 2021)\n",
"\n",
"# Initialize an empty list to store the data\n",
"data = []\n",
"\n",
"# Loop through each year and fetch the data\n",
"for year in years:\n",
" url = f'https://api.fas.usda.gov/api/psd/commodity/{commodity_code}/world/year/{year}?api_key=n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA'\n",
" response = requests.get(url)\n",
" if response.status_code == 200:\n",
" data.append(response.json())\n",
" else:\n",
" print(f'Failed to fetch data for {year}')\n",
"\n",
"# Convert the list of data into a DataFrame\n",
"df = pd.DataFrame(data)\n",
"\n",
"# Display the DataFrame\n",
"df\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" commodityCode countryCode marketYear calendarYear month attributeId \\\n",
"0 0440000 00 2011 2025 04 4 \n",
"1 0440000 00 2011 2025 04 20 \n",
"2 0440000 00 2011 2025 04 28 \n",
"3 0440000 00 2011 2025 04 57 \n",
"4 0440000 00 2011 2025 04 81 \n",
"\n",
" unitId value \n",
"0 4 175808.0 \n",
"1 8 115328.0 \n",
"2 8 910355.0 \n",
"3 8 100631.0 \n",
"4 8 101710.0 \n"
]
}
],
"source": [
"import requests\n",
"import pandas as pd\n",
"\n",
"# Define the URL for the USDA API\n",
"url = \"https://api.fas.usda.gov/api/psd/commodity/0440000/world/year/2011?api_key=n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
"\n",
"# Fetch the data\n",
"response = requests.get(url)\n",
"data = response.json()\n",
"\n",
"# Convert the JSON data to a pandas DataFrame\n",
"df = pd.DataFrame(data)\n",
"\n",
"# Display the DataFrame\n",
"print(df.head())\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" commodityCode countryCode marketYear calendarYear month attributeId \\\n",
"0 0440000 00 2011 2025 04 4 \n",
"1 0440000 00 2011 2025 04 20 \n",
"2 0440000 00 2011 2025 04 28 \n",
"3 0440000 00 2011 2025 04 57 \n",
"4 0440000 00 2011 2025 04 81 \n",
"\n",
" unitId value \n",
"0 4 175808.0 \n",
"1 8 115328.0 \n",
"2 8 910355.0 \n",
"3 8 100631.0 \n",
"4 8 101710.0 \n"
]
}
],
"source": [
"import requests\n",
"import pandas as pd\n",
"\n",
"# Define the URL for the USDA API\n",
"url = \"https://api.fas.usda.gov/api/psd/commodity/0440000/world/year/2011?api_key=n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
"\n",
"# Fetch the data\n",
"response = requests.get(url)\n",
"data = response.json()\n",
"\n",
"# Convert the JSON data to a pandas DataFrame\n",
"df = pd.DataFrame(data)\n",
"\n",
"# Display the DataFrame\n",
"print(df.head())\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Status code: 200\n",
"Number of entries: 63\n"
]
}
],
"source": [
"import requests\n",
"\n",
"API_KEY = \"n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
"url = f\"https://api.fas.usda.gov/api/psd/commodities?api_key={API_KEY}\"\n",
"\n",
"resp = requests.get(url)\n",
"print(\"Status code:\", resp.status_code)\n",
"resp.raise_for_status()\n",
"\n",
"data = resp.json()\n",
"print(\"Number of entries:\", len(data))\n",
"\n",
"# Print each commodity code\n",
"for item in data:\n",
" code = item.get(\"commodity_code\") or item.get(\"Commodity_Code\")\n",
" if code:\n",
" print(code)\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Status code: 200\n",
"Total commodity entries found: 63\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n",
"None\n"
]
}
],
"source": [
"import requests\n",
"\n",
"API_KEY = \"n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
"url = f\"https://api.fas.usda.gov/api/psd/commodities?api_key={API_KEY}\"\n",
"\n",
"resp = requests.get(url)\n",
"print(\"Status code:\", resp.status_code)\n",
"resp.raise_for_status()\n",
"\n",
"payload = resp.json()\n",
"\n",
"# Detect where the list of commodities lives\n",
"if isinstance(payload, dict):\n",
" for wrap_key in (\"data\", \"value\", \"commodities\"):\n",
" if wrap_key in payload and isinstance(payload[wrap_key], list):\n",
" entries = payload[wrap_key]\n",
" break\n",
" else:\n",
" entries = []\n",
"else:\n",
" entries = payload\n",
"\n",
"print(\"Total commodity entries found:\", len(entries))\n",
"\n",
"# Print each code\n",
"for item in entries:\n",
" code = item.get(\"commodity_code\") or item.get(\"Commodity_Code\")\n",
" print(code)\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0577400 Almonds, Shelled Basis\n",
"0011000 Animal Numbers, Cattle\n",
"0013000 Animal Numbers, Swine\n",
"0574000 Apples, Fresh\n",
"0430000 Barley\n",
"0579305 Cherries (Sweet&Sour), Fresh\n",
"0711100 Coffee, Green\n",
"0440000 Corn\n",
"2631000 Cotton\n",
"0230000 Dairy, Butter\n",
"0240000 Dairy, Cheese\n",
"0224400 Dairy, Dry Whole Milk Powder\n",
"0223000 Dairy, Milk, Fluid\n",
"0224200 Dairy, Milk, Nonfat Dry\n",
"0572220 Grapefruit, Fresh\n",
"0575100 Grapes, Fresh Table\n",
"0572120 Lemons/Limes, Fresh\n",
"0813700 Meal, Copra\n",
"0813300 Meal, Cottonseed\n",
"0814200 Meal, Fish\n",
"0813800 Meal, Palm Kernel\n",
"0813200 Meal, Peanut\n",
"0813600 Meal, Rapeseed\n",
"0813100 Meal, Soybean\n",
"0813101 Meal, Soybean (Local)\n",
"0813500 Meal, Sunflowerseed\n",
"0111000 Meat, Beef and Veal\n",
"0115000 Meat, Chicken\n",
"0113000 Meat, Swine\n",
"0459100 Millet\n",
"0459900 Mixed Grain\n",
"0452000 Oats\n",
"4242000 Oil, Coconut\n",
"4233000 Oil, Cottonseed\n",
"4235000 Oil, Olive\n",
"4243000 Oil, Palm\n",
"4244000 Oil, Palm Kernel\n",
"4234000 Oil, Peanut\n",
"4239100 Oil, Rapeseed\n",
"4232000 Oil, Soybean\n",
"4232001 Oil, Soybean (Local)\n",
"4236000 Oil, Sunflowerseed\n",
"2231000 Oilseed, Copra\n",
"2223000 Oilseed, Cottonseed\n",
"2232000 Oilseed, Palm Kernel\n",
"2221000 Oilseed, Peanut\n",
"2226000 Oilseed, Rapeseed\n",
"2222000 Oilseed, Soybean\n",
"2222001 Oilseed, Soybean (Local)\n",
"2224000 Oilseed, Sunflowerseed\n",
"0585100 Orange Juice\n",
"0571120 Oranges, Fresh\n",
"0579309 Peaches & Nectarines, Fresh\n",
"0579220 Pears, Fresh\n",
"0577907 Pistachios, Inshell Basis\n",
"0114200 Poultry, Meat, Broiler\n",
"0422110 Rice, Milled\n",
"0451000 Rye\n",
"0459200 Sorghum\n",
"0612000 Sugar, Centrifugal\n",
"0571220 Tangerines/Mandarins, Fresh\n",
"0577901 Walnuts, Inshell Basis\n",
"0410000 Wheat\n",
"First entry keys: ['commodityCode', 'commodityName']\n",
"\n",
"First entry full content:\n",
"{\n",
" \"commodityCode\": \"0577400\",\n",
" \"commodityName\": \"Almonds, Shelled Basis\"\n",
"}\n"
]
}
],
"source": [
"import requests, json\n",
"\n",
"API_KEY = \"n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
"url = f\"https://api.fas.usda.gov/api/psd/commodities?api_key={API_KEY}\"\n",
"\n",
"resp = requests.get(url)\n",
"resp.raise_for_status()\n",
"payload = resp.json()\n",
"for item in payload:\n",
" print(item[\"commodityCode\"] + \" \"+ item[\"commodityName\"])\n",
" \n",
"# Inspect the keys of the first entry\n",
"print(\"First entry keys:\", list(payload[0].keys()))\n",
"print(\"\\nFirst entry full content:\")\n",
"print(json.dumps(payload[0], indent=2))\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Returned 63 commodities:\n",
"\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n",
"None None\n"
]
}
],
"source": [
"import requests\n",
"\n",
"API_KEY = \"n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
"url = f\"https://api.fas.usda.gov/api/psd/commodities?api_key={API_KEY}\"\n",
"\n",
"resp = requests.get(url)\n",
"resp.raise_for_status()\n",
"data = resp.json()\n",
"\n",
"print(f\"Returned {len(data)} commodities:\\n\")\n",
"for item in data:\n",
" code = item.get(\"commodity_code\") or item.get(\"Commodity_Code\")\n",
" desc = item.get(\"commodity_desc\") or item.get(\"Commodity_Desc\")\n",
" # If the description key is different, inspect one entry with `print(item.keys())` to find the right name\n",
" print(f\"{code} {desc}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}