API Testing · Testing APIs by Hand

API Testing with Postman, Step by Step

Lesson 3 of 7 · 11 min read

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

  1. Install the Postman app (free) and create a workspace.
  2. Click New, then HTTP.
  3. Leave the method as GET, enter https://jsonplaceholder.typicode.com/users/1 and click Send.
  4. 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

  1. Change the method to POST and the URL to https://jsonplaceholder.typicode.com/posts.
  2. Open Body, choose raw and JSON, then paste:
    { "title": "Hello", "body": "First post", "userId": 1 }
  3. Send. You should get 201 Created and the post back with a new id.

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
Note: JSONPlaceholder is fake. A POST returns 201 but nothing is really saved, so a GET for the new id returns 404. On a real API, that same check would be a bug.
All 7 lessons in API Testing
  1. What Is API Testing?
  2. HTTP Basics for Testers: Methods, Status Codes, Headers
  3. API Testing with Postman, Step by Step
  4. What to Test in an API: A Complete Checklist
  5. Testing API Authentication: API Keys, Tokens and OAuth
  6. Automate API Tests with REST Assured (Java)
  7. API Test Framework Structure and CI