Python Beginner

Company Lookup CLI Tool

Build a command-line tool to look up SEC company profiles and search for companies by name or ticker.

Note: The code examples in this tutorial have not yet been verified against the live API. If you encounter issues, please let us know.

Prerequisites

  • Python 3.8 or later
  • The requests library (pip install requests)
  • A stockdata.dev API key — get one free

The free tier includes 1,000 API calls per month, which is plenty for building and testing CLI tools.

The Code

Create a file called company.py. This script has two modes: look up a company by ticker, or search for companies by name.

Python
import sys
import requests

API_KEY = "your_api_key_here"
BASE_URL = "https://api.stockdata.dev/v1"
HEADERS = {"X-API-Key": API_KEY}


def get_company(ticker):
    """Fetch and display a company profile by ticker."""
    resp = requests.get(
        f"{BASE_URL}/company/{ticker}",
        headers=HEADERS,
    )
    if resp.status_code != 200:
        print(f"Error: {resp.json().get('error', resp.text)}")
        return

    c = resp.json()
    print(f"Company: {c['name']} ({c['ticker']})")
    print(f"CIK:     {c['cik']}")
    print(f"Exchange: {c['exchange']}")
    print(f"SIC:     {c['sic_code']} - {c['sic_description']}")
    print(f"State:   {c['state']}")
    print(f"Fiscal Year End: {c['fiscal_year_end']}")


def search_companies(query):
    """Search for companies by name or ticker."""
    resp = requests.get(
        f"{BASE_URL}/search",
        headers=HEADERS,
        params={"q": query},
    )
    if resp.status_code != 200:
        print(f"Error: {resp.json().get('error', resp.text)}")
        return

    results = resp.json()
    print(f'Results for "{query}":')
    for r in results:
        print(f"  {r['ticker']:<6}{r['name']}")


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python company.py AAPL")
        print("       python company.py --search \"apple\"")
        sys.exit(1)

    if sys.argv[1] == "--search":
        search_companies(sys.argv[2])
    else:
        get_company(sys.argv[1].upper())

How It Works

The script uses two API endpoints:

Company Profile: GET /v1/company/{ticker}

This endpoint returns a single company's SEC registration details. The response includes the company name, CIK (Central Index Key, the SEC's unique identifier), stock exchange, SIC industry code, incorporation state, and fiscal year end month.

HTTP
GET https://api.stockdata.dev/v1/company/AAPL
X-API-Key: your_api_key_here

Search: GET /v1/search?q={query}

The search endpoint matches against both company names and tickers. It returns a list of matching companies, which is useful when you know a company name but not its ticker symbol.

HTTP
GET https://api.stockdata.dev/v1/search?q=apple
X-API-Key: your_api_key_here

Running It

Install the dependency and run the script:

Shell
pip install requests

Look up a company

Shell
$ python company.py AAPL

Company: Apple Inc. (AAPL)
CIK:     0000320193
Exchange: Nasdaq
SIC:     3571 - Electronic Computers
State:   CA
Fiscal Year End: September

Search for companies

Shell
$ python company.py --search "apple"

Results for "apple":
  AAPL  Apple Inc.
  APLE  Apple Hospitality REIT Inc.

Enhancements

Here are a few ways to extend this tool:

  • Environment variable for API key — Read the key from an STOCKDATA_API_KEY environment variable instead of hardcoding it. Use os.environ.get("STOCKDATA_API_KEY").
  • More fields — The company endpoint returns additional fields like category, entity_type, and addresses. Add flags like --verbose to display them.
  • JSON output — Add a --json flag that prints the raw API response, useful for piping into jq or other tools.
  • Batch lookups — Accept multiple tickers (python company.py AAPL MSFT GOOG) and display each company's profile in sequence.

Ready to build?

Get your free API key and start coding in minutes.

Get Free API Key