Skip to main content

Polygon Stock Market API Tools

Polygon The Stocks API provides REST endpoints that let you query the latest market data from all US stock exchanges.

This notebook uses tools to get stock market data like the latest quote and news for a ticker from Polygon.

import getpass
import os

os.environ["POLYGON_API_KEY"] = getpass.getpass()
from import PolygonLastQuote
from import PolygonTickerNews
from langchain_community.utilities.polygon import PolygonAPIWrapper
api_wrapper = PolygonAPIWrapper()
ticker = "AAPL"

Get latest quote for ticker

# Get the last quote for ticker
last_quote_tool = PolygonLastQuote(api_wrapper=api_wrapper)
last_quote =
print(f"Tool output: {last_quote}")
Tool output: {"P": 180.99, "S": 5, "T": "AAPL", "X": 11, "i": [604], "p": 180.98, "q": 31662815, "s": 2, "t": 1708445246516556649, "x": 21, "y": 1708445246516369924, "z": 3}
import json

# Convert the last quote response to JSON
last_quote =
last_quote_json = json.loads(last_quote)
# Print the latest price for ticker
latest_price = last_quote_json["p"]
print(f"Latest price for {ticker} is ${latest_price}")
Latest price for AAPL is $180.74

Get latest news for ticker

ticker_news_tool = PolygonTickerNews(api_wrapper=api_wrapper)
ticker_news =
print(f"Tool output: {ticker_news}")
Tool output: [{"id": "RckckqTS-K2zI6WRyycBB0HonfGLc2MPTQROWsbpKSA", "publisher": {"name": "The Motley Fool", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Should You Worry About Apple's Slowdown in China?", "author": " (Adria Cimino)", "published_utc": "2024-02-20T15:10:00Z", "article_url": "", "tickers": ["AAPL"], "image_url": "", "description": "A local smartphone giant is gaining market share.", "keywords": ["investing"]}, {"id": "5goYCKw3ZsHlJYwN1XX8AJCyybZuvfJeoUz38SzlYBM", "publisher": {"name": "The Motley Fool", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Vanguard Total Stock Market ETF: Buy, Sell, or Hold?", "author": " (Justin Pope)", "published_utc": "2024-02-20T12:30:00Z", "article_url": "", "tickers": ["VTI", "AAPL", "MSFT", "GOOGL", "AMZN", "NVDA", "META", "TSLA", "GOOG"], "image_url": "", "description": "This is the ultimate tool to diversify your portfolio.", "keywords": ["investing"]}, {"id": "F2nshszd6rBX-JJMHtbPuA325ZBizcq7iIJ7PBK_114", "publisher": {"name": "MarketWatch", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Wall Street\u2019s \u2018mob psychology\u2019 could fuel a dangerous stock meltup, warns top strategist", "author": "MarketWatch", "published_utc": "2024-02-20T12:20:00Z", "article_url": "", "tickers": ["NVDA", "GOOGL", "AMZN", "AAPL", "META", "MSFT", "NFLX", "TSLA", "COF", "DFS", "WMT", "VZIO", "HD", "PANW", "CZR", "SMCI", "OCGN", "NIO", "MARA", "AMD", "COIN"], "amp_url": "", "image_url": "", "description": "Ed Yardeni says giddy Wall Street analysts could be the undoing of this stock market."}, {"id": "uvUtqCvuVV-wyCECL0DTIbHdqAtEgviJqSrPurvrqEQ", "publisher": {"name": "Zacks Investment Research", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Should Vanguard Mega Cap ETF (MGC) Be on Your Investing Radar?", "author": "Zacks Equity Research", "published_utc": "2024-02-20T11:20:07Z", "article_url": "", "tickers": ["MGC", "AMZN", "AAPL", "MSFT", "SPY", "IVV"], "amp_url": "", "image_url": "", "description": "Style Box ETF report for MGC"}, {"id": "RRxHgtvw7_7Ql0QhUphMlzXdUjWhJbpZVjHqR7N5TCg", "publisher": {"name": "Zacks Investment Research", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Should Vanguard S&P 500 ETF (VOO) Be on Your Investing Radar?", "author": "Zacks Equity Research", "published_utc": "2024-02-20T11:20:06Z", "article_url": "", "tickers": ["VOO", "AMZN", "AAPL", "MSFT", "SPY", "IVV"], "amp_url": "", "image_url": "", "description": "Style Box ETF report for VOO"}, {"id": "QBaNxnCVlXTHlVAChWRdM5RrWCD9f20qp6aUxfdxrEI", "publisher": {"name": "Zacks Investment Research", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Should Fidelity Nasdaq Composite Index ETF (ONEQ) Be on Your Investing Radar?", "author": "Zacks Equity Research", "published_utc": "2024-02-20T11:20:05Z", "article_url": "", "tickers": ["ONEQ", "AMZN", "AAPL", "MSFT", "QQQ", "VUG"], "amp_url": "", "image_url": "", "description": "Style Box ETF report for ONEQ"}, {"id": "aVYhWKzOHm1Y8k8z9ka0xrCCI-tiUWYDdsrPuDFzG_g", "publisher": {"name": "The Motley Fool", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Apple Is Synonymous With iPhones, But Its Largest Gross Margin Comes From Somewhere Else Entirely", "author": " (Neil Patel)", "published_utc": "2024-02-20T11:07:00Z", "article_url": "", "tickers": ["AAPL"], "image_url": "", "description": "Investors need to pay attention to a budding segment.", "keywords": ["investing"]}, {"id": "6uW_52HjWOj3sKeCdPrqN4ZHmIbJAZxXV5naPVzjaHw", "publisher": {"name": "The Motley Fool", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "Warren Buffett Has 51% of Berkshire Hathaway's $370 Billion Portfolio Invested in 2 Stocks", "author": " (Trevor Jennewine)", "published_utc": "2024-02-20T10:45:00Z", "article_url": "", "tickers": ["AAPL", "KO", "BRK.A", "BRK.B"], "image_url": "", "description": "Warren Buffett's Berkshire Hathaway has more than half of its stock portfolio invested in Apple and Coca-Cola.", "keywords": ["investing"]}, {"id": "aE6I7jULkhZEsKWnVsRNUaPYNfsHBC89wARk4mf6iPQ", "publisher": {"name": "", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "20-Year High Bullish Sentiment Meets Record Tech Allocation: Recipe for Disaster?", "author": "", "published_utc": "2024-02-20T10:24:00Z", "article_url": "", "tickers": ["MSFT", "AAPL", "NVDA", "AMZN", "GOOGL", "META"], "amp_url": "", "image_url": ""}, {"id": "EJIIij7T9dEbvogmzWS_P_1cLvVskocSSLrM3p2V_qE", "publisher": {"name": "", "homepage_url": "", "logo_url": "", "favicon_url": ""}, "title": "3 Wide-Moat Stocks Worth Buying in 2024", "author": "The Tokenist", "published_utc": "2024-02-20T07:09:00Z", "article_url": "", "tickers": ["KO", "GOOGL", "AAPL", "META", "AMZN", "MSFT", "NVDA", "AX", "QTWO", "ADBE", "CRM"], "amp_url": "", "image_url": ""}]
# Convert the news response to JSON array
ticker_news_json = json.loads(ticker_news)
print(f"Total news items: {len(ticker_news_json)}")
Total news items: 10
# Inspect the first news item
news_item = ticker_news_json[0]
print(f"Title: {news_item['title']}")
print(f"Description: {news_item['description']}")
print(f"Publisher: {news_item['publisher']['name']}")
print(f"URL: {news_item['article_url']}")
Title: Should You Worry About Apple's Slowdown in China?
Description: A local smartphone giant is gaining market share.
Publisher: The Motley Fool