- Extracting data from a response in a specific way that requires multiple lines of code
- Handling specific authentication scenarios, such as getting authentication tokens from third party APIs or checking if auth tokens need to be renewed
- Executing specific cleanup code after every request in the test
- Creating assertions or variables from an XML-based response body
- Running console logs for debugging purposes
This guide outlines how to add pre-request, test, and flow-level scripts to an API test. For working examples, see the API script examples guide.
Test size limits
Pre-request scripts execute code before a request is made. To add one, open the API Test Editor and take the following steps:
- Click on the test step you'd like to edit on the left-side panel.
- In the Request details section, select the Pre-request tab.
- Edit your script code as needed.
Test scripts run code after a request is made. They can be added in the Validation and variables assignment section:
- In the API Test Editor, click on the step you'd like to edit in the left-side panel.
- Open up the Validation and variable assignment section.
- Select the Test tab.
- Edit your test script as needed.
You can review pre-request and test scripts for individual steps on the Test Details page.
Flow-level scripts are applied on every request in an API test; they include the option for both pre-request and post-request scripts.
To add a flow-level script, click on the pencil icon in the flow title card to open the flow properties modal:
In the modal, you can select the Pre-request or Post-request tab and edit your script code as needed. Close the modal using the X icon when you are happy with the script.
You can use the
require() method to call one of those libraries. Here is an example of using the
moment library to generate a future date that is seven days from today.
let moment = require('moment'); //get a date that is N number of days from today let date = moment.utc().add(7, 'days').toJSON(); //specify desired date and time format let dateFormat = "MM-DD-YYYY" //format the date and assign it to a variable to be used in other API requests and tests pm.environment.set("futureDate", moment(date).format(dateFormat));
Updated 15 days ago