> ## Documentation Index
> Fetch the complete documentation index at: https://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**.
</Warning>

### Aggregate Data from API Response

<Steps>
  <Step title="Create a Modify Response Rule">
    Open Requestly Interceptor and create a new [Modify Response](/http-interception/http-rules/rule-types/modify-response-body) rule.
  </Step>

  <Step title="Select Dynamic Modification (JavaScript)">
    Choose **Dynamic JavaScript Modification** in the rule options
  </Step>

  <Step title="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;
    }
    ```
  </Step>
</Steps>

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

<Steps>
  <Step title="Create a Modify Request Body Rule">
    Create a new <a target="_blank" href="/http-interception/http-rules/rule-types/modify-request-body">Modify Request Body</a> rule in Requestly Interceptor.
  </Step>

  <Step title="Select Dynamic Modification (JavaScript)">
    Select the **Dynamic Modification** option.
  </Step>

  <Step title="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
      };
    }
    ```
  </Step>
</Steps>
