> ## Documentation Index
> Fetch the complete documentation index at: https://interceptor-docs.requestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to conditionally fail requests based on request counts

Simulate a failed response on the first request and a successful one on subsequent attempts using [shared state](https://interceptor-docs.requestly.com/http-rules/advanced-usage/shared-state.md) in Requestly Interceptor.

> [!WARNING]
>
> `$sharedState` is only available in extension versions > v24.8.13 and desktop app versions > v1.7.1.

### Steps to conditionally fail requests are:

**1. Create a Modify Response Rule**

Open Requestly Interceptor and create a new **Modify Response** rule.

**2. Enable Dynamic JavaScript Modification**

Choose the **Dynamic JavaScript** option to write custom logic for the response.

**3. Add JavaScript to Control Response Based on Request Count**

Use the following code to fail the first request and let others succeed:

```javascript theme={null}
function modifyResponse(args) {
  const {
    method, url, response, responseType,
    requestHeaders, requestData, responseJSON
  } = args;

  // Get the current count or start from 0
  let count = $sharedState.count ?? 0;
  count++;
  $sharedState.count = count;

  // Fail only the first request
  if (count === 1) {
    return null;
  }

  // Allow all subsequent requests to succeed
  return response;
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.