Skip to content

How To Use Postman Flows

API Testing Blog

Streamlining API Testing with Postman Flows: A Comprehensive Guide

Postman Flows provide a powerful way to automate and orchestrate your API tests. They allow you to chain multiple requests together, manage test data, and integrate with external services, creating a robust and efficient testing workflow.

Let’s Dive into Postman Flows:

1. Setting up Your First Postman Flow

  • Create a New Flow: Start by navigating to the “Flows” tab in your Postman workspace. Click “Create Flow” and give your flow a descriptive name, like “User Registration and Login Flow.”
  • Add Your API Requests: Click the ”+” button to add your first request. Choose from your existing requests in the collection or create new ones directly within the flow.
  • Connect the Requests: Once you’ve added all your requests, use the “Connect” button to link them together in the desired order.

2. Writing Tests and Assertions

Example: To test the user registration endpoint, you could add a test to verify the response status code:

pm.test("Status code is 201", () => {
pm.response.to.have.status(201);
});

3. Handling Variables and Data

Postman Flows allow you to pass data between requests using variables:

  • Using Environment Variables: Store commonly used information, like API keys, in Environment variables.
  • Using Flow Variables: Create variables within the flow itself to store temporary values, such as user IDs or session tokens.

Example: Extracting the user ID from the registration response and using it in the login request:

pm.test("Extract User ID", () => {
const userId = pm.response.json().id;
pm.variables.set("userId", userId);
});

4. Looping Through Data

Postman Flows support data-driven testing with the forEach loop:

Example: Testing multiple items in a shopping cart with different quantities:

// Sample data
let cartItems = [
{ name: "Shirt", quantity: 2 },
{ name: "Pants", quantity: 1 }
];
pm.forEach(cartItems, (item) => {
pm.test("Adding item to cart", () => {
pm.sendRequest({
url: "https://api.example.com/cart/add",
method: "POST",
body: { name: item.name, quantity: item.quantity }
});
});
});

5. Leveraging Integrations

Postman Flows integrate seamlessly with external services, enhancing your testing capabilities:

  • Databases: Connect to databases, like MySQL or MongoDB, to fetch or update test data.
  • Message Queues: Send messages to queues like RabbitMQ or Kafka, triggering the next step in your workflow.
  • Webhooks: Trigger actions in your flow based on events from webhooks.

6. Debugging and Monitoring

Postman Flows provide powerful debugging and monitoring features:

  • Logs: View the logs for each request in the flow, including request headers, response content, and test results.
  • Timeline: Analyze the execution time of each request and see how long it takes to complete the entire flow.
  • Monitoring: Use Postman Monitors to continuously run your flows on a schedule and receive alerts if any tests fail.

7. Real-World Example: Testing a Basic E-commerce API

  1. Create a Flow: Name it “E-commerce Flow.”
  2. Add Requests:
    • Create User (POST): Register a new user on your e-commerce platform.
    • Login (POST): Authenticate the newly created user.
    • Add to Cart (POST): Add an item to the user’s cart.
    • View Cart (GET): Retrieve the contents of the cart.
    • Checkout (POST): Process the checkout.
  3. Connect Requests: Link the requests in the order described above.
  4. Write Tests:
    • Verify that the registration and login responses have the correct status codes.
    • Assert that the cart contains the expected items after adding an item.
    • Check the total amount and other details in the checkout response.
  5. Use Variables: Extract the user ID from the registration response and store it in a flow variable for use in subsequent requests.
  6. Monitor the Flow: Set up a monitor to run the flow regularly and ensure the API is functioning correctly.

Sample Code:

Create User request:

{
"method": "POST",
"url": "https://api.example.com/users",
"body": {
"mode": "raw",
"raw": "{\n \"email\": \"testuser@example.com\",\n \"password\": \"password123\"\n}"
},
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
]
}

Login request:

{
"method": "POST",
"url": "https://api.example.com/login",
"body": {
"mode": "raw",
"raw": "{\n \"email\": \"testuser@example.com\",\n \"password\": \"password123\"\n}"
},
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
]
}

Add to Cart request:

{
"method": "POST",
"url": "https://api.example.com/cart/add",
"body": {
"mode": "raw",
"raw": "{\n \"productId\": 123,\n \"quantity\": 2\n}"
},
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Authorization",
"value": "Bearer {{token}}"
}
]
}

Tests:

pm.test("Status code is 200", () => {
pm.response.to.have.status(200);
});
pm.test("Response body contains expected data", () => {
pm.response.to.have.jsonBody("productName", "My Awesome Product");
});

Variables:

// Store the user ID in a flow variable
pm.variables.set("userId", pm.response.json().id);

Conclusion:

By leveraging the power of Postman Flows, you can significantly enhance your API testing process. The flexibility to chain requests, manage data, and integrate with external services makes Postman Flows an invaluable tool for building robust and efficient testing workflows.

API Testing Blog