Alerts help you know when something important happens during your API tests. They make sure you don't miss errors or issues.
0
0
Alert configuration in Postman
Introduction
When you want to get notified if an API response is slow.
When a test fails and you want to be alerted immediately.
When a specific error code appears in the API response.
When monitoring uptime or availability of a service.
When you want to track changes in API behavior over time.
Syntax
Postman
pm.test("Test name", function () { pm.response.to.have.status(200); if (pm.response.code !== 200) { pm.environment.set("alert", "API returned error status"); } });
Use pm.test to define a test and check conditions.
Set environment or global variables to trigger alerts or notifications.
Examples
This test checks if the API response is fast enough and sets an alert if it is slow.
Postman
pm.test("Response time is less than 500ms", function () { pm.expect(pm.response.responseTime).to.be.below(500); if (pm.response.responseTime > 500) { pm.environment.set("alert", "Slow response time"); } });
This test triggers an alert when the API returns a 404 error.
Postman
pm.test("Status code is 404", function () { pm.response.to.have.status(404); if (pm.response.code === 404) { pm.environment.set("alert", "Resource not found"); } });
Sample Program
This test checks if the response has a userId. If not, it sets an alert message in the environment variable. The alert can then be logged or used to notify.
Postman
pm.test("Check if user exists", function () { pm.response.to.have.status(200); const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('userId'); if (!jsonData.userId) { pm.environment.set("alert", "User ID missing in response"); } }); // Later in the collection runner or monitor, you can check the alert variable if (pm.environment.get("alert")) { console.log("ALERT: " + pm.environment.get("alert")); }
OutputSuccess
Important Notes
Alerts in Postman are usually handled by setting variables or using external integrations like email or Slack.
You can use Postman monitors to run tests regularly and send notifications based on alert variables.
Summary
Alerts help catch important issues during API testing.
Use test scripts to set alert messages when conditions fail.
Combine alerts with Postman monitors for automated notifications.