Rules

In order to work with the rules API you first need to understand the structure of the rules data.

Rule schema

The following are the most important attributes inside a rule object.

PropertyUsageRequiredTypeDescription
idAuto generated Read-OnlyNoStringUnique ID for your rule. This starts with the rule type followed by a 5 character long unique string. For example id of a redirect rule could be Redirect_fohh4
nameUser defined Required Can be updatedYesStringName of the Rule
descriptionUser defined Optional Can be updatedNoStringDescription of the Rule
statusCan be updated Inactive by defaultNoStringCan be either Inactive or Active
groupIdAn empty groupId denotes an ungrouped rule. So this is required if you want to keep the rule groupedNoStringDefines the group the rule is a part of. Groups are separate objects that are not currently editable by the API. But you can update this property to another existing group.
ruleTypeRequired Can not be changed after creation.YesStringTakes one of the following values: Redirect Cancel Replace Headers UserAgent Script QueryParam Response Request Delay
pairsRequired Should always match the schema based on the rule typeYesArray of PairsArray of pairs that are part of the rule. Read more about pairs below

Rule pairs

Every rule has an array of rule pairs that contain:

  1. The matching condition inside the source property
  1. The rest of the details for the rules

The details and their schema vary based on the type of the rule,

But the schema of the source property is consistent throughout all the rules.

Source Schema

This is common for all Rule types. You can get a better understanding about what each operator does by going through the user docs

PropertyUsageTypeDescription
keyRequiredStringThe key for the source. Can be one of the following values: Url, host, path
operatorRequiredStringThe operator for matching. Can be one of the following values: Equals, Contains, Matches, Wildcard_Matches
valueRequiredStringThe value to match against
filtersOptionalArray of Filter objectsFilters are used for more accurate targeting of the requests

Filters

To learn more about the filters, read the user documentation on advanced rule targetting. Filters have the following schema:

PropertyUsageTypeDescription
requestMethodOptionalArray of StringAn array of HTTP method strings like GET, POST, etc.
resourceTypeOptionalArray of StringThe type of resource being requested. Possible values listed below.
URLOptionalObjectHost Page URL. This is similar to the source object, except it only requires the operator and value properties, both are required

The possible values for resourceType in the filters object are

  1. xmlhttprequest - To target only XHR requests
  1. script - Requests for javascript files
  1. stylesheet - Request for css files
  1. image - Request for images. These include files like png, jpeg, etc.
  1. media - Request for any media files, like video, audio, gifs, etc.
  1. font - Request for any font files like .ttf, .otf, etc
  1. websocket - All websocket requests. Not the Web socket upgrade request, but the requests made with the ws or wss protocol
  1. main_frame - Request made from the top level document
  1. sub_frame - Requests made from an iframe inside the top level document

Redirect Rule Pair

Refer the user docs for a better understanding about Redirect Rule. In case of this rule ruleType property of the rule is set to Redirect

The schema of the pairs is as follows:

PropertyUsageTypeDescription
sourceRequiredObjectThe source object for matching
destinationTypeRequiredStringThe type of destination. Can be one of the following values: url, map_local, mock_or_file_picker
destinationRequiredStringThe destination url value

Cancel Rule

Refer the user docs for a better understanding about Cancel Rule. In case of this rule ruleType property of the rule is set to Cancel

The schema of its pairs only requires the source object

PropertyUsageTypeDescription
sourceRequiredObjectThe source object for matching

Replace Rule

Refer the user docs for a better understanding about Replace Rule. In case of this rule ruleType property of the rule is set to Replace

The schema of the pairs is as follows:

PropertyUsageTypeDescription
sourceRequiredObjectThe source object for matching
fromRequiredStringTarget string that will be replaced
toRequiredStringThe final replaced value

Headers Rule

Refer the user docs for a better understanding about Header Rule. In case of this rule ruleType property of the rule is set to Header

The schema of the pairs is as follows:

PropertyUsageTypeDescription
sourceRequiredObjectThe source object for matching
modificationsRequiredArray of Request/Response ModificationsDefines modifications for both Request and Response

Header rule modification object

Each modification is an object that has the following structure

PropertyUsageTypeDescription
RequestOptionalArray of modificationsModifications to be applied on the request headers
ResponseOptionalArray of modificationsModifications to be applied on the response headers

The modifications Object for both request and response are as follows

PropertyUsageTypeDescription
headerRequiredStringThe name of the header to modify
typeRequiredStringThe type of modification. Can be one of the following values: Add, Remove, Modify
valueRequired (for Add and Modify types)StringThe value to set for the header

User-Agent Rule

Refer the user docs for a better understanding about User-Agent Rule. In case of this rule ruleType property of the rule is set to UserAgent

The schema of the pairs is as follows:

PropertyUsageTypeDescription
sourceRequiredObjectThe source object for matching
userAgentRequiredStringThe user agent value to set

Query Param Rule

Refer the user docs for a better understanding about Query Param Rule. In case of this rule ruleType property of the rule is set to QueryParam

The schema of the pairs is as follows:

PropertyUsageTypeDescription
sourceRequiredObjectThe source object for matching
modificationsRequiredArray of modification objectsThe array of query parameter modifications.

Modification object for query param rule

The modification objects for this rule are as follows

PropertyUsageTypeDescription
paramRequiredStringThe name of the query parameter to modify
typeRequiredStringThe type of modification. Can be one of the following values: Add, Remove, Remove All
valueRequired (for Add and Modify types)StringThe value to set for the query parameter

Modify Response Rule

In case of this rule ruleType property of the rule is set to Response

The schema of the pairs is as follows:

PropertyUsageTypeDescription
typeRequiredStringThe type of response. Can be one of the following values: code, static
valueRequiredStringThe value of the response, either a status code or a static response.
serveWithoutRequestOptionalBooleanWhen enabled, response is served directly from Requestly and does not reach the backend. false by default

Remember that in case of the code type, you have special arguments that are passed to the custom function that you define. Go through the user docs for a list of arguments that you might find useful

Modify Request Rule Schema

In case of this rule ruleType property of the rule is set to Request

The schema of the pairs is as follows:

PropertyUsageTypeDescription
typeRequiredStringThe type of request. Can be one of the following values: code, static
valueRequiredStringThe value of the request, either a request code or a static request

Remember that in case of the code type, you have special arguments that are passed to the custom function that you define. Go through the user docs for a list of arguments that you might find useful

Delay Rule

Refer the user docs for a better understanding about Delay Rule. In case of this rule ruleType property of the rule is set to Delay

PropertyUsageTypeDescription
sourceRequiredObjectThe source object for matching
delayRequirednumberDelay value in milliseconds
Updated on