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.
python -m venv .venv
source .venv/bin/activate
pip install --upgrade openai
openai --versionAuthenticate 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.
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.
openai api models.list | head -n 20If 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.listcall before building anything larger.
With authentication confirmed, you're ready to make your first real request — which we'll cover next.