How to Use Mockaroo for API Mocking: A Complete Guide

Photo by Walkator on Unsplash

How to Use Mockaroo for API Mocking: A Complete Guide

·

7 min read

When building, testing, or developing any application or service that interacts with APIs, having data to work with is essential. However, real-world data might not always be readily available, especially during the early stages of development. This is where mock APIs and synthetic data come into play, and one tool that stands out for creating such data is Mockaroo.

Mockaroo is a powerful tool for generating realistic yet fake data that can simulate production-ready datasets. It saves developers from the headache of manually writing sample data or relying on incomplete data sources when mocking APIs. In this complete guide, we’ll dive deep into how to use Mockaroo for API mocking, focusing on creating, customizing, and integrating mocked data for your projects.

Before we get started, let’s introduce an alternative extension called APIDog, which serves as another powerful Postman alternative.

main interface

APIDog is a VSCode extension designed to simplify API testing by offering a feature-rich, yet lightweight solution directly within your editor. Like REST Client, APIDog allows you to send requests, manage environments, and analyze responses. It provides a balance between raw, script-based testing and the convenience of a graphical interface, making it accessible for both beginners and advanced users alike. While this guide primarily focuses on using the REST Client extension, it’s worth exploring APIDog as an option if you’re looking for slightly different functionality or a more intuitive environment that still integrates seamlessly into VSCode.


What is Mockaroo?

Mockaroo is an online tool that allows developers to generate synthetic data in various formats such as JSON, CSV, Excel, SQL, and more. What makes it unique is its ability to create realistic, random, and customizable datasets tailored to your specifications. With Mockaroo, you can mock APIs that mimic the behavior of real-world APIs, helping you test features and functionalities without needing access to the actual backend.

For instance, you can create a synthetic API that provides fake customer or product data for your e-commerce app. During development, this mock API can act as a placeholder for your real API, ensuring your application logic works seamlessly before live data becomes available.


Why Use Mockaroo for API Mocking?

Before diving into how to use Mockaroo, let’s understand why it’s such a game-changer for developers:

  1. Customizable and Realistic Data: Mockaroo offers dozens of predefined data types (names, email addresses, countries, URLs, etc.) and gives you the ability to customize field types for specific needs.

  2. Saves Time: Generating data manually can be a tedious task. Mockaroo automates this process, significantly reducing the effort and time needed to create sample datasets.

  3. Prototyping APIs: Mockaroo enables you to simulate APIs with endpoints that return custom JSON payloads. This is incredibly helpful if your backend isn’t ready but you need something to work with while developing the frontend.

  4. Supports Testing: Mocked APIs and synthetic data can be used to test applications, ensuring the code behaves correctly without relying on live systems.

  5. Ease of Use: Mockaroo is beginner-friendly yet powerful, making it accessible regardless of your level of expertise.


How to Get Started with Mockaroo

Follow these steps to get started with Mockaroo and set up your mock API in no time.

1. Sign Up for Mockaroo

To use Mockaroo, start by navigating to Mockaroo's website. You can use many of its features without creating an account, but signing up for a free account allows you to save your schema configurations and access the API functionality.

Once logged in, you’ll be greeted by a simple and intuitive interface designed to make data generation easy.


2. Design a Schema

The schema defines the structure and fields of the data you want to generate. Each field corresponds to a column or key in your dataset, with a specified type of data.

Steps to Create a Schema:

  1. Add Fields: The first step is to add fields to your schema. Click on the “Add Another Field” button for each field you want to include. For example, if you’re mocking a "Users" API, fields could include id, name, email, and phone.

  2. Select Field Types: For each field, Mockaroo provides predefined data types (over 140 options!). Select the type that fits your needs:

    • For name, you can choose Full Name.

    • For email, you can select Email Address.

    • For dates, you can use Date or Timestamp.

  3. Set Field Properties: Customize each field to meet your requirements. For instance:

    • Adjust the maximum and minimum values for numbers.

    • Customize date ranges for time-based fields.

    • Include static or formula-based fields for generating derived data.

  4. Preview Your Data: Mockaroo has a Preview feature that lets you see a small sample of the data before downloading or using it. Adjust your schema until the preview data looks just right for your use case.

Example Schema for a Users API:

Field NameTypeExample Value
idNumber101
nameFull NameJohn Doe
emailEmail Addressjohndoe@example.com
created_atTimestamp2023-01-05 15:30:00

3. Generate and Download Data

Once your schema is ready, you can generate data in various formats:

  1. Adjust the number of rows to create as many entries as you need (up to 1,000 rows in the free version, with options to pay for larger datasets).

  2. Choose an output format (JSON, CSV, SQL, Excel, or even a custom format).

  3. Download the dataset and use it in your project.

Example: Generating a JSON dataset for testing your frontend would yield an output like this:

[  
  {  
    "id": 1,  
    "name": "Jane Smith",  
    "email": "jane.smith@test.com",  
    "created_at": "2023-01-01T10:15:30Z"  
  },  
  {  
    "id": 2,  
    "name": "John Doe",  
    "email": "john.doe@test.com",  
    "created_at": "2023-01-02T12:00:45Z"  
  }  
]

4. Set Up an API Endpoint

Mockaroo isn’t limited to generating static datasets—it also lets you create mocked API endpoints. This is especially useful for frontend projects or third-party integrations where you need an API to emulate real-world behavior.

Steps to Create a Mock API Endpoint:

  1. Navigate to the "API" Section: In Mockaroo, click on the “API” tab.

  2. Create a New API:

    • Set the name of your API (e.g., "Mock Users API").

    • Select the schema you created earlier. The API will use this schema to generate the response.

  3. Customize Response:

    • Provide a Base URL for the mock API (Mockaroo generates this for you).

    • Customize the number of records the API should return per request (e.g., 10 users).

    • Specify the response format, such as JSON.

  4. Test the API: Once the API is set up, Mockaroo gives you the endpoint URL. Use a tool like Postman, curl, or your frontend application to make GET requests to the API and view the mock data.

Example API Endpoint:

https://api.mockaroo.com/api/customers?key=your_api_key

Response from GET request:

[  
  {  
    "id": 101,  
    "name": "Alice Cooper",  
    "email": "alice.cooper@example.com",  
    "created_at": "2023-02-15T12:34:56Z"  
  },  
  {  
    "id": 102,  
    "name": "Bob Marley",  
    "email": "bob.marley@example.com",  
    "created_at": "2023-02-16T14:22:10Z"  
  }  
]

5. Integrate Your Mock API

After setting up your Mockaroo API, you can integrate it into your development workflow.

Use Cases for Integration:

  • Frontend Testing: Your frontend application can make requests to the mocked API, simulating interactions with a backend service.

  • Endpoint Prototyping: While your real API is under development, your mocked API acts as a stand-in, helping teams work independently.

  • Load Testing: Mockaroo makes it easy to generate large datasets for load and performance testing of your systems.


6. Customizing Mockaroo Features

Mockaroo provides additional features to customize the mocked data and API even further:

  • Dynamic Responses: Use formulas and logic to generate dynamic field values that depend on each other. For example, a status field might depend on the value of an order_total field.

  • Scalability: Generate thousands or even millions of records if you upgrade to Mockaroo’s paid plans.

  • Custom APIs: Use Mockaroo’s API customization tools to mimic specific headers, query parameters, or HTTP methods tailored to your app's needs.


Tips for Using Mockaroo Effectively

  1. Think About Production Data: Consider the structure and properties of your actual production data to make your mocked API as realistic as possible.

  2. Iterate: Preview and tweak your dataset or API multiple times to ensure it fits your application’s needs.

  3. Use Mockaroo API Keys Securely: Ensure you securely store and handle API keys required for accessing Mockaroo’s API endpoints.

  4. Combine Mockaroo with Other Mocking Tools: While Mockaroo focuses on synthetic data, tools like WireMock can simulate API behavior, adding deeper functionality to your mocks.


Conclusion

Mockaroo is a versatile and easy-to-use tool for developers who need to mock APIs and generate realistic synthetic data. Whether you're testing your frontend, prototyping an app, or preparing for system integrations, Mockaroo simplifies the process, saving you time and effort.

From designing schemas and creating datasets to setting up mocked endpoints, Mockaroo has you covered. The tool empowers developers to work without being bottlenecked by unavailable backend systems or incomplete data sources, allowing teams to develop, test, and innovate faster.

So the next time you need mock data or APIs, let Mockaroo come to the rescue! You’ll find your workflow smoother than ever.