Pre-request scripts

Pre-request scripts are snippets of code associated with a collection request that are executed before the request is sent. This is perfect for use-cases like including the timestamp in the request headers or sending a random alphanumeric string in the URL parameters.

For example, to include a timestamp in the request headers, you can set an environment variable with the value returned from a function.

set environment variable

You can then access the timestampHeader variable in the header data editor by typing {{timestampHeader}}. When the request is sent, your pre-request script will be executed, and the value of timestampHeader will be sent in place of {{timestampHeader}}.

timestampHeader variable

Note: An environment should be an active one for environment variables to be set.

Pre-request scripts are written in JavaScript, and the syntax is similar to the test scripts except that the response object is not present.

Adding a pre-request script to a collection or folder

You can add pre-request scripts to a collection, a folder, or a single request within a collection. A pre-request script associated with a collection runs prior to every request in the collection. A pre-request script associated with a folder runs prior to every request in the folder. This allows you to reuse commonly executed code prior to every request.

Collection and folder scripts can be updated in the collection or folder details respectively. Click on the ellipsis (...) next to the collection or folder name, and select “Edit” to open the modal. Select the Pre-request Scripts tab to add and update the scripts. You can also add collection scripts when initially creating the collection.

pre-request script for folder

Read more about the execution order of scripts.