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

$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 and create a new Modify Response 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:

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 rule in Requestly.

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:

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
  };
}
Updated on