Developer documentation
Postman-style pm API
Pre-request hooks get a small Postman-style API for variables and chained requests.
Variable APIs
| API | Methods | Use |
|---|---|---|
pm.environment | get(key), set(key, value) | Environment-style variables. |
pm.variables | get(key), set(key, value) | General variables. |
pm.collectionVariables | get(key), set(key, value) | Collection variables. |
pm.globals | get(key), set(key, value) | Global variables. |
pm.bus | input, read, output, setVariable, publish | Plugin 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;
}
}