4.7 KiB
Installing OpenAPI CLI
You can install and use OpenAPI CLI in several ways:
Installing globally
npm
:::success Tip This is the recommended method :::
npm i -g @redocly/openapi-cli@latest
npm i -g @redocly/openapi-cli@latest
added 1 package, removed 1 package, changed 87 packages, and audited 89 packages in 4s
found 0 vulnerabilities
yarn
yarn global add @redocly/openapi-cli
yarn global add @redocly/openapi-cli
yarn global v1.22.11
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 🔨 Building fresh packages...
success Installed "@redocly/openapi-cli@1.0.0-beta.54" with binaries:
- openapi
✨ Done in 6.90s.
Verifying global installation
After installing it globally, run openapi --version to confirm that the installation was successful:
openapi --version
1.0.0-beta.54
Installing in runtime
npx
:::info
npx is npm's package runner. It will install and run a command at the same time without installing it globally.
:::
npx @redocly/openapi-cli <command> [options]
npx @redocly/openapi-cli@latest lint petstore.yaml
npx @redocly/openapi-cli@latest lint petstore.yaml
No configurations were defined in extends -- using built in recommended configuration by default.
validating petstore.yaml...
[1] petstore.yaml:3:3 at #/info
Info object should contain `description` field.
1 | swagger: "2.0"
2 | info:
3 | version: 1.0.0
4 | title: Swagger Petstore
5 | license:
6 | name: MIT
7 | host: petstore.swagger.io
8 | basePath: /v1
Warning was generated by the info-description rule.
[2] petstore.yaml:6:5 at #/info/license
License object should contain `url` field.
4 | title: Swagger Petstore
5 | license:
6 | name: MIT
7 | host: petstore.swagger.io
8 | basePath: /v1
Warning was generated by the info-license-url rule.
petstore.yaml: validated in 70ms
Woohoo! Your OpenAPI definition is valid. 🎉
You have 2 warnings.
npx @redocly/openapi-cli@latest lint petstore.yaml
No configurations were defined in extends -- using built in recommended configuration by default.
validating petstore.yaml...
petstore.yaml: validated in 43ms
Woohoo! Your OpenAPI definition is valid. 🎉
Docker
To give the Docker container access to the OpenAPI definition files, you need to mount the containing directory as a volume. Assuming the OAS definition is rooted in the current working directory, the command will be the following:
docker run --rm -v $PWD:/spec redocly/openapi-cli lint petstore.yaml
Unable to find image 'redocly/openapi-cli:latest' locally
latest: Pulling from redocly/openapi-cli
540db60ca938: Pull complete
fe289da0f830: Pull complete
c5a1e48e74e5: Pull complete
63ccc02b1289: Pull complete
4b42ba54a874: Pull complete
3f76218e8e91: Pull complete
Digest: sha256:fa95c9a8b9e282b21779e8ffa86ed28b87f9d97517e46d96af7a6676d47b0430
Status: Downloaded newer image for redocly/openapi-cli:latest
No configurations were defined in extends -- using built in recommended configuration by default.
validating petstore.yaml...
petstore.yaml: validated in 51ms
Woohoo! Your OpenAPI definition is valid. 🎉
Post-installation
Setting up tab completion
To set up tab completion for the terminal, generate the completion script with the command:
openapi completion
###-begin-openapi-completions-###
#
# yargs command completion script
#
# Installation: /Users/gold/.npm/_npx/b36cbb32e525fe97/node_modules/.bin/openapi completion >> ~/.bashrc
# or /Users/gold/.npm/_npx/b36cbb32e525fe97/node_modules/.bin/openapi completion >> ~/.bash_profile on OSX.
#
_openapi_yargs_completions()
{
local cur_word args type_list
cur_word="${COMP_WORDS[COMP_CWORD]}"
args=("${COMP_WORDS[@]}")
# ask yargs to generate completions.
type_list=$(/Users/gold/.npm/_npx/b36cbb32e525fe97/node_modules/.bin/openapi --get-yargs-completions "${args[@]}")
COMPREPLY=( $(compgen -W "${type_list}" -- ${cur_word}) )
# if no match was found, fall back to filename completion
if [ ${#COMPREPLY[@]} -eq 0 ]; then
COMPREPLY=()
fi
return 0
}
complete -o default -F _openapi_yargs_completions openapi
###-end-openapi-completions-###
The command output contains installation instructions. For example, to install the completion script in bash, use:
openapi completion >> ~/.bashrc
The approach is similar for other shells. After the installation, restart your terminal for changes to take effect.