Skip to content

Pre-request Scripts

Run a small JavaScript snippet before a request is sent. Pre-request scripts are useful for setting environment variables dynamically - for example, generating a timestamp, computing a signature, or reading a value that changes between requests.

Where to Write Scripts

Open a saved request and click the Pre-request tab in the request form. A dot appears on the tab label when a script is present.

Pre-request script editorPre-request script editor

The editor provides autocompletion for all available globals.

Available Globals

environment

Read and write variables in the active environment.

MethodDescription
environment.get(key)Returns the current value of key, or an empty string if the variable does not exist
environment.set(key, value)Sets the current value of key in the active environment

environment.set() writes to the variable's current value, which is a local override stored separately from the committed environments.json file. This means scripts never cause unintended git changes. See Environments: Initial Value and Current Value for details.

request

Read-only view of the outgoing request.

PropertyTypeDescription
request.methodstringHTTP method, e.g. "GET"
request.urlstringFull URL before variable substitution
request.headersRecord<string, string> | undefinedRequest headers
request.bodystring | undefinedRaw request body

Example

js
// Set a timestamp for use in a signed request
environment.set('timestamp', new Date().toISOString());

// Compute a derived value from an existing variable
const baseToken = environment.get('apiToken');
environment.set('authHeader', 'Bearer ' + baseToken);

After the script runs, any variables set with environment.set() are applied before variable substitution happens. This means you can set {{timestamp}} in a header and the pre-request script will supply its value.

Execution Order

  1. Pre-request script runs
  2. Environment variables are updated with any overrides from the script
  3. Variable substitution replaces {{placeholders}} in the URL, headers, and body
  4. The request is sent

Limitations

  • Scripts time out after 5 seconds
  • No network access (fetch and XMLHttpRequest are not available)
  • No require or import - scripts run in an isolated sandbox
  • No async/await - scripts must be synchronous
  • Only enabled variables in the active environment are visible to environment.get()

Released under the MIT License.