> ## 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.

# Use Shared State to Aggregate Data Across Rules

You can store response data from one API call and use it in the request body of another. This is done using `$sharedState`, allowing seamless data sharing between Modify Response and Modify Request rules

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

### Aggregate Data from API Response

**1. Create a Modify Response Rule**

Open Requestly Interceptor and create a new [Modify Response](https://interceptor-docs.requestly.com/http-rules/rule-types/modify-response-body.md) rule.

**2. Select Dynamic Modification (JavaScript)**

Choose **Dynamic JavaScript Modification** in the rule options

**3. Store Response Data in \$sharedState**

Use the following code to store response values like `products` and `currency`:

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

  // Extract data from response
  const productList = responseJSON.products;
  const currency = responseJSON.currency;

  // Store in shared state
  $sharedState.productList = productList;
  $sharedState.currency = currency;

  return response;
}
```

## **Use Aggregated Data in Request Body**

**1. Create a Modify Request Body Rule**

Create a new [Modify Request Body](https://interceptor-docs.requestly.com/http-rules/rule-types/modify-request-body.md) rule in Requestly Interceptor.

**2. Select Dynamic Modification (JavaScript)**

Select the **Dynamic Modification** option.

**3. Use Data from \$sharedState in the Request**

**Inject the stored products and currency into your outgoing request**\
Here’s the code to include the previously saved data:

```javascript theme={null}
function modifyRequestBody(args) {
  const { method, url, body, bodyAsJson } = args;

  // Retrieve shared state values
  const products = $sharedState.productList;
  const currency = $sharedState.currency;

  // Inject into request body
  return {
    ...bodyAsJson,
    products,
    currency
  };
}
```


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