Paginated Mock APIs using Mockaroo

Venkateswarlu Vajrala
3 min readFeb 7, 2024

--

I recently found this tool called Mockaroo which is providing free (200 requests per day) test data generation and also mock API’s.

It proved quite useful for constructing UI components without the necessity of developing a backend, all while simulating API calls. This allowed us to generate realistic data instead of random sets.

Mockaroo boasts a feature called Projects, which comprises schemas, datasets, and scenarios that can be shared with collaborators. This functionality proves valuable when collaborating on a project.

While Mockaroo provided all the necessary features, such as custom schema creation, datasets, and schema relations with foreign keys, it lacked built-in pagination support for Apis. So, I proceeded to construct one using the available built-in features.

In this blog post, we will explore how to simulate pagination behaviour in Mockaroo.

Scenarios:

Whenever API call is made, we want to return 10 users per page, and it should also have a property indicating next page availability.

This gives us 2 scenarios.

  1. If user details are available for the page requested then it should return id, name and email of the user.

2. If user details are not available for the page requested, then it should return an empty array.

Now that we have our scenarios defined, Let’s create schema for this.

Schema Creation:

  1. Go to Mockaroo schemas page.
  2. Click on New Schema
  3. Add the fields as shown below.
  4. Save this schema as users.
  • result is JSON Array as it needs to include array of Json objects.
  • To insert a property inside result, we simply need to use dot notation.
  • ID property of result is formula. Formulas are useful when we want to define a property based on other property values. In here ID value is defined based on page number and current row number.
  • Adding __ (two underscores) before a property makes it hidden in response. So, it won’t returned as one of result property. This is useful in our case where we can take advantage of built in row number and page number param to create unique user_id.
  • You can see all the supported functions by clicking on question mark next to formula field value.

Note: It uses Ruby language for formulas

  • Below formula conditionally sets hasNexPage param to true if page number is less than 5 otherwise false.
if request_params["page"].to_i >=4 then false else true end

Mock API creation:

  1. Go to Mockaroo API page.
  2. Update handler script with below code snippet.
  3. Save the API with method and route defined.

Handler script provides caching response, error handling, response altering. We are using handler script to write a simple conditional response. If the page number is greater than 4 then we are simply returning empty result otherwise returning the actual data.

schema "users"
data = generate 1
if params["page"].to_i >= 5
{hasNextPage: false,result:[]}
else
data
end

Interested in crafting your own Mock APIs? wait no further and checkout Mockaroo.

I have put some more examples in this repository: venkyvajrala/mockaroo-examples (github.com)

--

--