DEV Community

Cover image for Mocking APIs Made Simple: Pain Points, Solutions & Best Practices with EchoAPI
Mason Carter
Mason Carter

Posted on

Mocking APIs Made Simple: Pain Points, Solutions & Best Practices with EchoAPI

In everyday API development, mocking is almost unavoidable. Whether it’s fast iteration at the early stage of a project or when an endpoint isn’t ready yet, mocking ensures front-end development continues smoothly—even without real backend data.

1. Real-World Pain Points

Imagine this scenario:
You’re building a front-end for an e-commerce site. When a user clicks "Pay Now," your front-end calls the backend /pay endpoint, which should return something like:

{
    "data": {
        "code": 0,
        "message": "success",
        "pay_dtime":"2025-08-10 10:00:00",
        "order_id":"sn12345678"
    }
}
Enter fullscreen mode Exit fullscreen mode

But the problems are:

  • The backend isn’t ready, so you can’t test the post-payment flow.
  • The payment API depends on an external gateway, not configured in the test environment.
  • Some endpoints require complex authentication or data prep, making them unusable early.

Waiting on backend development blocks front-end progress—this is where mocking becomes critical.

2. Common Mocking Approaches & Limitations

2.1 Local JSON Files

Simplest approach: create mock/data.json and fetch it:

fetch('/mock/pay.json')
Enter fullscreen mode Exit fullscreen mode

Drawbacks:

  • Data is static; cannot simulate multiple scenarios.
  • Cannot handle complex logic like pagination or conditional queries.

2.2 Front-End Request Interception (axios-mock-adapter, Mock.js)

Intercept requests in the browser:

mock.onPost('/api/pay/confirm').reply(200, {
  "data": {
    "code": 0,
    "message": "success",
    "pay_dtime":"2025-08-10 10:00:00",
    "order_id":"sn12345678"
  }
});
Enter fullscreen mode Exit fullscreen mode

Drawbacks:

  • Only usable within the front-end project.
  • Data hardcoded in code; hard to maintain.

2.3 Self-Hosted Mock Server (json-server, Easy Mock)

Run a separate mock service:
Drawbacks:

  • High setup and maintenance cost.
  • Complex logic requires extra scripting; flexibility is limited.

These methods work for basic needs but lack flexibility and dynamic data generation as project complexity grows.

3. EchoAPI Mocking in Practice

EchoAPI offers a flexible and modern solution, supporting both rapid development and complex scenarios. Let’s take /pay as an example.

3.1 Fixed-Value Mock

Simulate a fixed response:

{
  "data": {
    "code": 0,
    "message": "success"
  }
}
Enter fullscreen mode Exit fullscreen mode

In EchoAPI, create a POST /pay API, define the response visually:

Mock Best Practices with EchoAPI .png

Then switch to the Mock tab to get the generated Mock URL:

Mock URL Best Practices with EchoAPI.png

Calling this URL returns the predefined response:

Calling Mock URL.png

3.2 Randomized Values

Add dynamic fields like pay_dtime. EchoAPI has built-in variables to generate random or time-based values:

{
  "data": {
    "code": 0,
    "message": "success",
    "pay_dtime":"2025-08-10 10:00:00"
  }
}
Enter fullscreen mode Exit fullscreen mode

Just insert a built-in date variable:

insert a built-in date variable in echoapi.png

Example response:

Mocks in API Development Example response.png

Now each request returns fresh data, avoiding static JSON limitations.

3.3 Custom Functions

For highly customized fields, like order_id starting with sn followed by 8 digits:

{
  "data": {
    "code": 0,
    "message": "success",
    "pay_dtime":"2025-08-10 10:00:00",
    "order_id":"sn12345678"
  }
}
Enter fullscreen mode Exit fullscreen mode

Use EchoAPI’s custom function:

EchoAPI’s custom function.png

Create fn_orderno using AI or manual logic:

Create  raw `fn_orderno` endraw  using AI or manual logic in echoapi.png

EchoAPI mock.png

Response example:

Response example in echoapi.png

3.4 Conditional Responses

Handle multiple scenarios: success, insufficient balance, account locked. Set expectations for "insufficient balance":

Conditional Responses in echoapi.png
Conditional Responses in echoapi1.png

Returned response:

Returned response in echoapi.png
Returned response in echoapi1.png

This approach is far closer to real-world behavior than static JSON.

4. Summary

Mocking is essential for front-end efficiency:

  • Front-end can develop independently of backend readiness.
  • QA can reuse mock links to test early.

EchoAPI Advantages:

  • Built-in variables: generate diverse data quickly.
  • Custom functions: implement complex business logic, AI-assisted if needed.

Whether you need rapid iteration, independent development, or realistic scenario simulation, EchoAPI delivers a flexible, professional, and time-saving solution that keeps your projects moving forward seamlessly.

Top comments (0)