Postman Sandbox API reference

Note: The functionality described here is exclusive to Postman's native apps for Mac, Windows, and Linux.

Global functions (pm.*)

require

require(moduleName:String):function → *

The require function allows you to use the sandbox built-in library modules. The list of available libraries are listed below. The list links to their corresponding documentation.

A number of NodeJS modules are also available to use in the sandbox:

In order to use a library, simply call the require function and pass the module name as a parameter and assign the return of the function to a variable.

var atob = require('atob'),
    _ = require('lodash'),
  
    arrayOfStrings,
    base64Strings;
  
arrayOfStrings = ['string1', 'string2'];
  
base64Strings = _.map(arrayOfStrings, atob);
  
console.log(base64Strings);

pm

pm:Object

The pm object encloses all information pertaining to the script being executed and allows one to access a copy of the request being sent or the response received. It also allows one to get and set environment and global variables.

pm.info:Object

The pm.info object contains information pertaining to the script being executed. Useful information such as the request name, request Id, and iteration count are stored inside of this object.

  • pm.info.eventName:String

    Contains information whether the script being executed is a "prerequest" or a "test" script.

  • pm.info.iteration:Number

    Is the value of the current iteration being run.

  • pm.info.iterationCount:Number

    Is the total number of iterations that are scheduled to run.

  • pm.info.requestName:String

    The saved name of the individual request being run.

  • pm.info.requestId:String

    The unique guid that identifies the request being run.

pm.sendRequest

pm.sendRequest:Function

The pm.sendRequest function allows sending HTTP/HTTPS requests asynchronously. Simply put, with asynchronous scripts, you can execute logic in the background if you have a heavy computational task or are sending multiple requests. Instead of waiting for a call to complete and blocking any next requests, you can designate a callback function and be notified when an underlying operation has finished.

Some things to know about pm.sendRequest():

  • The method accepts a collection SDK compliant request and a callback. The callback receives two arguments, an error (if any) and an SDK-compliant response. Refer to Collection SDK Documentation to view more information.
  • It can be used in the pre-request or the test script.
// example with a plain string URL
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.environment.set("variable_key", "new_value");
    }
});

// Example with a full fledged SDK Request
const echoPostRequest = {
  url: 'https://postman-echo.com/post',
  method: 'POST',
  header: 'headername1:value1',
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};
pm.sendRequest(echoPostRequest, function (err, res) {
  console.log(err ? err : res.json());
});

// example containing a test ** under the Tests tab only
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
  if (err) { console.log(err); }
  pm.test('response should be okay to process', function () {
    pm.expect(err).to.equal(null);
    pm.expect(res).to.have.property('code', 200);
    pm.expect(res).to.have.property('status', 'OK');
  });
});

Extended Reference:

pm.variables

pm.variables: Read more about VariableScope

In Postman, all variables conform to a specific hierarchy. All variables defined in the current iteration take precedence over the variables defined in the current environment, which overrides ones defined in the global scope, i.e. Iteration Data < Environment < Global.

  • pm.variables.has(variableName:String):function → Boolean
  • pm.variables.get(variableName:String):function → *
  • pm.variables.toObject():function → Object

The variables defined in the individual scopes may also be accessed via pm.environment for the environment scope and pm.globals for the global scope.

pm.environment

pm.environment: Read more about VariableScope

  • pm.environment.has(variableName:String):function → Boolean
  • pm.environment.get(variableName:String):function → *
  • pm.environment.set(variableName:String, variableValue:String):function
  • pm.environment.unset(variableName:String):function
  • pm.environment.clear():function
  • pm.environment.toObject():function → Object

pm.globals

pm.globals: Read more about VariableScope

  • pm.globals.has(variableName:String):function → Boolean
  • pm.globals.get(variableName:String):function → *
  • pm.globals.set(variableName:String, variableValue:String):function
  • pm.globals.unset(variableName:String):function
  • pm.globals.clear():function
  • pm.globals.toObject():function → Object

pm.request

pm.request: Read more about VariableScope

The request object inside pm is a representation of the request for which this script is being run. For a pre-request script, this is the request that is about to be sent and when in a test script, this is the representation of the request that was sent.

request contains information stored in the following structure:

  • pm.request.url:Url
  • pm.request.headers:HeaderList
  • pm.request.headers.add(headerName:String):function
  • pm.request.headers.delete(headerName:String):function
  • pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):function)

The following items are ONLY available in the test scripts.

pm.response

pm.response: Read more about Response

Inside the test scripts, the pm.response object contains all information pertaining to the response that was received.

The response details are stored in the following format:

  • pm.response.code:Number
  • pm.response.reason():Function → String
  • pm.response.headers:HeaderList
  • pm.response.responseTime:Number
  • pm.response.text():Function → String
  • pm.response.json():Function → Object

pm.iterationData

pm.iterationData: Read more about VariableScope

The iterationData object contains data from the data file provided during a collection run.

  • pm.iterationData.get(variableName:String):function → *
  • pm.iterationData.toObject():function → Object

pm.cookies

pm.cookies: Read more about CookieList

The cookies object contains a list of cookies that are associated with the domain to which the request was made.

  • pm.cookies.has(cookieName:String):Function → Boolean

    Check whether a particular cookie (addressed by its name) exists for the requested domain.

  • pm.cookies.get(cookieName:String):Function → String

    Get the value of a particular cookie.

  • pm.cookies.toObject:Function → Object

    Get a copy of all cookies and their values in the form of an object. The cookies returned are the ones defined for the requested domain and path.

pm.test

pm.test(testName:String, specFunction:Function):Function

You can use this function to write test specifications inside either the Pre-request Script or Tests sandbox. Writing tests inside this function allows you to name the test accurately and this function also ensures the rest of the script is not blocked even if there are errors inside the function.

In the following sample test, we are checking that everything about a response is valid for us to proceed.

  pm.test("response should be okay to process", function () {
      pm.response.to.not.be.error;
      pm.response.to.have.jsonBody('');
      pm.response.to.not.have.jsonBody('error');
  });

An optional done callback can be added to pm.test, to test asynchronous functions.

  pm.test('async test', function (done) {
    setTimeout(() => {
        pm.expect(pm.response.code).to.equal(200);
        done();
    }, 1500);
  });
  • pm.test.index():Function → Number

    Get the total number tests from a specific location.

pm.expect

pm.expect(assertion:*):Function → Assertion

pm.expect is a generic assertion function. Underlying this is the ChaiJS expect BDD library. Using this library, it is easy to write tests where the syntax becomes readable.

This function is useful to deal with assertions of data from a response or variables.

  pm.test("response value check", function () {
      var jsonData = pm.response.json();
      pm.expect(jsonData.value).to.eql(100);
  });
  pm.test('environment to be production', function () {
      pm.expect(pm.environment.get('env')).to.equal('production');
  });

Response Assertion API available in the test scripts

  • 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.*

The properties inside the pm.response.to.be object allows you to easily assert a set of pre-defined rules.

  • pm.response.to.be.info

    Checks 1XX status code

  • pm.response.to.be.success

    Checks 2XX status code

  • pm.response.to.be.redirection

    Checks 3XX status code

  • pm.response.to.be.clientError

    Checks 4XX status code

  • pm.response.to.be.serverError

    Checks 5XX

  • pm.response.to.be.error

    Checks 4XX or 5XX

  • pm.response.to.be.ok

    Status code must be 200

  • pm.response.to.be.accepted

    Status code must be 202

  • pm.response.to.be.badRequest

    Status code must be 400

  • pm.response.to.be.unauthorized

    Status code must be 401

  • pm.response.to.be.forbidden

    Status code 403

  • pm.response.to.be.notFound

    Status code of response is checked to be 404

  • pm.response.to.be.rateLimited

    Checks whether response status code is 429