mockfly logo

Introduction

When mocking APIs, our aim is to have the most realistic and diverse data possible. For instance, imagine you want a list of users: Would you like these users to always be the same? Do you want them to have authentic names? Can we make these names change with every request?

With Mockfly, we can generate random data for our endpoints, providing an experience as close as possible to when we consume a real API.

Generating random data

To generate random data, we utilize one of the best libraries available: Faker.js (v7.6.0). This library allows us to emulate numerous data types for use in our mock API responses.

Imagine wanting to generate a random name each time a request is made to your mock API. This can be easily accomplished by defining the endpoint response as follows:

{"name": "{{name.firstName}}"}

Now, when a request is made to our mock API, it will respond with random names, for example, "Ocie".

The list of methods available is extensive, aligning with those provided by Faker.js. These can be consulted at this link. As an illustration, to generate a dog breed name, we can use:

{"dog": {{animal.dog}}}

This can be employed wherever appropriate, as long as it's of the String type and follows the correct syntax. For example, it can be used within arrays and combined with non-random elements:

{
	"users": [{
		"name": {
			"firstName": "{{name.fullName}}"
		}
	}, {
		"name": {
			"firstName": "test"
		}
	}]
}

You can also use more than one Faker.js method in a string. For example, imagine you want to dynamically generate a name and gender in the same string. To do this, you can do the following

{"text": "{{name.firstName}} {{name.gender}}"}

And what the mock API will return will be something like this:

{"text":"Angelica Cisgender female"}

Using params to generate random data

Some Faker.js methods require parameters. In Mockfly, we can also make use of them. For instance, if we want a random 2-digit number, we can pass the parameters to the function as:

{"number": "{{random.numeric 2}}" }

This would return a random 2-digit number, such as: 42.

Using Some Very Useful Helpers

In addition to generating random data, we also have some useful helpers. For example, we may want the API to return one of the indicated elements for a field, and this would be done as:

{ "colors": "{{helpers.arrayElement red, green, yellow}}" }

This would return one of the specified colors, like:

{ "colors": "yellow" }

Another handy helper is the shuffle function, which randomizes the order of an array's elements. If we want our API to respond with a varied sequence, this can be achieved with the shuffle helper. Its use is similar to arrayElement. If we define our mock API response as:

{ "word": "{{helpers.shuffle cat,dog,mouse}}" }

Our mock API might respond with:

{ "word": ["dog", "mouse", "cat"] }

or

{ "word": ["mouse", "dog", "cat"] }

In essence, it takes the provided elements and rearranges the order of the array's elements.

If you're interested in exploring more methods to use in your mock API response, I recommend checking out the FakerJS API documentation.

Create a project