How to Mock an API in ONE minute
Tags: webdev, tutorial, productivity, beginners
Published: 2022-11-18 06:45:23

Front-end developers usually meet this problem. The API is designed, but it's not been implemented yet. I need to make some fake data for my interface, which means I have to set up a mock server and add some response examples. If there are no response examples in the API documentation, I must make up some response data myself.

Each time I do this, I think it's not what a front-end developer should do. 

There should be a better way to accomplish this.

I tried many tools. I used postman as a mock server, but all response data must be added manually. I tried faker.js, but I have to write mocking code for every field.

In my expectation, an excellent mocking server should generate data itself. Developers should pay attention to real essential things.

Great news: Now, you can **mock an API in only one minute**. And you don't need to write any scripts.

## Why mocking

Mocking is a good choice if you're on one of the following occasions.

1. The API is designed but not developed. This usually happens in agile teams.
1. Restricted or paid APIs. For example, bank APIs. You cannot get real data before development completes. So you need mock data.
1. Offline environment or intranet. Mock data allows you to work on a local machine.

## Let‘s Mock it

There are two steps to mock the API.

1. Import API definitions.
2. Get the mock URL.

So easy. Let's mock it!

### Step 1 - Import API definitions

API documentation is necessary. OpenAPI (Swagger) is best, and any other format is OK, too.

Assume you have a YAML or JSON file. We’ll import it into this tool: [Apidog](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock-API-in-one-minute). If you don’t have an API yet, you can also create it in Apidog.

1.Create a new project in [Apidog](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock-API-in-one-minute).

![Create project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgfoi8b131h0s1q1kbre.png)
2.Go to "Settings"-"Import", and drag your API file in.

![Import API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y7u7fwmqkzswb5hvkj9.png)

More than ten formats of API documentation are supported. Click "Next" and your API will be imported.

Now you'll see the APIs in Apidog.

![API imported](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyjogm1znufwm7mfi2g5.png)

### Step 2 - Get the mock URL

1.Click an API in Apidog. You'll see an API definition page, in which there is a Section named "Mock".

2.Click the URL in the "Local Mock" part which starts with "127.0.0.1" to copy it.

![Mock URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rx4var72tt2blzt5v2m.png)

3.Paste it into your browser.

OK, it's done! You'll see a mocking JSON!

![Mocking JSON](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3q73y9xvb561qh5wnct.png)

A "city" field is filled by a city name, an "id" field is filled by an integer, and "createdAt" is filled by a date-time.

Now you can use the API data in your App.

And you don't need to write any scripts! Data in all fields are generated automatically.

Moreover, if you click "reload" in the browser, the data will refresh!

![Mocking JSON](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnhbodysym7qyry5arpl.png)

Amazing. 

## How it works

The first time I found this feature, I was quite surprised. Why can Apidog generate this? I didn't set anything!

Later I found this.

![Mocking rules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h22geh3o76scyl5wm23i.png)

There is a batch of built-in mocking rules in Apidog, as above. If your field name in the response definition matches one of them, the field data will be mocked automatically.

And Apidog starts a mock server in your local machine automatically. So you don't need to do anything in the server layer.

What if you don't like the data it mocks automatically? That's also easy.

You can directly fill the mock value of the field with Faker.js like this.

![Faker.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0buwc572v4vaxlskpgb2.png)

All Faker.js grammar is supported so that you can select them easily.

And if some field has a fixed value, you can also fill the "mock" with the specified value.

![Fixed value](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0gu9nemvxme8bpg42mk.png)

Alright, all done.

I hope you mock happily with [Apidog](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock-API-in-one-minute).

It's really a good tool. And I found it's not only good for mocking but for everything in API development.

[Download Apidog here](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock-API-in-one-minute).