Keyrunner Scripting Documentation
Getting Started
To get started with Keyrunner scripting, ensure you have the necessary environment setup. Follow these steps to use various functionalities provided by Keyrunner.
Retrieve the value of an environment variable.
await kr.environment.get("<keyName>");Set the value of an environment variable.
await kr.environment.set("<keyName>", "<value>");Get the current request URL.
kr.request.url.get();Set a new request URL.
kr.request.url.set("<newUrl>");Set a request header.
kr.request.headers.set({
key: "<keyName>",
value: "<value>"
});Remove a request header.
kr.request.headers.remove("<keyName>");Get the current request method.
kr.request.method.get();Set a new request method.
kr.request.method.set("POST");Get the response code of the request.
kr.response.code();Get the response headers.
kr.response.headers();Get the response text.
kr.response.text();Get the response in JSON format.
kr.response.json();Log a message to KeyRunner console.
kr.console.log("Hello Keyrunner !!!");Log an error message to KeyRunner console.
kr.console.error("Hello Keyrunner !!!");Send a request using the specified parameters.
var request = {
url: "<request-url>",
method: "POST",
header: {
Accept: "application/json"
},
body: ""
};
kr.sendRequest(request, function(error, response){
if(error){
kr.console.error(error);
}
if(response){
kr.console.log(response);
}
});Collection-Level Scripts
Collections now support Pre-Scripts and Post-Scripts, giving you the ability to run JavaScript logic before or after every request in a collection — without having to duplicate script logic across individual requests.
How it works:
- Open any collection and navigate to the Scripts tab.
- Write a Pre-Script to execute logic before each request in the collection fires (e.g., set auth tokens, inject headers, resolve dynamic variables).
- Write a Post-Script to execute logic after each request completes (e.g., validate response structure, chain data to the next request, log results).
- Collection-level scripts run in addition to any request-level scripts. The execution order is:
- Collection Pre-Script
- Request Pre-Script
- Request executes
- Request Post-Script
- Collection Post-Script
Why it matters: Previously, teams had to copy the same boilerplate auth or token-refresh logic into every single request's script. With collection-level scripts, you write it once at the collection and it applies to all requests inside it automatically.
Reusable Global Script Libraries
KeyRunner now supports a Script Library — a workspace-level store of reusable JavaScript modules that can be imported into any script (collection scripts, request scripts, or other library scripts).
How it works:
- Access the Script Library from the bottom navigation panel.
- Create a new library by clicking the + icon and giving it a name.
- Write your reusable functions inside the library and export them using Node.js module syntax:
// my-library
function getAuthHeader(token) {
return `Bearer ${token}`;
}
module.exports = { getAuthHeader };- To use the library in any script, import it with
kr.require:
const myLib = await kr.require('my-library');
const header = myLib.getAuthHeader(kr.environment.get('token'));
kr.request.setHeader('Authorization', header);- The Libraries picker button (shown in the script editor toolbar) lets you browse and insert the
kr.requireimport statement without typing it manually. - Use the copy icon next to the library name to copy it to your clipboard for quick referencing.
- Libraries are scoped to your workspace and automatically available to all members.
Why it matters: Eliminates duplication of utility functions like token generators, retry helpers, and response validators. One change to the library propagates everywhere it is used.
