← All articles
AI Tools1 min read

Getting Started with the OpenAI CLI

Set up the OpenAI command-line tools, authenticate securely, and confirm everything works before you write a single line of application code.

S

Swapnika Voora

Author

Before building anything on OpenAI's models, it's worth getting the command-line workflow right. A clean setup means reproducible calls, safe credentials, and a fast feedback loop from your terminal.

Install the tools

The Python SDK ships a CLI, and the tooling installs from pip. Use a virtual environment so it doesn't pollute your system Python.

install.sh
python -m venv .venv
source .venv/bin/activate
pip install --upgrade openai
openai --version

Authenticate with an environment variable

Never hard-code keys. The CLI and SDK read OPENAI_API_KEY from the environment, which keeps secrets out of your shell history and source.

auth.sh
export OPENAI_API_KEY="sk-..."      # add to your shell profile or a .env
echo "$OPENAI_API_KEY" | head -c 6  # sanity check, prints "sk-..."

For projects, put the key in a .env file that is listed in .gitignore and load it with a tool like direnv or dotenv.

Verify the connection

A quick list call confirms your key works and shows which models you can access.

verify.sh
openai api models.list | head -n 20

If you see a JSON list of model ids, you're connected. A 401 means the key is missing or wrong.

Takeaways

  • Install into a virtual environment to keep things isolated.
  • Store the key in OPENAI_API_KEY, never in code or version control.
  • Verify with a models.list call before building anything larger.

With authentication confirmed, you're ready to make your first real request — which we'll cover next.

#openai#cli#ai#setup

More in AI Tools