API Testing with Postman, Step by Step
Postman lets you build and send API requests with a form instead of code. It is the fastest way to explore a new API, and most teams share their Postman collections.
Step 1: Send your first request
- Install the Postman app (free) and create a workspace.
- Click New, then HTTP.
- Leave the method as
GET, enterhttps://jsonplaceholder.typicode.com/users/1and click Send. - Look at the bottom panel: the body, the status
200 OK, the time in ms and the headers.
Step 2: Send a POST with a body
- Change the method to
POSTand the URL tohttps://jsonplaceholder.typicode.com/posts. - Open Body, choose raw and JSON, then paste:
{ "title": "Hello", "body": "First post", "userId": 1 } - Send. You should get
201 Createdand the post back with a newid.
Step 3: Save requests in a collection
A collection is a folder of saved requests, for example "Posts API" with Get post, Create post, Update post and Delete post. You can run a whole collection with one click and share it with the team.
Step 4: Use environment variables
Do not type the server address into every request. Create an environment called "Practice" with a variable:
baseUrl = https://jsonplaceholder.typicode.com
Then use {{baseUrl}}/posts/1 in the URL. To test another server (dev, staging, production), switch the environment, not every request.
Step 5: Write tests
Open the Scripts (or Tests) tab of a request and add JavaScript checks that run after every response:
pm.test("status is 200", () => {
pm.response.to.have.status(200);
});
pm.test("response is fast enough", () => {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
pm.test("user has the right fields", () => {
const user = pm.response.json();
pm.expect(user.id).to.eql(1);
pm.expect(user.email).to.include("@");
pm.expect(user).to.have.property("address");
});
Results appear in the Test Results tab.
Step 6: Chain requests
Save a value from one response and use it in the next request. In the tests of "Create post":
pm.environment.set("postId", pm.response.json().id);
Then "Get post" can call {{baseUrl}}/posts/{{postId}}. This is how you test real flows: log in, save the token, create an order, check the order.
Step 7: Run the collection
Click Run on the collection to run every request and test in order. To run it in CI, export the collection and use Newman, Postman's command-line runner:
npm install -g newman
newman run posts.postman_collection.json -e practice.postman_environment.json