Reusable requests
Commands for managing reusable requests in the Knock CLI.
Reusable request commands enable you to manage reusable requests in your Knock account from the CLI.
A reusable request is a versioned resource. Pushing one upserts it in the target environment, and you commit and promote that change the same way you do for a workflow. knock pull and knock push include reusable requests, and they push reusable requests before workflows that reference them.
Reusable request file structure
#When reusable requests are pulled from Knock, they are stored in directories named by their reusable request key. Each directory contains a reusable_request.json file. The key is the directory name. Pulled files store it under __readonly.key.
reusable-requests/ āāā fetch-user/ āāā reusable_request.json
reusable_request.json describes the request:
- name. The human-readable name.
- description. Optional context about what the request does.
- type.
http_fetch. This is the only supported type. - settings. The HTTP configuration:
method(get,post,put,delete, orpatch),url, and optionalbody,headers,query_params,response_path, andsigning_key. - settings.data_input_schema. Optional JSON Schema for request inputs.
- settings.data_input_values_str. Optional preview input values, as a JSON string.
- conditions. Optional conditions that decide whether the request runs.
A reusable request key must be 3 to 255 characters and use only lowercase letters, numbers, dashes, or underscores.
If you're migrating your local reusable request files into Knock, arrange them using the example file structure above and then push them into Knock with knock reusable-request push --all.
knock reusable-request list [flags]
#Display all reusable requests for an environment. Use an --environment flag to specify the target environment; if omitted, the Knock CLI uses the account's default environment.
Flags
The environment to use. When omitted, the account's default environment is used.
The branch to use. Defaults to the main branch.
Should any uncommitted changes be hidden? Defaults to false.
The total number to fetch per page.
Fetches all entries after this cursor.
Fetches all entries before this cursor.
Format output as json.
knock reusable-request get <reusable_request_key> [flags]
#Display a single reusable request from an environment.
Use an --environment flag to specify the target environment; if omitted, the Knock CLI uses the account's default environment.
Flags
The environment to use. When omitted, the account's default environment is used.
The branch to use. Defaults to the main branch.
Should any uncommitted changes be hidden? Defaults to false.
Format output as json.
knock reusable-request new [flags]
#Create a new reusable request with a minimal configuration.
The command creates a new reusable request directory in your local file system. By default, this will be in the reusable requests resource directory set by your knock.json file, or the current working directory if not configured.
When --name or --key is omitted, the command prompts for them. --type defaults to http_fetch, --method defaults to get, and --url defaults to https://example.com.
Flags
The name of the reusable request. If not provided, will be prompted.
The key of the reusable request. If not provided, will be prompted and defaults to a slug of the name.
The reusable request type. Only http_fetch is supported. Defaults to http_fetch.
The description of the reusable request.
The HTTP method of the request. One of: get, post, put, delete, patch. Defaults to get.
The URL of the request. Defaults to https://example.com.
The environment to use. When omitted, the account's default environment is used.
The branch to use. Defaults to the main branch.
Push the reusable request to Knock after creating it locally. Defaults to false.
Overwrites an existing reusable request directory without prompting for confirmation. Defaults to false.
knock reusable-request pull <reusable_request_key> [flags]
#Pull one or more reusable requests from an environment into a local file system. Knock CLI will create a new reusable request directory or update the existing reusable request directory in the local file system.
By default this command will resolve to the reusable requests resource directory via your knock.json file. When not set, will use the current working directory as the default. In the case of the --all flag, the target directory path will be resolved via your knock.json file or the --reusable-requests-dir flag.
Note: if pulling the target reusable request for the first time (or all reusable requests), Knock CLI will ask to confirm before writing to the local file system.
See the Reusable request file structure section for details on how reusable request files are organized.
Flags
The environment to use. When omitted, the account's default environment is used.
The branch to use. Defaults to the main branch.
Whether to pull all reusable requests from the specified environment into the target directory path set by --reusable-requests-dir. Defaults to false.
When used, all contents in the target directory will be erased and replaced with all reusable requests from the specified environment.
Specifies which target directory path to pull all reusable requests into. Only available to be used with --all, and defaults to the current working directory.
Should any uncommitted changes be hidden? Defaults to false.
Removes the confirmation prompt. Defaults to false.
Format output as json.
knock reusable-request push <reusable_request_key> [flags]
#Push one or more reusable requests from a local file system to Knock. Knock will update an existing reusable request by the matching key, or create a new reusable request if it does not exist yet.
By default this command will resolve to the reusable requests resource directory via your knock.json file. When not set, will use the current working directory as the default. In the case of the --all flag, the target directory path will be resolved via your knock.json file or the --reusable-requests-dir flag.
Note:
- You must be directly above the target reusable request directory when running the
reusable-request pushcommand, so the CLI can locate thereusable_request.jsonfile. - You can also pass in the
--commitflag (with an optional--commit-messageflag) to commit the upserted changes. - You can pass
--allow-emptywith--committo create an empty commit when re-upserting an unchanged resource. See empty commits for more on when and why to use this.
See the Reusable request file structure section for details on how reusable request files are organized.
Flags
The environment to use. When omitted, the account's default environment is used.
The branch to use. Defaults to the main branch.
Push and commit the reusable request(s) at the same time. Defaults to false.
The commit message to pass when using the --commit flag.
Only meaningful with --commit. When the resource is already published with no unpublished changes, create an empty commit instead of a no-op publish. Defaults to false.
Whether to push all reusable requests from the target directory path set by --reusable-requests-dir. Defaults to false.
Specifies the target directory path to find and push all reusable requests from. Only available to be used with --all, and defaults to the current working directory.
Force pushes the resource or resources to Knock, overwriting whatever is currently stored. If you're using this on a non-development environment, you should also ensure you commit the changes. Defaults to false.
Format output as json.
knock reusable-request validate <reusable_request_key> [flags]
#Validate one or more reusable requests from a local file system. Knock will validate the given reusable request payload in the same way as it would with the reusable-request push command, except without persisting those changes.
Flags
The environment to use. When omitted, the account's default environment is used.
The branch to use. Defaults to the main branch.
Whether to validate all reusable requests from the target directory path set by --reusable-requests-dir. Defaults to false.
Specifies the target directory path to find and validate all reusable requests from. Only available to be used with --all, and defaults to the current working directory.
Format output as json.
knock reusable-request archive <reusable_request_key> [flags]
#Archive a reusable request in every environment. To restore it, unarchive the reusable request.
If a local copy of the reusable request exists in <knock dir>/reusable-requests/<reusable_request_key>, or in the directory you pass with --reusable-requests-dir, the command removes that directory so a later push doesn't create the reusable request again. Any changes in it that you haven't pushed are lost.
If you've enabled environments and you're working on a branch, exit the branch with knock branch exit first. Archiving can't run on a branch, and the command exits with an error.
The command asks you to confirm before it archives. Pass --force to skip the prompt. You must pass it when you use --json or run the command outside an interactive terminal, such as in CI. Otherwise the command exits with an error.
Flags
The environment to run the command in. The reusable request is archived in every environment.
The directory to look in for a local copy of the reusable request. Defaults to the reusable-requests directory in your knock dir.
Skips the confirmation prompt. Required when using --json or running outside an interactive terminal. The command still removes a local copy of the reusable request if it finds one. Defaults to false.
Format output as json. The output includes local_dir_removed, the path of the local directory the command removed, when it removed one.
knock reusable-request unarchive <reusable_request_key> [flags]
#Restore an archived reusable request by its original key. Unarchiving restores the reusable request in every environment it was archived from. If the key was archived more than once, the command restores the most recent archive.
If a live reusable request uses the same key, archive it before you unarchive the archived one.
Unarchiving doesn't create a local copy. Pull the reusable request to get one.
If you've enabled environments and you're working on a branch, exit the branch with knock branch exit first. Unarchiving can't run on a branch, and the command exits with an error.
Flags
The environment to run the command in. The reusable request is restored in every environment it was archived from.
Format output as json.
knock reusable-request discard <reusable_request_key> [flags]
#Discard the uncommitted changes to a reusable request, returning it to its last committed version. The command changes only the environment or branch you pass.
The command asks you to confirm before it discards. Pass --force to skip the prompt. You must pass it when you use --json or run the command outside an interactive terminal, such as in CI. Otherwise the command exits with an error.
Flags
The environment to discard changes in.
The branch to use. Defaults to the main branch.
Skips the confirmation prompt. Required when using --json or running outside an interactive terminal. Defaults to false.
Format output as json.