Finnhub Python API Client
finnhub-python is the official Python client for the Finnhub API, providing institutional-grade financial data. This includes real-time stock prices, global fundamentals, global ETF holdings, and alternative data. The library is actively maintained, with regular updates and a current version of 2.4.27, offering a comprehensive interface for financial market data access.
Common errors
-
AttributeError: partially initialized module 'finnhub' has no attribute 'Client' (most likely due to a circular import)
cause The Python script attempting to import `finnhub` is itself named `finnhub.py`, causing Python to import the local file instead of the installed library.fixRename your Python script file to something unique, like `stock_data_fetcher.py`, to resolve the naming conflict. -
{'error': "You don't have access to this resource."}cause Attempting to access a Finnhub API endpoint or data type that is not included in your current subscription plan (e.g., trying to fetch premium data with a free API key).fixVerify your Finnhub API key and its associated subscription plan. Consult the Finnhub API documentation to confirm the required subscription tier for the specific endpoint you are trying to use. -
HTTPSConnectionPool(host='api.finnhub.io', port=443): Max retries exceeded with url: /api/v1/...
cause This error typically indicates network issues, a very aggressive rate limit being hit repeatedly without backoff, or firewall/proxy blocking the outbound connection.fixFirst, check your internet connection. If the issue persists, ensure your API key is valid and you're not exceeding rate limits. If running in a restricted environment, check firewall rules or ensure the `api.finnhub.io` domain is whitelisted. -
{'error': 'The API key is invalid.'}cause The API key provided to the `finnhub.Client` is incorrect, expired, or has leading/trailing whitespace.fixDouble-check your Finnhub API key in your dashboard (`finnhub.io/dashboard`). Ensure there are no extra spaces or characters. If using environment variables, confirm it's loaded correctly.
Warnings
- breaking Version 2.0.0 introduced significant changes, simplifying library usage and making return data more compatible with data analysis libraries like pandas. Code written for version 1.x.x will likely break.
- gotcha Naming your Python script 'finnhub.py' will lead to a circular import error: 'AttributeError: partially initialized module 'finnhub' has no attribute 'Client''.
- gotcha Free tier API keys often have strict rate limits (e.g., 60 calls/minute) and may not have access to all resources. Exceeding limits results in '429 Too Many Requests', and attempting to access premium data will return 'You don't have access to this resource.'.
- gotcha When deploying applications using `finnhub-python` on certain hosting platforms (e.g., PythonAnywhere), you might encounter '403 Forbidden' errors due to proxy restrictions.
Install
-
pip install finnhub-python
Imports
- Client
from finnhub import Client
import finnhub finnhub_client = finnhub.Client(api_key=os.environ.get('FINNHUB_API_KEY', ''))
Quickstart
import finnhub
import os
# Get API key from environment variable for security
finnhub_api_key = os.environ.get('FINNHUB_API_KEY', 'YOUR_FREE_API_KEY_HERE')
# Setup client
finnhub_client = finnhub.Client(api_key=finnhub_api_key)
# Fetch real-time stock quote for Apple Inc.
quote = finnhub_client.quote('AAPL')
print("AAPL Quote:", quote)
# Fetch company profile for Apple Inc.
company_profile = finnhub_client.company_profile2(symbol='AAPL')
print("AAPL Company Profile:", company_profile)