Apidog Docs
🇺🇸 English
  • 🇺🇸 English
  • 🇯🇵 日本語
HomeLearning CenterSupport CenterAPI References
HomeLearning CenterSupport CenterAPI References
Discord Community
Slack Community
X / Twitter
🇺🇸 English
  • 🇺🇸 English
  • 🇯🇵 日本語
  1. Using scripts
  • Apidog Learning Center
  • Get started
    • Introduce Apidog
    • Basic concepts in Apidog
    • Navigating Apidog
    • Quick Start
      • Overview
      • Specify a new endpoint
      • Make a request to the endpoint
      • Add an assertion
      • Create a test scenario
      • Share your API documentation
      • Explore more
      • Send a request and save as an endpoint
    • Migration
      • Overview
      • Manual import
      • Scheduled import
      • Import options
      • Export data
      • Import from...
        • Import from Postman
        • Import OpenAPI (Swagger) spec
        • Import cURL
        • Import Markdowns
        • Import from Insomnia
        • Import from apiDoc
        • Import .har file
        • Import WSDL
  • Design APIs
    • Overview
    • Create a new API project
    • Endpoint basics
    • Components
    • Common fields
    • Global parameters
    • Endpoint change history
    • Batch endpoint management
    • Configure multiple request body examples
    • Schemas
      • Generate Schemas from JSON etc.
      • Build a schema
      • Overview
      • Create a new schema
    • Security schemes
      • Overview
      • Create a security scheme
      • Use the security scheme
      • Security scheme in online documentation
    • Advanced features
      • Custom endpoint fields
      • Import endpoints as test steps
      • Endpoint status
      • Appearance of parameter lists
      • Endpoint unique idenfication
  • Develop and Debug APIs
    • Overview
    • Generate requests
    • Send requests
    • Endpoint cases
    • Dynamic values
    • Validate responses
    • Design-first Mode & Request-first Mode
    • Generate code
    • Environments & variables
      • Overview
      • Using variables
      • Environments & services
    • Vault secrets
      • Overview
      • HashiCorp Vault
      • Azure Key Vault
      • AWS Secrets Manager
    • Pre/Post processors
      • Overview
      • Assertion
      • Extract variable
      • Wait
      • Database operations
        • Overview
        • MongoDB
        • Redis
        • Oracle Client
      • Using scripts
        • Overview
        • Postman scripts reference
        • Pre processor scripts
        • Post processor scripts
        • Public scripts
        • Calling other programming languages
        • Using JS libraries
        • Visualizing responses
        • Script examples
          • Assertion scripts
          • Using variables in scripts
          • Using scripts to modify request messages
          • Other examples
    • Dynamic values Modules
  • Mock API data
    • Overview
    • Smart mock
    • Custom mock
    • Mock priority sequence
    • Mock scripts
    • Cloud mock
    • Self-hosted runner mock
    • Mock language (Locales)
  • Automated tests
    • Overview
    • Test reports
    • Test scenarios
      • Create a test scenario
      • Pass data between requests
      • Flow control conditions
      • Import endpoints/endpoint cases from other projects
      • Sync data from endpoints/endpoint cases
      • Export test scenarios
    • Run test scenarios
      • Run a test scenario
      • Data-driven testing
      • Run test scenarios in batch
      • Scheduled tasks
      • Manage the runtime environment of APIs from other projects
    • Test APIs
      • Integration testing
      • Performance testing
      • End-to-end testing
      • Regression testing
    • Apidog CLI
      • Overview
      • Installing and running Apidog CLI
      • Apidog CLI Options
    • CI/CD
      • Overview
      • Integrate with Jenkins
      • Integration with Gitlab
  • Publish API Docs
    • Overview
    • API Technologies Supported
    • Quick share
    • View the API documentation
    • Publish docs sites
    • Folder tree settings
    • Custom layouts
    • Visibility settings
    • Endpoint SEO settings
    • Custom domain
    • Embedding values in document URLs
    • Documentation Search
    • Integrating Google Analytics with Doc Sites
    • CORS Proxy
    • API Versions
      • Overview
      • Create API versions
      • Publish API versions
      • Share endpoints with API versions
  • Send requests
    • Overview
    • gRPC
    • Use request proxy agents for debugging
    • SOAP/WebService
    • GraphQL
    • WebSocket
    • Socket.IO
    • SSE debugging
    • Create requests
      • Request History
      • Request basics
      • Parameters and body
      • Request headers
      • Request settings
      • HTTP/2
    • Authentication and authorization
      • Overview
      • CA and client certificates
      • Authorization types supported by Apidog
      • Digest Auth
      • OAuth 1.0
      • OAuth 2.0
      • Hawk Authentication
      • Kerberos
      • NTLM
      • Akamai EdgeGrid
    • Response and cookies
      • Overview
      • API response in Apidog
      • Create and send cookies
      • Debug requests
      • Save the request as an endpoint
  • Branches
    • Overview
    • Create a new sprint branch
    • Test APIs in a branch
    • Design API in a branch
    • Merge sprint branches
    • Manage sprint branches
  • Apidog MCP Server
    • Overview
    • Conntect API Specification within Apidog Project to AI via Apidog MCP Server
    • Conntect Online API Documentation Published by Apidog to AI via Apidog MCP Server
    • Conntect OpenAPI Files to AI via Apidog MCP Server
  • Best practices
    • How to handle API signatures
    • How to access OAuth 2.0 protected APIs
    • Apidog collaboration workflow
    • Managing authentication state in Apidog
  • Administration
    • Onboarding Checklist
      • Basic Concepts
      • Onboarding Guide
    • Managing Teams
      • Managing Teams
      • Managing Team Members
      • Member Roles & Permission Settings
      • Team Activities
      • Team Resources
        • General Runner
        • Team Variables
        • Request Proxy Agent
      • Real-time Collaborations
        • Team Collaboration
    • Managing Projects
      • Managing Projects
      • Managing Project Members
      • Notification Settings
      • Project Resources
        • Database Connection
    • Managing Organizations
      • Single Sign-On (SSO)
        • SSO Overview
        • Configure Microsoft Entra ID
        • Configure Okta
        • Configure SSO for an Organization
        • Managing user accounts
        • Mapping Groups to Teams
      • SCIM Provisioning
        • Intro to SCIM Provisioning
        • Microsoft Entra ID
        • Okta
      • Organization Resources
        • Self-hosted Runner
  • Billing
    • Overview
    • Credits
    • Unable to use credit cards?
    • Managing subscriptions
    • Upgrade plan
  • Add-ons
    • API Hub
    • Apidog Intellij IDEA plugin
    • Browser Extension
      • Chrome
      • Microsoft Edge
    • Request Proxy
      • Request proxy in Apidog web
      • Request proxy in shared docs
      • Request proxy in Apidog client
  • Account & preferences
    • Language settings
    • Data backup
    • Network proxy configuration
    • Hot keys
    • Updating Apidog
    • Generate OpenAPI access token
    • Deleting account
    • Account settings
  • References
    • API-Design First Approach
    • Apidog OpenAPI Specificaiton Extensions
    • JSONPath
    • XPath
    • Regular Expressions
    • JSON Schema
    • CSV File Format
    • Install Java Environment
    • Runner deployment environment
    • Apidog flavored Markdown
  • Apidog Europe
    • Apidog Europe
  • Support Center
  1. Using scripts

Postman scripts reference

pm#

pm objects contain information related to running API or test collections. You can use it in your request and response data. You can get or set on environment and global variables.
pm.info.eventName:String: the type of scripts that is currently running (preprocessor script or postprocessor script).
pm.info.iteration:Number: the number of the current iteration(only valid in test collections).
pm.info.iterationCount:Number: the number of total iterations (only valid in test collections).
pm.info.requestName:String: the name of the current API running.
pm.info.requestId:String: the ID of the current API running.

pm.sendRequest#

pm.sendRequest:Function
pm.sendRequest is used to send HTTP/HTTPS requests asynchronously in scripts.
This method accepts a request parameter compatible with collection SDK- and a callback function parameter. The callback has 2 arguments. The first is an error and the second is a response compatible with the collection SDK. View more information in the Collection SDK documentation.
You can use it in both preprocessor and postprocessor scripts.
For more references, please visit:
Request JSON structure
Response structure

pm.variables#

pm.variables:view Variable SDK documentation here.
Local variables.The priority of different variables is as follows:
Local Variables > Environment Variables > Global Variables Shared within Project > Global Variables Shared within Team.
pm.variables.has(variableName:String):function → Boolean: Check whether a temporary variable exists.
pm.variables.get(variableName:String):function → *: get a temporary variable.
pm.variables.set(variableName:String, variableValue:String):function → void: set a temporary variable.
pm.variables.replaceIn(variableName:String):function: Replaces "dynamic variables" within a string (e.g., {{variable_name}}) with actual values. Example:
// Define a string containing a dynamic variable
let stringWithVariable = "Hello, {{username}}";

// Use the replaceIn method to replace the {{username}} placeholder
let realValueString = pm.variables.replaceIn(stringWithVariable);

// Output the replaced value
console.log(realValueString); // Output: "Hello, john_doe"
pm.variables.replaceInAsync(variableName:String):function: Replaces "dynamic value expressions" within a string (e.g., {{$person.fullName}}) with actual values. This method returns a Promise, so you need to use await when calling it. Example:
// Define a string containing a dynamic value expression
let stringWithVariable = "Hello, {{$person.fullName}}";

// Use the replaceInAsync method to replace the {{$person.fullName}}
let realValueString = await pm.variables.replaceInAsync(stringWithVariable);
pm.variables.toObject():function → Object: get all local variables as objects.

pm.iterationData#

pm.iterationData:
Test Data Variables
We currently do not support setting test data variables directly in scripts, since test data is managed separately. However, you can access the variables in scripts as shown below:
pm.iterationData.has(variableName:String):function → Boolean: Check whether a test variable exists.
pm.iterationData.get(variableName:String):function → *: get a test variable.
pm.iterationData.replaceIn(variableName:String):function: replace dynamic variables in a string with their actual values, for example, {{variable_name}}.
pm.iterationData.toObject():function → Object: get all local variables as objects.

pm.environment#

pm.environment.name:String: the environment name.
pm.environment.has(variableName:String):function → Boolean:Check whether an environment variable exists.
pm.environment.get(variableName:String):function → *: get an environment variable.
pm.environment.set(variableName:String, variableValue:String):function:set an environment variable.
pm.environment.replaceIn(variableName:String):function: replace dynamic variables in a string with their actual values, for example, {{variable_name}}.
pm.environment.toObject():function → Object: get all local variables as objects.
pm.environment.unset(variableName:String):function: unset an environment variable.
pm.environment.clear():function: clear all environment variables under the current environment.
TIP the operations mentioned above only read and write current values; they do not read or write remote values.

pm.globals#

pm.globals.has(variableName:String):function → Boolean: Check whether a global variable exists.
pm.globals.get(variableName:String,variableScope:String):function → *: get a global variable. Use 'PROJECT' (default) or 'TEAM' to specify the variable's scope.
pm.globals.set(variableName:String,variableValue:String, variableScope:String):function: set a global variable. Use 'PROJECT' (default) or 'TEAM' to specify the variable's scope.
pm.globals.replaceIn(variableName:String):function: replace dynamic variables in a string with their actual values, for example, {{variable_name}}.
In order to get the value of a request parameter that contains a variable in preprocessor scripts, use pm.globals.replaceIn to replace the variable with the real value.
pm.globals.toObject():function → Object: get all global variables as objects.
pm.globals.unset(variableName:String,variableScope:String):function: unset a global variable. Use 'PROJECT' (default) or 'TEAM' to specify the variable's scope.
pm.globals.clear():function: clear all global variables under the current environment.
TIP
1.
All operations above affect onlycurrent values, notinitial values.
2.
When using set with the 'TEAM' scope, it will only update the current value of an existing team variable. If the team variable doesn't exist, it will not be created. Instead, the variable will be treated as a local variable.

pm.request#

pm.request: view Request SDK documentation here.
request is the API request object. In the preprocessor script, it is the request that will be sent. In the postprocessor script, it is the request that has already been sent.
request includes the following information:
pm.request.url:Url: the URL of the current request.
pm.request.getBaseUrl(): Retrieves BASE URL selected by the current runtime environment. This feature is supported after version 2.1.39.
pm.request.headers:HeaderList: the header list of the current request.
pm.request.method:String: the method of the current request, such as GET, POST, etc.
pm.request.body: RequestBody: the body of the current request.
pm.request.headers.add({ key: headerName:String, value: headerValue:String}):function: Add a header with a key, headerName, in the current request.
pm.request.headers.remove(headerName:String):function: Delete a header with a key, headerName, in the current request.
pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):function: Upsert a header with a key, headerName, in the current request. If the key already exists, it will be modified.
The following API can only be used inpostprocessor scripts.

pm.response#

pm.response: view Response SDK documentation here.
Use pm.response to access return response information in postprocessor scripts.
pm.response includes the following information:
pm.response.code:Number
pm.response.status:String
pm.response.headers:HeaderList
pm.response.responseTime:Number
pm.response.responseSize:Number
pm.response.text():Function → String
pm.response.json():Function → Object

pm.cookies#

pm.cookies: view CookieList SDK documentation here.
Cookies is the list of cookies under the domain name of the current request.
pm.cookies.has(cookieName:String):Function → Boolean
Check whether the cookie value of a cookieName exists.
pm.cookies.get(cookieName:String):Function → String
Get cookie value from cookieName.
pm.cookies.toObject:Function → Object
Get all cookies under the current domain as an object.
pm.cookies.jar().clear(pm.request.url)
Clear all cookies.
TIP
pm.cookies is the cookie returned after the API request, not the cookie sent by the API request.

pm.test#

This function is used to assert whether a result meets expectations.
The example below can be used to determine whether a response is correct.
You can run an async test using done(an optional parameter) in a callback function.
pm.test.index():Function → Number
Get the total number tests from a specific location.

pm.expect#

pm.expect is an assertion method. View ChaiJS expects BDD library documentation here.
This method is designed to assert data in response or variables. For more pm.expect examples, please visit Assertion library examples.

pm.response.to.have.*#

pm.response.to.have.status(code:Number)
pm.response.to.have.status(reason:String)
pm.response.to.have.header(key:String)
pm.response.to.have.header(key:String, optionalValue:String)
pm.response.to.have.body()
pm.response.to.have.body(optionalValue:String)
pm.response.to.have.body(optionalValue:RegExp)
pm.response.to.have.jsonBody()
pm.response.to.have.jsonBody(optionalExpectEqual:Object)
pm.response.to.have.jsonBody(optionalExpectPath:String)
pm.response.to.have.jsonBody(optionalExpectPath:String, optionalValue:*)
pm.response.to.have.jsonSchema(schema:Object)
pm.response.to.have.jsonSchema(schema:Object, ajvOptions:Object)

pm.response.to.be.*#

You can use the built-inpm.response.to.befor quick assertions.
pm.response.to.be.info
Check whether the status code is 1XX.
pm.response.to.be.success
Check whether the status code is 2XX.
pm.response.to.be.redirection
Check whether the status code is 3XX.
pm.response.to.be.clientError
Check whether the status code is 4XX.
pm.response.to.be.serverError
Check whether the status code is 5XX.
pm.response.to.be.error
Check whether the status code is 4XX or 5XX.
pm.response.to.be.ok
Check whether the status code is 200.
pm.response.to.be.accepted
Check whether the status code is 202.
pm.response.to.be.badRequest
Check whether the status code is 400.
pm.response.to.be.unauthorized
Check whether the status code is 401.
pm.response.to.be.forbidden
Check whether the status code is 403.
pm.response.to.be.notFound
Check whether the status code is 404.
pm.response.to.be.rateLimited
Check whether the status code is 429.
Modified at 2025-01-15 09:50:42
Previous
Overview
Next
Pre processor scripts
Built with