Developer documentation

Postman-style pm API

Pre-request hooks get a small Postman-style API for variables and chained requests.

Variable APIs

APIMethodsUse
pm.environmentget(key), set(key, value)Environment-style variables.
pm.variablesget(key), set(key, value)General variables.
pm.collectionVariablesget(key), set(key, value)Collection variables.
pm.globalsget(key), set(key, value)Global variables.
pm.businput, read, output, setVariable, publishPlugin bus from hook code.

Pre-request token example

hooks: {
  beforeRequest: function(ctx) {
    var token = ctx.pm.environment.get("token") || ctx.bus.input("auth.token", "");
    if (token) ctx.request.headers.Authorization = "Bearer " + token;
    ctx.pm.environment.set("lastUrl", ctx.request.url);
    return ctx.request;
  }
}

pm.sendRequest

pm.sendRequest(input) queues a chained request action. Use it for token handshakes or preflight calls.

hooks: {
  beforeRequest: function(ctx) {
    ctx.pm.sendRequest({
      name: "Audit preflight",
      method: "POST",
      url: "https://example.com/audit",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ url: ctx.request.url })
    });
    return ctx.request;
  }
}