Postman API Testing Guide
Postman is a powerful tool for testing and documenting APIs. This guide walks you through setting up Postman, authenticating requests, and testing common API endpoints with detailed examples.
Prerequisites
Before you begin, ensure you have:
- Postman installed (Download here)
- An API to test (with authentication details)
- Basic understanding of HTTP requests (GET, POST, PUT, DELETE)
Step 1: Set Up Postman
- Install and launch Postman.
- Create a new Collection to organize your API requests.
- Give the collection a descriptive name (e.g.,
Leave Management API). - Optionally, set up environments (e.g., Development, Staging, Production) to manage base URLs and tokens dynamically.
Step 2: Obtain an Authentication Token
Most APIs require authentication. Here we’ll obtain a Bearer Token using a login endpoint.
- Create a new POST request.
- Set the URL to your login endpoint, e.g.:
http://yourapiurl/api/login
- Go to the Body tab → select raw → choose JSON.
- Provide your credentials:
{
"email": "user@example.com",
"password": "password"
}
- Click Send.
- Copy the access token from the JSON response.
✅ Tip: Save the token in an Environment Variable (e.g., {token}) for reusability.
Step 3: Test the Index Endpoint (GET)
- Create a new GET request.
- Set the URL to your index endpoint, e.g.:
http://yourapiurl/api/leaves
- Go to the Headers tab and add:
Key: Authorization
Value: Bearer YOUR_ACCESS_TOKEN
- Replace
YOUR_ACCESS_TOKENwith the token from Step 2. - Click Send.
If successful, you should see a JSON response with a list of leaves.
Step 4: Test the Create Endpoint (POST)
- Create a new POST request.
- Set the URL to your create endpoint, e.g.:
http://yourapiurl/api/leaves
- Add Authorization Header:
Key: Authorization
Value: Bearer YOUR_ACCESS_TOKEN
- Go to the Body tab → select raw → choose JSON.
- Provide the payload. For example:
{
"employee_id": 1,
"leave_type_id": 2,
"start_date": "2024-08-01",
"end_date": "2024-08-05",
"reason": "Family event"
}
- Click Send.
You should see a JSON response confirming the leave creation.
Step 5: Example Postman Configuration
Here’s how your Postman setup should look:
- Collection: Leave Management API
- Request 1: Login (POST) → Saves token
- Request 2: Leaves Index (GET) → Requires token
- Request 3: Create Leave (POST) → Requires token
✅ Best Practice: Store tokens in Postman Environments and reference them with {token} instead of pasting manually.
Additional Testing Scenarios
-
Update Request (PUT/PATCH)
Test updating existing records with JSON payloads. -
Delete Request (DELETE)
Test deleting resources by ID. -
Negative Testing
Provide invalid data or omit required fields to verify error handling. -
Automated Test Scripts
Add JavaScript test snippets in Postman’s Tests tab to validate responses automatically, e.g.:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response has token", function () {
var jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("token");
});
Conclusion
By following this guide, you can use Postman to authenticate, test, and automate API requests.
This structured approach ensures:
- Secure authentication with tokens
- Organized request collections
- Automated validation with Postman scripts
- Clear separation of environments (Dev, Staging, Prod)
With these practices, you’ll have a reliable workflow for API testing and validation in any project.