Skip to main content

Command Palette

Search for a command to run...

Postman Setup & API Testing for Your NestJS Blog (Beginner‑Friendly Guide)

Published
3 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

1) Install Postman

  1. Go to postman.com/downloads.

  2. Download the installer for your OS (Windows/macOS/Linux).

  3. Install and open Postman. If you’re prompted to sign in, you can choose “Use the lightweight API client” to skip account setup.

Tip: Keep Postman updated—it ships frequent improvements to the client and testing features.


2) Start your NestJS app (local)

In your project folder:

npm run start:dev

By default, NestJS serves on http://localhost:3000 (unless you changed the port in main.ts).


3) Create a Postman Environment

Environments let you avoid hard‑coding URLs/tokens.

Create a new environment named “Local (Nest Blog)” with variables:

  • base_urlhttp://localhost:3000

  • token → leave empty for now (we’ll set it after login)

You’ll reference variables like this: {{base_url}} and {{token}}.

Switch the active environment (top‑right environment selector in Postman).


4) Create a Collection for your API

Click Collections → New Collection and name it “Blog API (Local)”. Inside it, create folders:

  • Auth (login, refresh)

  • Users

  • Posts

  • Tags (later)

We’ll add requests to these folders as we build features.


5) Your first request (GET /)

In the Blog API (Local) collection, add a request:

  • Method: GET

  • URL: {{base_url}}/

  • Click Send → you should see the default Hello World JSON/string from your Nest app.


6) Working with routes: params & query

A. List posts with pagination

  • Method: GET

  • URL: {{base_url}}/posts?limit=10&offset=0

  • Save it as Posts → List posts.

B. Posts by author (path param)

  • Method: GET

  • URL: {{base_url}}/posts/author/:authorId
    In Postman, click ParamsPath Variables and set authorId to 42. Save as Posts → Posts by author.


7) Sending JSON bodies (create & update)

Always set the header Content-Type: application/json when sending JSON.

A. Create a post

  • Method: POST

  • URL: {{base_url}}/posts

  • Headers: Content-Type: application/json, Authorization: Bearer {{token}} (optional until auth is added)

  • Body → raw → JSON

{
  "title": "My first NestJS post",
  "description": "Hello, world!",
  "authorId": 42
}

Save as Posts → Create post.

B. Replace a post (PUT)

  • Method: PUT

  • URL: {{base_url}}/posts/:id (set id path variable)

  • Body (full resource): send the complete post object.

C. Partially update a post (PATCH)

  • Method: PATCH

  • URL: {{base_url}}/posts/:id

  • Body (partial)

{
  "title": "Updated title only"
}

D. Delete a post

  • Method: DELETE

  • URL: {{base_url}}/posts/:id

PUT vs PATCH: PUT replaces the entire resource; PATCH updates selected fields.


8) Headers & Authorization (Bearer JWT)

As we add auth later, most endpoints will require a token. In requests that need auth, include:

Authorization: Bearer {{token}}

You can set this in the request’s Headers or in the Authorization tab (Type: Bearer Token → {{token}}).

Auto‑inject the token for the whole collection

In the collection Authorization tab, set Type = Bearer Token and Value = {{token}}. Child requests inherit this, so you don’t have to repeat it.


9) Save the token automatically after login

When you implement POST /auth/login (later), have it return a JSON like:

{ "access_token": "<jwt>" }

In the Tests tab of your Auth → Login request, add:

pm.test('status is 200', function () {
  pm.response.to.have.status(200);
});

const data = pm.response.json();
if (data && data.access_token) {
  pm.environment.set('token', data.access_token);
}

Now, once you log in, Postman saves {{token}} to the environment automatically.


10) Add lightweight tests to any request

Open the Tests tab and paste checks like:

pm.test('response is JSON', function () {
  pm.response.to.be.json;
});

pm.test('request succeeded', function () {
  pm.expect(pm.response.code).to.be.oneOf([200, 201]);
});

pm.test('responded quickly', function () {
  pm.expect(pm.response.responseTime).to
L

Great guide on setting up Postman for testing your NestJS blog API! The step-by-step instructions make it easy to understand how to send requests, validate responses, and automate tests. For local development, tools like ServBay can simplify the setup process, allowing you to focus more on coding and less on environment configuration. Definitely worth checking out if you're looking to streamline your dev workflow.