OpenAI CLI: Run OpenAI API Calls from Terminal

Use OpenAI CLI to call the OpenAI REST API from terminal commands, scripts, files, and admin workflows.

The OpenAI CLI is the official command-line interface for the OpenAI REST API, written in Go and published by OpenAI as an open-source project. It enables your terminal to access OpenAI endpoints for text generation, file management, audio, images, and organization-level usage reporting.

You can use the CLI to send requests, inspect responses, and pull admin-level usage data from a shell. It can be useful for testing model behavior, scripting automated API workflows, and exploring endpoints before embedding them into a full application.

Features

  • Accepts text input and file arguments for any supported API endpoint.
  • Outputs results in seven configurable formats: auto, explore, json, jsonl, pretty, raw, and yaml.
  • Supports GJSON syntax for transforming output data directly in the terminal.
  • Passes files to the API using @filename syntax, with automatic filetype detection for text versus binary.
  • Accepts both a standard API key and a separate admin API key for organization-level endpoints.
  • Debug mode logs full HTTP request and response bodies.
  • Supports a custom base URL for testing against alternate API backends.
  • All credentials load from environment variables or inline flags.

OpenAI CLI vs OpenAI Codex CLI

OpenAI CLI and Codex CLI solve different developer problems. OpenAI CLI is the better fit when you want direct API access from shell commands. Codex CLI is the better fit when you want an AI coding agent that can inspect a project, edit code, and run terminal workflows.

CategoryOpenAI CLIOpenAI Codex CLI
Main purposeAPI calls from the terminal.Agentic coding from the terminal.
Best use caseScripts, API requests, admin usage checks, file-based API workflows.Code edits, repository inspection, code review, and terminal coding workflows.
Primary commandopenaicodex
InstallationHomebrew or Go install.npm, Homebrew cask, or release binary.
AuthenticationAPI key or admin API key.ChatGPT sign-in or API-key setup.
Coding agent behaviorNo built-in repository agent workflow.Runs as a local coding agent.
Output controlOutput formats and GJSON transforms.Agent sessions, code changes, and local workflow control.
Best reader choiceUse it to call OpenAI APIs directly.Use it to work on code with an AI agent.

How to Use It

1. Install OpenAI CLI with Homebrew:

brew install openai/tools/openai

2. OR install OpenAI CLI with Go:

go install 'github.com/openai/openai-cli/cmd/openai@latest'

Go places the binary in the Go bin directory. The default location is $HOME/go/bin, unless GOPATH changes the location.

go env GOPATH

A missing openai command usually means the Go bin directory needs PATH access.

export PATH="$PATH:$(go env GOPATH)/bin"

3. Set an OpenAI API key:

export OPENAI_API_KEY="sk-..."

4. Run a Responses API request:

openai responses create \
  --input "Say this is a test" \
  --model gpt-5.5

5. Set an admin API key:

export OPENAI_ADMIN_KEY="sk-admin-..."

6. Query organization usage:

openai admin:organization:usage completions \
  --start-time 1735689600 \
  --end-time 1735776000 \
  --bucket-width 1d

7. Read command-specific help:

openai --help
openai responses create --help

8. Pass a file as an argument

openai <command> --arg @abe.jpg

9. Pass a file inside JSON:

openai <command> --arg '{image: "@abe.jpg"}'

10. Pass a file inside YAML:

openai <command> <<YAML
arg:
  image: "@abe.jpg"
YAML

11. Escape an @ string:

openai <command> --username '\@abe'

12. Use explicit file encoding:

openai <command> --arg @file://myfile.txt

The @data:// syntax sends a file as base64-encoded data.

openai <command> --arg @data://file.txt

13. Run the project locally:

./scripts/run args...

Technical Reference

Basic Commands

SyntaxPurpose
openai [resource] <command> [flags...]Runs a command against an API resource.
openai --helpShows top-level command help.
openai [resource] <command> --helpShows command-specific help.
openai --versionShows the installed CLI version.
openai -vShows the installed CLI version.

Installation Commands

MethodCommandRequirement
Homebrewbrew install openai/tools/openaiHomebrew installed.
Gogo install 'github.com/openai/openai-cli/cmd/openai@latest'Go 1.25 or later.
Local repository run./scripts/run args...Cloned project repository.

Authentication Variables

Environment variableRequiredDefault valuePurpose
OPENAI_API_KEYNonullStandard API authentication.
OPENAI_ADMIN_KEYNonullAdmin endpoint authentication.
OPENAI_ORG_IDNonullOrganization selection.
OPENAI_PROJECT_IDNonullProject selection.
OPENAI_WEBHOOK_SECRETNonullWebhook verification.

Global Flags

FlagEnvironment variablePurpose
--api-keyOPENAI_API_KEYSets the standard API key.
--admin-api-keyOPENAI_ADMIN_KEYSets the admin API key.
--organizationOPENAI_ORG_IDSets the organization.
--projectOPENAI_PROJECT_IDSets the project.
--webhook-secretOPENAI_WEBHOOK_SECRETSets the webhook secret.
--helpNoneShows command-line usage.
--debugNoneEnables HTTP request and response debug logging.
--versionNoneShows the CLI version.
-vNoneShows the CLI version.
--base-urlNoneUses a custom API backend URL.
--formatNoneChanges the normal output format.
--format-errorNoneChanges the error output format.
--transformNoneTransforms normal output with GJSON syntax.
--transform-errorNoneTransforms error output with GJSON syntax.

Output Formats

FormatUse
autoLets the CLI choose an output format.
exploreUses an exploratory output view.
jsonOutputs JSON.
jsonlOutputs JSON Lines.
prettyOutputs readable formatted text.
rawOutputs raw response data.
yamlOutputs YAML.

File Argument Syntax

SyntaxPurpose
@myfile.extPasses a local file as an argument.
@abe.jpgPasses an image file.
{image: "@abe.jpg"}Passes a file reference inside JSON-style input.
'\@abe'Sends a literal string that starts with @.
@file://myfile.txtSends a file as plain text.
@data://file.txtSends a file as base64-encoded data.
@file:///tmp/file.txtSends an absolute path as plain text.
@data:///tmp/file.datSends an absolute path as base64-encoded data.

Development Linking Commands

The ./scripts/link script links the CLI against another OpenAI Go SDK version during development.

./scripts/link github.com/org/repo@version

A local SDK copy can also serve as the linked target.

./scripts/link ../path/to/openai-go

The script defaults to ../openai-go when no argument is supplied.

./scripts/link

Alternatives and Related Resources

Pros

  • Open-source under OpenAI.
  • Installs via Homebrew or Go.
  • Seven output formats, including JSONL and YAML.
  • GJSON support for inline output transformation.
  • Debug mode for full HTTP request inspection.
  • Supports both standard and admin API keys.

Cons

  • No GUI or browser interface.
  • Not a coding agent.
  • Does not read or write local files on its own.
  • Admin endpoints need a separate admin API key.

FAQs

Q: Is the OpenAI CLI free?
A: The CLI is free and open-source. API usage is billed per token to your OpenAI account.

Q: Does the OpenAI CLI require a signup?
A: An OpenAI account and a valid API key from the OpenAI platform are required.

Q: What is the difference between the OpenAI CLI and the OpenAI Codex CLI?
A: The OpenAI CLI is an API wrapper for sending requests, managing resources, and pulling usage stats from the terminal. The Codex CLI is an agentic coding tool that reads and edits files on your local machine in response to natural language prompts. They are separate projects with different purposes.

Q: Does the OpenAI CLI run on Windows?
A: The Go-based install works on any platform Go supports, including Windows, macOS, and Linux. The Homebrew method is macOS and Linux only.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest & top AI tools sent directly to your email.

Subscribe now to explore the latest & top AI tools and resources, all in one convenient newsletter. No spam, we promise!