Skip to main content
NJannasch.Dev

Prototyping Shareable CLIs Fast with Python and Typer

· 2 min read
PythonREST APICLI

I had a habit of writing quick Python scripts to automate things — API calls, bulk operations, data fetching. They worked fine for me. Sharing them with colleagues was always the painful part. They’d need to know the exact argument names, remember the flag order, or dig through the code to figure out what options existed.

My solution for a while was argparse. It works, but the setup is boilerplate-heavy for what should be a 10-line task.

Then I found typer.

The argparse version

A script that fetches users from an API endpoint:

import argparse
import requests

def get_users(endpoint):
    response = requests.get(endpoint)
    for user in response.json().get('users', []):
        print(user['firstName'])

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Retrieve users from API.")
    parser.add_argument('--endpoint', type=str, default="https://dummyjson.com/users")
    args = parser.parse_args()
    get_users(args.endpoint)

The argparse setup is almost as long as the actual logic. And --help output is only as good as whatever you remembered to write in the description strings.

The typer version

import typer
import requests

app = typer.Typer()

@app.command()
def get_users(endpoint: str = "https://dummyjson.com/users"):
    """Fetch users from a specified API endpoint."""
    response = requests.get(endpoint)
    for user in response.json().get('users', []):
        print(user['firstName'])

if __name__ == "__main__":
    app()

Same logic. The type annotation endpoint: str becomes the CLI argument. The docstring becomes the help text. That’s the whole interface definition.

Running python script.py --help:

Usage: script.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  get-users  Fetch users from a specified API endpoint.

Why this mattered

The person running my script doesn’t need to read the source to know what arguments exist. --help just works. Type errors get caught before anything runs. Defaults are visible in the signature.

For internal tooling — scripts that hit APIs, transform data, kick off bulk operations — this made sharing fast. Write the logic, annotate the types, hand it to a colleague. They can actually use it without you standing next to them.

Typer is by the same author as FastAPI and the pattern is deliberately similar: decorators and type hints do the heavy lifting. If you write FastAPI endpoints, the CLI pattern feels immediately familiar.

For anything beyond a one-off throwaway script, I reach for typer now.

The views and opinions expressed here are my own and do not reflect those of my employer.