Create Local API Mocks

Learn how to mock APIs locally with ease using Requestly

Introduction

Creating mock APIs locally with Requestly is a powerful way to simulate server responses directly in your development environment. This feature is invaluable for developers looking to test features, handle edge cases, or debug applications without relying on live servers.

On this page, you'll see how to use Requestly’s Modify API Response rule to mock API responses and create bulk mocks using recorded sessions, making your workflow faster and more efficient.

Steps to Create a Mock API Using the Modify Response Rule

The Modify API Response rule allows you to intercept and alter API responses without needing changes on the server. It’s a powerful feature for testing, debugging, and mocking APIs during development.

This rule is available on both the Requestly desktop app and the browser extension, giving you flexibility across environments.

To create a mock API, you can use this rule to intercept and return a static response for a specific request without sending it to the server.

1

Open the Requestly Dashboard

Log in to your Requestly account and navigate to the HTTP Rules section.

2

Create a New Modify Response Rule

Here click on New rule and select Modify API Response as the rule type.

3

Configure the Rule

Set up your Modify API Response rule with the necessary configurations. We'll use Requestly Playground as a demo for this example:

You can tick the “Server this response body without making call to the server” checkbox as the label suggests it will ensure you get the response locally without hitting your API server.

For a detailed breakdown of all available options, refer to the Modify API Response Rule page.

4

Save and test the Rule

Save your rule and visit the website to test your mock api

Bulk API Mocking (Desktop App Only)

Bulk API Mocking allows you to mock multiple API endpoints at once using recorded sessions. This helps replicate API responses for testing or development without hitting the live server.

Recording a Session for Bulk API Mocking

Record a Session

If you haven’t already recorded a session, start by capturing the API requests and responses you want to mock using Requestly’s interceptor and save it in a session, start by:

1

Open Requestly Desktop app

Open the Requestly Desktop App and navigate to the network traffic tab on the left sidebar.

2

Open the Connect Panel

Click any of the Connect Apps buttons to open the Connect Panel, which displays all supported apps Requestly can connect to.

3

Select the Application

Choose the application you want to connect to from the Connect Panel.

4

Capture API Requests

Perform the actions or user flows in your app or website to trigger the API requests you want to record. Requestly will log all requests and responses automatically.

5

Stop Recording

Once you've captured all the necessary API requests disconnect the app.

6

Save the Session

Click the Save button to store the recorded session. This saves all the captured API calls, allowing you to generate mock rules from them later.

Using Recorded Sessions for Bulk API Mocking

Once you've recorded an API session, you can easily generate bulk mock rules based on the captured requests. This is a quick way to mock multiple API endpoints without manually configuring each one.

1

Go to the Sessions Tab

Navigate to the Sessions tab in the left sidebar of the Requestly dashboard.

2

Select the Session for Mocks

From the list of recorded sessions, select the session you want to use for mocking.

3

Create Mocks from the Session

Click on Create Mocks from This Session in the top-right corner of the session page.

4

Choose the Resource Type

In the dropdown, select the type of resource you want to mock: REST or GraphQL. Then, click Proceed.

If you're mocking GraphQL requests, specify the keys (e.g., operationName) and values (e.g., ExampleQuery) to filter out the GraphQL requests you want to mock. This is useful to target specific GraphQL operations and ensure you're mocking only the relevant queries.

5

Select Requests or API Calls for Mocking

In the traffic table, choose the responses you want to create mocks for. You can select individual requests or multiple requests as needed.

6

Choose Matching Condition

Click on the Create Mocks button and select the matching condition from the dropdown:

  • Match Entire URL - Matches the full URL, including protocol, host, path, and query parameters. eg: https://mock.redq.io/api/products?limit=5 matches only if everything is identical.

  • Match Only Path - Matches just the path, ignoring protocol, host, and query parameters. eg: /api/products matches regardless of https://mock.redq.io or https://demo.redq.io

  • Match Path and Query Params - Matches the path and query parameters together. Example: /v1/users?id=123 matches only if both are identical.

7

Create Rules

Click Create Rules to generate the mock rules. Requestly will automatically create a collection of Modify Response rules, all enabled and ready to use.

To disable the mock you can either disable the whole collection or individual rules

These rules will intercept the API requests and return the mocked responses you configured, simplifying the process of mocking multiple APIs at once.

Updated on