Postman JavaScript API Cheat Sheet
# Postman JavaScript API Cheat Sheet
## 1. Request & Response Access
- `pm.request` Current request object.
- `pm.request.headers`
- `pm.request.body`
- `pm.response` Current response object.
- `pm.response.code`
- `pm.response.status`
- `pm.response.headers`
- `pm.response.body`
- `pm.response.json()`
## 2. Variables
- `pm.variables.set(name, value)`
- `pm.variables.get(name)`
- `pm.environment.set(name, value)`
- `pm.environment.get(name)`
- `pm.environment.unset(name)`
- `pm.globals.set(name, value)`
- `pm.globals.get(name)`
- `pm.globals.unset(name)`
## 3. Tests
- `pm.test("Test name", function () { ... })`
- `pm.expect(value).to.eql(expected)`
- `pm.response.to.have.status(code)`
- `pm.response.to.be.ok`
- `pm.response.to.have.header("Content-Type")`
## 4. Collection & Iteration Data
- `pm.collectionVariables.set(name, value)`
- `pm.collectionVariables.get(name)`
- `pm.info` Meta info about request/script.
- `pm.iterationData.get("key")`
- `pm.iterationData.toObject()`
## 5. Assertions (ChaiJS)
- `pm.expect(actual).to.eql(expected)`
- `pm.expect(str).to.include("value")`
- `pm.expect(num).to.be.above(0)`
## 6. Tools
- `pm.sendRequest(options, callback)`
Postman JavaScript API Cheat Sheet
- `pm.cookies.get("cookieName")`
- `pm.cookies.jar()`
## Example
```javascript
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.environment.set("authToken", pm.response.json().token);
```