how to create dummy api in postman

How to Create Dummy API in Postman

By Brian Garcia
AD

Creating a dummy API in Postman is an essential skill for any developer looking to test and demonstrate APIs. Dummy APIs mimic the behavior of real-world APIs, allowing you to simulate responses and handle various scenarios without the need for an active server. In this article, we will guide you through the process of creating a dummy API in Postman, enhancing your understanding and capabilities in API testing.

Before we dive into the creation process, let's briefly understand why dummy APIs are crucial. They enable you to:

  • Test API clients or applications without the need for backend integration.
  • Demonstrate API functionality during presentations or workshops.
  • Handle asynchronous or delayed responses effectively.

Setting Up the Environment

The first step in creating a dummy API in Postman is to set up the environment. An environment in Postman is a collection of variables that can be reused across various requests.

AD
How to Test APIs Using Postman – Step by Step
How to Test APIs Using Postman – Step by Step
Complete API Testing with Postman: Beginner to Advanced Guide 🧪
Complete API Testing with Postman: Beginner to Advanced Guide 🧪
Postman + APIs - Interactive Tutorial — Hands-on Activity! 🛫
Postman + APIs - Interactive Tutorial — Hands-on Activity! 🛫

To create an environment, follow these steps:

  • Click on the gear icon in the top-right corner to open the settings menu.
  • Select " Environments" from the list.
  • Click on "Add" to create a new environment. Name it something relevant, like "API demo".

Defining Variables

Now, let's define some variables in our "API demo" environment. We'll need two main variables: baseURL (the imaginary backend's URL) and delay (to simulate API response time).

10 Postman Hacks for Faster API Testing
10 Postman Hacks for Faster API Testing
Master API Testing with Postman: From Basics to Automation 🧪
Master API Testing with Postman: From Basics to Automation 🧪
Postman Collections Explained: Features, Benefits & How to Create Collections
Postman Collections Explained: Features, Benefits & How to Create Collections

Here's how to do it:

  • Under the "API demo" environment, click on "Add" to create a new variable.
  • Name the variable "baseURL" and set its value to an imaginary backend URL, like https://API.demo.com/v1/.
  • Repeat the process to add another variable called "delay" and set its value to a realistic delay time, like 3000 milliseconds.

Using Variables in Requests

With our environment set up and variables defined, we can now use them in our requests to create a dummy API.

AD

To use variables:

a poster with the words testing from basicss on it and an image of a computer monitor
a poster with the words testing from basicss on it and an image of a computer monitor
Postman on Linux System: The Best API Tool for Software Developer
Postman on Linux System: The Best API Tool for Software Developer
📋 Streamlining API Testing with Postman Environment Variables Hardcoding API tokens, passwords, and server URLs directly into your request parameters creates security risks and testing friction. Postman environment variables allow you to manage dynamic configuration data seamlessly across stages. 💡 Key Highlights: • Environment variables store reusable parameters like base URLs and authentication tokens in a single centralized place. • Switching between development, staging, and production en...
📋 Streamlining API Testing with Postman Environment Variables Hardcoding API tokens, passwords, and server URLs directly into your request parameters creates security risks and testing friction. Postman environment variables allow you to manage dynamic configuration data seamlessly across stages. 💡 Key Highlights: • Environment variables store reusable parameters like base URLs and authentication tokens in a single centralized place. • Switching between development, staging, and production en...
Postman Tutorial #88 - Setup Jenkins Job with Postman API and Manage Secrets
Postman Tutorial #88 - Setup Jenkins Job with Postman API and Manage Secrets
the logo for postman web utopia
the logo for postman web utopia
How to Use Postman Scripts to Simplify Your API Authentication Process
How to Use Postman Scripts to Simplify Your API Authentication Process
  • When writing the request URL, use the variable syntax: {{baseURL}} followed by the endpoint, e.g., {{baseURL}}users.
  • To simulate a delay, use the delay variable in the request.body or request.headers section, e.g., delay: {{delay}}.

Building a Dummy API

Now that we have our environment and variables set up, let's create a simple dummy API with two endpoints: GET for retrieving users and POST for adding a new user.

Follow these steps for each endpoint:

AD

Creating the GET Endpoint

To create a GET endpoint, follow these steps:

Postman API testing tool
Postman API testing tool
7 Postman Features Every Backend Developer Should Know 🟠💻 | API Testing Guide
7 Postman Features Every Backend Developer Should Know 🟠💻 | API Testing Guide
the diagram shows how to use postman's environment for testing and analyzing data
the diagram shows how to use postman's environment for testing and analyzing data
  • Click on "New" in the top-left corner to create a new request.
  • Set the request method to GET.
  • In the Address bar, enter the URL using the variable syntax: {{baseURL}}users.
  • To simulate a response, click on the "Body" tab and select "Raw". Set the radio button to Text.
  • To mimic a successful response, enter a simple JSON array of users, like this:
    [
      {"id": 1, "name": "John Doe"},
      {"id": 2, "name": "Jane Smith"}
    ]
  • To simulate a delay, add a "Codigo" header with the key " delaying" and the value "delay: {{delay}}" (ensure there are quotes around the delay variable).

Creating the POST Endpoint

To create a POST endpoint, follow these steps:

  • Click on "New" to create another request.
  • Set the request method to POST.
  • In the Address bar, enter the URL using the variable syntax: {{baseURL}}users.
  • In the Body section, add a raw JSON object representing a new user, like this:
    {
      "id": 3,
      "name": "New User"
    }
  • To mimic a successful creation, in the "Tests" tab, add a test script to check for the "location" header and its value, e.g., `pm.test("Location header should contain the new user id"), pm.expect(pm.response.headers.get("location")).to.include("3");`.

Congratulations! You've just created a simple dummy API in Postman. This dummy API can now be used to demonstrate or test other services and applications.

AD

In the ever-evolving world of APIs, mastering the art of creating dummy APIs in Postman will not only boost your confidence but also help you stand out as a versatile developer. So go ahead, experiment, and create more complex dummy APIs, pushing your API testing skills to the next level. Happy hacking!

AD