APIs (Application Programming Interfaces) power modern software, connecting everything from mobile apps to cloud services. Testing APIs ensures they work as expected, but manually sending requests via code or terminals is time-consuming. API Testing with Postman simplifies API testing, automation, and collaboration.
In this tutorial, you’ll learn:
- How to send your first API request in Postman.
- Validate responses with status codes and assertions.
- Automate tests using Postman Collections.
- Pro tips to debug APIs like a pro.
What is Postman?
Postman is a collaboration platform for API development and testing. It allows you to:
- Send HTTP requests (GET, POST, PUT, DELETE).
- Organize requests into Collections.
- Automate tests with JavaScript.
- Generate API documentation.
Step 1: Install Postman
- Download Postman: Visit postman.com and sign up for free.
- Choose Your OS: Install the desktop app (Windows/Mac/Linux) for better performance.
Step 2: Send Your First API Request
Let’s test a sample REST API (JSONPlaceholder) to fetch dummy data.
- Open Postman and click New → HTTP Request.
- Enter Request Details:
- Method: GET
- URL:
https://jsonplaceholder.typicode.com/posts/1
- Click Send.
Expected Response:
{ "userId": 1, "id": 1, "title": "sunt aut facere repellat...", "body": "quia et suscipit..." }
What Happened?
Postman sent a GET request to retrieve data for post ID 1. The Status Code (200 OK) confirms success.
Step 3: Validate API Responses
Check Status Codes
- 2xx (Success): 200 OK, 201 Created.
- 4xx (Client Error): 404 Not Found, 400 Bad Request.
- 5xx (Server Error): 500 Internal Server Error.
Add Assertions with Tests
Postman lets you write JavaScript tests to validate responses automatically.
- Go to the Tests tab in your request.
- Add this script to check if the status code is 200:
pm.test("Status code is 200", function () { pm.response.to.have.status(200); });
- Click Send again.
- Check the Test Results tab for pass/fail status.
Pro Tip: Use pre-built snippets under SNIPPETS (right sidebar) for common tests.
Step 4: Organize Requests into Collections
Collections group related API requests (e.g., all endpoints for a user service).
- Click New → Collection.
- Name it (e.g., “JSONPlaceholder Tests”).
- Drag existing requests into the collection.
Benefits:
- Run all requests in a collection sequentially.
- Share collections with your team.
Step 5: Automate Tests with Postman Runner
- Open the Collection Runner (click the rocket icon).
- Select your collection and environment (if any).
- Set iterations (e.g., run 5 times).
- Click Run JSONPlaceholder Tests.
Result: Postman executes all requests and displays test results.
Step 6: Use Variables for Dynamic Testing
Variables help reuse values across requests (e.g., tokens, user IDs).
- Define a Variable:
- Go to the collection’s Variables tab.
- Add a variable
base_url
=https://jsonplaceholder.typicode.com
.
- Update Request URL: Replace the URL with
{{base_url}}/posts/1
.
Why: Simplify URL changes across multiple requests.
Step 7: Test Authentication APIs
Let’s test a POST request with authentication:
- Create a New Request:
- Method: POST
- URL:
https://api.example.com/login
- Headers:
Content-Type: application/json
- Body → Raw → JSON:
{ "username": "test_user", "password": "test123" }
- Tests: Validate if a token is returned:
pm.test("Token is generated", function () { var jsonData = pm.response.json(); pm.expect(jsonData.token).to.exist; });
Common Postman Shortcuts
- Ctrl + S: Save request.
- Ctrl + Enter: Send request.
- Ctrl + B: Toggle sidebar.
Postman Alternatives
- Insomnia: Lightweight API client.
- Swagger UI: For API documentation and testing.
- SoapUI: Advanced SOAP and REST testing.
FAQs
Q: Is Postman free?
A: Yes! The free plan covers most testing needs. Paid plans start at $15/user/month for advanced features.
Q: Can I test GraphQL APIs in Postman?
A: Yes! Use the GraphQL body type to send queries.
Q: How to share Postman collections?
A: Click Share → Generate a Public Link or export as a JSON file.
Conclusion
Postman transforms API testing from a chore into a streamlined, automated process. By mastering collections, tests, and variables, you’ll catch bugs faster and collaborate seamlessly with developers.