Help us shape the future of Go! Take the 2026 Go Developer Survey

openai-cli

module
v1.40.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: Apache-2.0

README

OpenAI CLI

The official command-line tool for the OpenAI REST API.

Install · CLI reference · Guides

OpenAI CLI terminal showing commands for images, files, batches, and JSON output

Call the OpenAI REST API from your terminal or shell scripts. Generate images, upload files, submit batches, and inspect API results without writing an application.

[!NOTE] OpenAI CLI or Codex CLI? Use openai when you want to call a specific API or put API operations in a script. Use Codex CLI (codex) when you want a coding agent to work on a project: read and edit code, run commands, and help implement or debug a change.

Things to try

These examples use an API key; see installation and authentication below.

Generate an image and save it locally

openai images generate --prompt "A tiny orange robot watering a houseplant"

Images are saved to ~/Downloads/gpt-images/, and the command prints their paths. Use --output-dir for an existing destination directory or --name for a filename. Supported terminals can preview the result. See the image guide for editing, model selection, and preview settings.

Upload a file and retrieve it later

openai files upload report.txt --purpose user_data
openai files get file-example
openai files download file-example --output downloaded-report.txt

Use your own local file and replace file-example with the ID returned by the upload. See working with files for more examples.

Run requests in a batch

Submit a JSONL request file, then wait for the batch and download its results:

openai files upload requests.jsonl --purpose batch
openai batches create --input-file-id file-example \
  --endpoint /v1/responses --completion-window 24h
openai batches wait batch_example --output-dir ./results

Replace the file and batch IDs with the returned IDs. The batch guide includes a sample requests.jsonl and explains result files and failed requests.

Export API data for a script

openai models list --format jsonl | jq '.id'
openai files list --format json > files.json

Choose --format json for a JSON document or --format jsonl for records. The default output is readable text, even in pipes. The first example uses jq.

Installation

Installing a prebuilt binary

Download the archive for your operating system and architecture from GitHub Releases. Binaries are available for macOS, Linux, and Windows. Extract the archive and place openai (openai.exe on Windows) in a directory on your PATH.

Installing with Homebrew

brew install openai/tools/openai

Installing with Go

Requires Go 1.26 or later.

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

Once you have run go install, the binary is placed in your Go bin directory:

  • Default location: $HOME/go/bin (or $GOPATH/bin if GOPATH is set)
  • Check your path: Run go env GOPATH to see the base directory

If commands aren't found after installation, add the Go bin directory to your PATH:

# Add to your shell profile (.zshrc, .bashrc, etc.)
export PATH="$PATH:$(go env GOPATH)/bin"

The CLI checks for updates on eligible interactive runs; it never installs them automatically. Set OPENAI_CLI_NO_UPDATE_CHECK=1 to disable checks and notices. See update notices for details.

Authentication

Run the setup guide to enter your API key safely:

openai help setup

For scripts, supply OPENAI_API_KEY through your environment or secret manager. Create a key in API settings.

Try a request:

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

Admin endpoints use OPENAI_ADMIN_KEY, a separate admin API key. For example, retrieve one day of organization usage:

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

Usage

Commands follow the API's resources:

openai <resource> [subresource...] <command> [flags...]

Browse commands and options with help. Help needs no API key or network connection.

openai --help
openai images generate --help
openai admin organization --help

openai help COMMAND and openai COMMAND --help show the same page. Existing colon-separated commands, such as openai admin:organization:usage completions, also work.

Output and errors

Successful JSON responses display readable text by default. Select a data format explicitly when parsing results. Use --transform to extract fields with GJSON syntax:

openai files get file-example --transform filename --raw-output

Errors go to stderr as readable summaries. Use --format-error json for API error details, or --transform-error to extract error fields. Explicit --format json, jsonl, raw, and yaml also apply to errors unless --format-error overrides them; --format-error text restores summaries. API error details and --debug logs can contain sensitive data: sanitize them before sharing.

Check the command's exit status before using redirected results; a failed request can leave an incomplete output file. See reading command results for list formats, streaming, binary downloads, and error behavior.

File input

Binary uploads take a plain path. JSON input flags can read a value from a local file with @path:

openai files upload report.txt --purpose user_data
openai responses create --model gpt-5.5 --input @prompt.txt

Use OPENAI_UNTRUSTED_STDIN=1 when piping JSON or YAML from an untrusted source so input cannot cause local file reads. In that mode, provide upload paths explicitly as flags. See file arguments and encoding for escaping literal @ values, JSON/YAML input, and text or base64 encoding.

Shell shortcuts

Set up shell completion to complete commands and file paths. Command insertion lets you search examples with Ctrl+X, then Tab and insert them without running them.

The first eligible interactive run saves image-picker Tab shortcuts for future Bash, zsh, and fish sessions. Open a new terminal to use them. See setup and removal for requirements and opt-out. In PowerShell, press Enter after openai images generate to open the picker.

Guides

Task Guide
Generate, edit, save, and preview images Images
Upload and download files Files
Submit batches and collect results Batches
Generate speech and work with audio Audio
Inspect and count tokens locally Tokenizer
Test webhook handling Webhooks
Parse results in scripts Output formats
Configure headers, mTLS, and file input CLI reference

[!TIP] Managing OpenAI resources with Terraform? Use the official OpenAI Terraform provider to manage projects, service accounts, rate limits, and other supported resources as infrastructure as code.

Development

From a clone of this repository, run the CLI with:

./scripts/run --help

To test against another OpenAI Go SDK version, use ./scripts/link:

./scripts/link github.com/org/repo@version
./scripts/link ../path/to/openai-go

With no arguments, it uses ../openai-go. See CONTRIBUTING.md for setup, checks, and repository structure.

Contributing

Please share bug reports and feature requests through GitHub issues. Pull requests are limited to repository collaborators; we do not accept pull requests from non-collaborators. See CONTRIBUTING.md for the contribution policy and development guide. For security vulnerabilities, follow SECURITY.md.

License

Copyright 2026 OpenAI

This project is licensed under the Apache License 2.0.

Directories

Path Synopsis
cmd
openai command
generated
tokenizer
Code generated by scripts/generate-tokenizer-unicode.go; DO NOT EDIT.
Code generated by scripts/generate-tokenizer-unicode.go; DO NOT EDIT.
internal
app
Package app owns the CLI invocation lifecycle.
Package app owns the CLI invocation lifecycle.
audiohistory
Package audiohistory stores private audio artifacts and creates verified, independent snapshots for replay and export.
Package audiohistory stores private audio artifacts and creates verified, independent snapshots for replay and export.
audiolive
Package audiolive serves a private browser companion for live audio translation.
Package audiolive serves a private browser companion for live audio translation.
audiomedia
Package audiomedia analyzes immutable local audio and owns cancellable playback.
Package audiomedia analyzes immutable local audio and owns cancellable playback.
clihelp
Package clihelp owns local onboarding, help-topic routing, and safe command recovery.
Package clihelp owns local onboarding, help-topic routing, and safe command recovery.
commands
Package commands shapes the CLI command tree and defines shared contracts for handwritten features.
Package commands shapes the CLI command tree and defines shared contracts for handwritten features.
commands/admin
Package admin owns admin credential requirements and secure interactive setup.
Package admin owns admin credential requirements and secure interactive setup.
commands/audio
Package audio owns speech controls, local audio history commands, voice creation, and transcript/speech response handling.
Package audio owns speech controls, local audio history commands, voice creation, and transcript/speech response handling.
commands/files
Package files owns handwritten file workflows and their result presentation.
Package files owns handwritten file workflows and their result presentation.
commands/images
Package images owns handwritten image workflows, saving, previews, and picker state.
Package images owns handwritten image workflows, saving, previews, and picker state.
commands/models
Package models owns model-list selection and interactive presentation.
Package models owns model-list selection and interactive presentation.
commands/webhooks
Package webhooks owns guided webhook creation and webhook result guidance.
Package webhooks owns guided webhook creation and webhook result guidance.
commandsuggest
Package commandsuggest records commands explicitly authored by CLI presenters.
Package commandsuggest records commands explicitly authored by CLI presenters.
forminput
Package forminput provides value-only text editing and ordered form navigation.
Package forminput provides value-only text editing and ordered form navigation.
imagefont
Package imagefont builds bitmap fonts for terminal image rendering.
Package imagefont builds bitmap fonts for terminal image rendering.
imagefontmac
Package imagefontmac connects inline images to macOS's font APIs.
Package imagefontmac connects inline images to macOS's font APIs.
imagegallery
Package imagegallery stores private revision metadata and immutable typography fonts for terminal image scrollback.
Package imagegallery stores private revision metadata and immutable typography fonts for terminal image scrollback.
imagemodels
Package imagemodels maintains the known image model catalog and checks model metadata visibility without downloading the full account model catalog.
Package imagemodels maintains the known image model catalog and checks model metadata visibility without downloading the full account model catalog.
imageoutput
Package imageoutput saves base64 Images API responses to local image files.
Package imageoutput saves base64 Images API responses to local image files.
imageprefs
Package imageprefs stores only the automatic image-preview preference.
Package imageprefs stores only the automatic image-preview preference.
output
Package output provides shared output writing and failure classification.
Package output provides shared output writing and failure classification.
output/listview
Package listview provides lazy terminal list navigation and loaded-page rendering.
Package listview provides lazy terminal list navigation and loaded-page rendering.
output/transform
Package transform contains CLI-owned response transformations.
Package transform contains CLI-owned response transformations.
readable
Package readable renders API values as plain, labeled text.
Package readable renders API values as plain, labeled text.
terminal
Package terminal contains shared terminal behavior and platform adapters.
Package terminal contains shared terminal behavior and platform adapters.
terminalimage
Package terminalimage writes native images, image fonts, or color-block previews.
Package terminalimage writes native images, image fonts, or color-block previews.
testutil/console
Package console provides synthetic console records and synchronized observer frames for native Windows feature tests.
Package console provides synthetic console records and synchronized observer frames for native Windows feature tests.
testutil/fileperms
Package fileperms checks private file permissions in native-platform tests.
Package fileperms checks private file permissions in native-platform tests.
testutil/mtls
Package mtls provides synthetic certificate fixtures for mTLS tests.
Package mtls provides synthetic certificate fixtures for mTLS tests.
testutil/socket
Package socket provides borrowed-descriptor fixtures for input contracts.
Package socket provides borrowed-descriptor fixtures for input contracts.
tokenizer
Package tokenizer adapts embedded text encodings for local CLI token inspection.
Package tokenizer adapts embedded text encodings for local CLI token inspection.
updatenotice
Package updatenotice checks public releases and throttles optional update notices.
Package updatenotice checks public releases and throttles optional update notices.
windowsfile
Package windowsfile handles private creation and non-following Windows file opens.
Package windowsfile handles private creation and non-following Windows file opens.
scripts
demos command
demo-api serves fixed synthetic responses for local CLI recordings.
demo-api serves fixed synthetic responses for local CLI recordings.
demos/batches command
batches-demo-api serves synthetic lifecycle and file data on loopback only.
batches-demo-api serves synthetic lifecycle and file data on loopback only.
demos/files-workflow command
files-workflow-demo-api accepts only the fixed synthetic Files workflow.
files-workflow-demo-api accepts only the fixed synthetic Files workflow.
demos/global-flags command
global-flags-demo-api reflects a synthetic request's project header in model data.
global-flags-demo-api reflects a synthetic request's project header in model data.
demos/image-models command
image-model-demo-api serves synthetic metadata only.
image-model-demo-api serves synthetic metadata only.
demos/list-navigation command
list-navigation-demo-api serves two fixed synthetic file pages.
list-navigation-demo-api serves two fixed synthetic file pages.
demos/list-tables command
list-table-demo-api serves fixed synthetic list pages on loopback only.
list-table-demo-api serves fixed synthetic list pages on loopback only.
demos/output command
output-demo-api serves only fixed synthetic model and stream responses.
output-demo-api serves only fixed synthetic model and stream responses.
demos/readable-audio command
audio-demo-api serves a fixed, delayed synthetic transcription event stream.
audio-demo-api serves a fixed, delayed synthetic transcription event stream.
demos/shell-files command
Synthetic loopback API for the shell/file comparison recording.
Synthetic loopback API for the shell/file comparison recording.
demos/streaming-text command
streaming-demo-api serves a fixed, delayed synthetic Responses event stream.
streaming-demo-api serves a fixed, delayed synthetic Responses event stream.
render-linux-picker command
Render the package-owned fish startup file without initializing the CLI.
Render the package-owned fish startup file without initializing the CLI.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL