Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Designing

API Mock

API Automated Testing

Sign up for free

Step-by-Step Guide to Mastering Mock APIs with Apidog

Start for free
Contents
Home / Applied skills / Step-by-Step Guide to Mastering Mock APIs with Apidog

Step-by-Step Guide to Mastering Mock APIs with Apidog

Mock API is a technique that replaces the real API interface with simulated data. With Apidog, it only takes 3 steps to set up a Mock API.

Mock API is a technique that uses simulated data instead of a real API interface to return data. In the development process, front-end developers often rely on back-end APIs, but the back-end APIs are still under development. With Mock API, front-end developers do not have to wait for the completion of backend API development. They can use the simulated data returned by Mock API to develop their front-end applications.

Apidog is a powerful and user-friendly API management tool. With Apidog, you can easily create Mock APIs in just three steps.

button

Step 1. Create a New Project

Apidog uses projects to manage APIs. You can create multiple APIs under one project. Each API must belong to a project. To create a new project, click the "New Project" button on the right side of the Apidog app homepage.

Create a New Project

Step 2. Create a New API

To demonstrate the creation of an API for user details, follow these steps:

  • Request method: GET.
  • URL: api/user/{id}, where {id} is the parameter representing the user ID.
  • Response type: json.
  • Response content:
{
id: number, // user id
name: string, // username
gender: 1 | 2, // gender: 1 for male, 2 for female
phone: string, // phone number
avatar: string, // avatar image address
}

To create a new interface, go to the homepage of the previously created project and click on the "+" button on the left side.

img
Create a New Interface

Fill in the corresponding interface information and save it.

Parameters
Responese

With this, the user detail interface has been created. Meanwhile, Apidog has automatically generated a mock for us based on the format and type of the response fields. Click on the " Request" button under Mock to view the mock response.

API Request

Let's take a look at the mock response. Click on the "Request" button and then click "Send" on the page that opens.

img
Send the Request

Step 3. Set Mock  Matching Rules

Have you noticed something magical? Apidog set the "name" field to string type, yet it returns names; set the "phone" field to string type, yet it returns phone numbers; set the "avatar" field to string type, yet it returns image addresses.

The reason is that Apidog supports setting matching rules for Mock. Apidog has built-in rules, and users can also customize their own rules. These rules can be found in Project Settings > Feature Settings > Mock Settings.

 Set Mock  Matching Rules

You can also set dedicated Mock rules for each field. Click on the "Mock" next to the field:

Mock

Apidog Mock matching rules are as follows:

  1. When a field meets a matching rule, the response will return a random value that satisfies the Mock rule.
  2. If a field does not meet any matching rule, the response will return a random value that meets the data type of the field.

There are three types of matching rules:

  1. Wildcards: * matches zero or more characters, and ? matches any single character. For example, *name can match user name, name, and so on.
  2. Regular expressions.
  3. Exact match.

Mock rules are fully compatible with Mock.js, and have extended some syntax that Mock.js does not have (such as domestic phone numbers "@phone"). Commonly used Mock rules include:

  1. @integer: integer. @integer(min, max).
  2. @string: string. @string(length): specifies the length of the string.
  3. @regexp(regexp): regular expression.
  4. @url: URL.

During development, we may encounter flexible and complex Mock scenarios, such as returning customized content based on different request parameters. For example, return normal user information when the ID is 1, and report an error when the ID is 2. Apidog also supports these scenarios, and those interested can check out the Advanced Mock document.

Conclusion

In summary, this article introduces how to use Apidog to create Mock APIs. Apidog also supports other features such as API documentation, API debugging, and API automation testing. Give it a try!

button