Prompt
How do I test a REST API with collections and assertions?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
A common way to test a REST API is to use an API client like Postman or Insomnia and organize requests into collections with built-in assertions.
Basic workflow
-
Create a collection
- Group related requests together, such as:
UsersAuthenticationOrders
- Group related requests together, such as:
-
Add requests
- Examples:
POST /loginGET /usersPUT /users/:idDELETE /users/:id
- Examples:
-
Set variables
- Use environment or collection variables for reusable values like:
baseUrltokenuserId
- Use environment or collection variables for reusable values like:
-
Write assertions
- Assertions verify that the API response is what you expect.
Common assertions to check
- Status code
- Example:
200,201,204,401,404
- Example:
- Response time
- Ensure the API responds within a limit
- Response body
- Check specific fields and values
- Headers
- Validate content type, auth headers, etc.
- Schema
- Confirm the response structure matches expectations
Example assertions in Postman
1. Status code
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
2. Response time
pm.test("Response time is less than 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
3. JSON field value
pm.test("User name is correct", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("Alice");
});
4. Header validation
pm.test("Content-Type is JSON", function () {
pm.response.to.have.header("Content-Type");
pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
});
Example collection flow
Request 1: Login
- Send credentials to
POST /login - Assert
200 OK - Extract token:
const jsonData = pm.response.json();
pm.environment.set("token", jsonData.token);
Request 2: Get profile
- Use token in header:
Authorization: Bearer {{token}}
- Assert:
200 OK- Profile contains expected user details
Running collection tests
- Use the collection runner in Postman to execute all requests
- Run them in sequence if later requests depend on earlier ones
- Review failed assertions to find issues quickly
Best practices
- Keep tests small and focused
- Reuse variables instead of hardcoding values
- Validate both success and error responses
- Test edge cases and invalid inputs
- Add collection-level pre-request scripts or tests for reusable logic
- Version your collections if the API changes
If you want automation
You can also run collections from CI/CD using:
- Newman for Postman collections
- pytest + requests in Python
- RestAssured in Java
- Jest + supertest for Node.js
If you want, I can show you:
- a Postman collection example, or
- how to do this in JavaScript/Python automation.