Configuration
Pre-request scripts
Inject Postman pre-request scripts on specific operations with operationPreRequestScripts, inline or loaded from a JavaScript file.
operationPreRequestScripts injects Postman pre-request scripts into targeted requests. Postman runs them before the request is sent, which is useful to compute values, read iteration data or prepare variables that overwrites then use.
For scripts that should run before every request in the collection, use collectionPreRequestScripts in globals instead.
Options
| Property | Description |
|---|---|
openApiOperationId | OpenAPI operationId. |
openApiOperationIds | Array of operationIds. |
openApiOperation | Method and path, supports wildcards. |
excludeForOperations | Optional. Operations to skip. |
scripts | Array of scripts. A value is either the script content or a path to a script file with the file: prefix. |
Example
This example reads a lead's monetary_amount, doubles it in a pre-request script, and sends the new value when updating the lead.
yaml
$schema: https://raw.githubusercontent.com/apideck-libraries/portman/main/src/utils/portman-config-schema.jsonversion: 1globals: stripResponseExamples: trueassignVariables: # After getting a lead, save its monetary_amount in a collection variable - openApiOperation: GET::/crm/leads/{id} collectionVariables: - responseBodyProp: data.monetary_amount name: "<tag>_monetary_amount" operationPreRequestScripts: # Before updating a lead, update the variable in a pre-request script - openApiOperation: "PATCH::/crm/leads/{id}" scripts: - file:increaseMonetaryAmount.js overwrites: # When updating a lead, use the variable as a number - openApiOperation: "PATCH::/crm/leads/{id}" overwriteRequestBody: - key: monetary_amount value: '{{{<tag>_monetary_amount}}}' overwrite: trueincreaseMonetaryAmount.js:
js
// Pre-request script to check the existing monetary_amount of the lead and then double itlet monetaryAmount = pm.collectionVariables.get("Leads_monetary_amount")if (monetaryAmount > 0) { monetaryAmount *= 2;} else { monetaryAmount = 25000;}// Update the variable with the new amount prior to updating the leadpm.collectionVariables.set("Leads_monetary_amount", monetaryAmount);Inline scripts
json
"operationPreRequestScripts": [ { "openApiOperationId": "leadsAdd", "scripts": [ "pm.collectionVariables.set('leadName', 'Lead ' + Date.now());" ] }]