How to Mock complex APIs in TWO minutes
Tags: webdev, tutorial, productivity, beginners
Published: 2022-11-21 12:15:34

Many developers love the solution I wrote in [*How to Mock an API in ONE minute*](https://dev.to/ismailkamil/how-to-mock-an-api-in-one-minute-eip/). In the article, I introduced an approach to mock an API rapidly.

But what I wrote is only a tiny part of the tool. After a period of exploring, I found more valuable features to mock complex data faster.

## Complex JSON

[My last article](https://dev.to/ismailkamil/how-to-mock-an-api-in-one-minute-eip/) introduced how to import a Swagger API into [Apidog](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock-complex-JSON) to mock some JSON data. Then you can have your mock data directly without any scripts.

But some fields are complicated; you can't mock them without any settings.

For example, **an enum field** has several optional values. It usually appears in status fields.

Or **an array with a fixed length**. It's often used in a table where each row has several attributes, and each page has 20 or 50 rows.

Or, **an object refers to a schema**. OpenAPI specification allows an API to refer to a schema as an object or part of an object.

Luckily, now we can quickly mock all these complex JSON in two minutes in [Apidog](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock-complex-JSON).

## Let's Mock it

There are three steps to mock the API.

1. Import API definitions.
2. Set specific fields.
3. Get the mock URL.

So easy. Let's mock it!

Step 1 is the same as [*How to Mock an API in ONE minute*](https://dev.to/ismailkamil/how-to-mock-an-api-in-one-minute-eip/). You just need an OpenAPI JSON or YAML and import it into Apidog.

![Imported API definitions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uesrzqxj4i6lkpbnky2i.png)

### Step 2. Set specific fields.

#### Enum values

In this sample API, the 'status' field has three optional values: available, pending, and sold. So I hope this field in my mocking data has only one of these values too.

Click 'More' in the 'status' field. You'll see an 'Enum' switch. Turn it on, and you'll be able to add enum values. After that, data of this field will be mocked according to the settings.

![Field: More](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87k93n83vhgsxz7w27ic.png)

#### Arrays with fixed length

PhotoUrls is an array field. I need it to have a fixed five items.

Click 'More' in the 'PhotoUrls' field. You'll see an 'Element Limit' setting. Set the minItems 5 and maxItems 5.

![Field: More](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3eh5eflzkffh88h88fff.png)

#### Object refers to a schema

Apidog support schemas in OpenAPI specification. So you just need to select a Schema for an object, and Apidog will mock the object according to the schema.

For example, the 'Category' field.

![Select schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o940nu7bzdsi5xw06z9b.png)

### Step 3. Get the mock URL.

Click ‘Save', and switch to the 'API' tab. Copy the URL in the "Local Mock" part which starts with "127.0.0.1".

![Copy the URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m264bceauolzvpgnnxoc.png)

Paste the URL into the browser. 

That's it.

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

Notice these fields: **photoURLs** with fixed item quantity(5), while URLs are mocked automatically with available image URLs; **status** with enum values(sold, pending, or available), and **category** refers to a schema. The other fields, like city and createdAt are mocked automatically. 

And if you reload the URL, the mock data refreshes.

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

## JSON Schema

OpenAPI 3.0 data types are based on JSON Schema, which describes the data structure of APIs.

Apidog is compatible with OpenAPI. Length range, format, and pattern(regular expression) of strings, min and max value of integers, readonly/writeonly; all attributes in JSON Schema are supported.

Not only JSON but XML is also supported to mock. You can even set the tag names and prefixes of XML data.

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

Based on JSON Schema, any complex mock data can be generated easily with a few mouse clicks. Still, you don't need to write any scripts.

Just enjoy it.

[Download Apidog here](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock-complex-JSON), and wait for my next post to see more powerful mock features.