Skip to main content

api scripts

Runs an Optic Script with the current specification. Scripts are defined in the optic.yml file and allow you to build integration workflows with other tools via OpenAPI exports. Scripts may manage dependencies as well. It takes the name of the Script task to run and has an optional flag to install dependencies if necessary.

Flags & Options#

  • --install will install any declared dependencies that are missing, using the provided command. By default, if a dependency is missing the Script task will stop and ask you to use the --install flag to proceed.

While running commands, the environment variables OPENAPI_JSON and OPENAPI_YAML are both available. They will contain the value of the file location of the JSON and YAML versions of the generated OpenAPI file. You can use this to populate parameters in your command.

Examples#

Run a script#

api scripts
[optic] Found Script export-spec
[optic] Generated OAS files {...}/todo-js/.optic/generated/openapi.json[optic] {...}/todo-js/.optic/generated/openapi.yamlRunning command: { command parameter from the script definition in optic.yml }

Install pre-requisites then run a script#

For a script with dependencies, Optic will check that they are present before running the script. If not, they must be installed. The following example installs dependencies with the install parameter of the script configuration in optic.yml:

api scripts export-spec --install
[optic] Found Script export-specChecking bin dependencies Requiring ["someprogram","anotherprogram"]... Missing dependencies[optic] Some bin dependencies are missing ["anotherprogram"]. falseRunning install command: { install command } ...Running install command: { install command } ... Success!
[optic] Generated OAS files {...}/todo-js/.optic/generated/openapi.json[optic] {...}/todo-js/.optic/generated/openapi.yamlRunning command: { command parameter from the script definition in optic.yml }
{ command output ... }

Errors#

If dependencies are declared for the script with the dependsOn parameter, and the dependencies cannot be found, Optic will prompt you to run the scripts command with the --install flag. This will run the command in the install parameter provided in the script definition.

api scripts export-spec
[optic] Found Script export-specChecking bin dependencies Requiring ["someprogram","anotherprogram"]... Missing dependencies[optic] Some bin dependencies are missing ["anotherprogram"]. Run the command again with the flag '--install' to install them

Issues & Contributing#

Having trouble? Open an issue on GitHub

Have an idea for changing this command? Here's where it lives in the repo. PRs welcome!

Need help or want to talk with us about a use case? Join Contributing on Discord