Conditions are part of policy configuration as explained on the What is Policy? page. Condition definitions are made from the conditions section when creating or editing a policy.
Conditional Policy Execution Concept
Conditional policy execution enables policies to be executed based on specific conditions. In Apinizer, conditions are created without writing code, using Variable and form-based configuration. The following diagram shows how conditional policy execution occurs through the Gateway:Form-Based Condition Creation
When creating conditional policies, the following steps are followed:1
First Variable Selection
You select the variable on the left side of the comparison. This variable can be:
- Header variable
- Query parameter variable
- Message content variable (with JSONPath/XPath)
- Context variable
- System variable
- Custom variable
2
Data Type Determination
You select the data type of the variable:
- STRING: For text comparisons
- NUMERIC: For numeric comparisons
- DATE: For date comparisons (date format must be specified)
3
Comparison Operator Selection
You select the comparison operator from the dropdown menu:
- Comparison operators such as equality, greater than, less than
- Text operators such as contains, starts with, ends with
- Existence check operators such as exists, not exists, empty
4
Second Value Source Determination
You determine the source of the second value:
- Constant Value: A constant value entered directly
- Variable: Selection of another variable (variable-to-variable comparison)
5
Nested Conditions (Optional)
You combine conditions with AND, OR, NOT operators by adding sub-conditions for complex logic.
Condition Types and Variable Usage
Conditions are created using Variable. Variables can get values from message content, headers, query parameters, or context.Message Content Conditions
To create conditions based on message content, variables are defined using JSONPath or XPath:JSONPath Variable
To extract values from JSON messages:
- Variable Type: Message Content (JSONPath)
- JSONPath Expression:
$.user.roleor$.products[*].price - Data Type: STRING or NUMERIC
XPath Variable
To extract values from XML messages:
- Variable Type: Message Content (XPath)
- XPath Expression:
/user/roleor//product/@price - Data Type: STRING or NUMERIC
Header Variable
Based on HTTP header values:
- Variable Type: Header
- Header Name:
Content-TypeorX-API-Version - Data Type: STRING
Query Parameter Variable
Based on query parameters:
- Variable Type: Query Parameter
- Parameter Name:
versionorformat - Data Type: STRING
Client Conditions
To create conditions based on client information, relevant variables are used:IP Address
IP Address
For control based on IP address:
- Variable Type: Client IP
- Data Type: STRING
- Operator: IN or NOT_IN
- Value:
192.168.1.0/24#10.0.0.0/8(list values separated by #)
User Agent
User Agent
For control based on User-Agent header:
- Variable Type: Header
- Header Name:
User-Agent - Data Type: STRING
- Operator: CONTAINS or STARTS_WITH
API Key
API Key
For control based on API Key value:
- Variable Type: Header
- Header Name:
X-API-KeyorAuthorization - Data Type: STRING
- Operator: EQ or IN
Context Values
Context Values
For control based on user information in context:
- Variable Type: Context Variable
- Variable Name:
context.user.roleorcontext.user.id - Data Type: STRING
- Operator: EQ, IN, etc.
Time Conditions
System variables are used for time-based conditions:Date Range
- Variable Type: System Variable (date variable)
- Data Type: DATE
- Date Format:
yyyy-MM-dd(must be specified) - Operator: GE, LE, GT, LT
Time Range
- Variable Type: System Variable (time variable)
- Data Type: STRING or DATE (according to time format)
- Operator: GE, LE
Day-Based
- Variable Type: System Variable (day variable)
- Data Type: STRING
- Operator: EQ or IN
Time Zone
- Variable Type: System Variable (timezone variable)
- Data Type: STRING
- Operator: EQ
System and Environment Conditions
Conditions based on system and environment variables:Environment Variable
Environment Variable
For control based on environment variables:
- Variable Type: Environment Variable
- Variable Name:
ENVIRONMENTorFEATURE_FLAG - Data Type: STRING
- Operator: EQ, NE, etc.
System Property
System Property
For control based on system properties:
- Variable Type: System Property
- Property Name:
app.versionorsystem.property - Data Type: STRING
- Operator: EQ, NE, etc.
Cache Value
Cache Value
For control based on values in cache:
- Variable Type: Cache Variable
- Cache Key:
user.statusor cache key - Data Type: STRING
- Operator: EQ, IS_EXISTS, etc.
Condition Configuration
Conditions are created using Variable and form-based configuration. Each condition consists of the following components:Condition Components
-
First Variable: Variable on the left side of the comparison
- Header variable
- Query parameter variable
- Message content variable (JSONPath/XPath)
- Context variable
- Custom variable
-
Data Type: Data type of the variable
- STRING: For text comparisons
- NUMERIC: For numeric comparisons
- DATE: For date comparisons (date format must be specified)
- Comparison Operator: Type of comparison between two values
-
Second Value Source:
- Constant Value: A constant value entered directly
- Variable: Selection of another variable
-
Logical Operator: To combine multiple conditions
- AND: All conditions must be met
- OR: At least one condition must be met
- NOT: Condition must not be met
Comparison Operators
Comparison operators that can be used in conditions:Text (STRING) Operators
| Operator | Description | Usage Scenario |
|---|---|---|
| Equals (EQ) | Are two values exactly equal? | Content-Type == "application/json" |
| Not Equals (NE) | Are two values not equal? | version != "v1" |
| Less Than (LT) | Smaller in alphabetical order? | "apple" < "banana" |
| Less Than or Equal (LE) | Less than or equal? | "apple" <= "banana" |
| Greater Than (GT) | Larger in alphabetical order? | "zebra" > "apple" |
| Greater Than or Equal (GE) | Greater than or equal? | "zebra" >= "apple" |
| Contains (CONTAINS) | Does it appear in the text? | userAgent contains "Mobile" |
| Not Contains (NOT_CONTAINS) | Does it not appear in the text? | path NOT_CONTAINS "/admin" |
| Starts With (STARTS_WITH) | Does it start with the specified text? | path STARTS_WITH "/api/v2" |
| Ends With (ENDS_WITH) | Does it end with the specified text? | filename ENDS_WITH ".json" |
| In (IN) | Is it in the specified list? | role IN ["admin", "user"] |
| Not In (NOT_IN) | Is it not in the specified list? | status NOT_IN ["deleted", "archived"] |
Numeric (NUMERIC) Operators
| Operator | Description | Usage Scenario |
|---|---|---|
| Equals (EQ) | Are two numbers equal? | price == 100 |
| Not Equals (NE) | Are two numbers not equal? | count != 0 |
| Less Than (LT) | Is the first number less than the second? | age < 18 |
| Less Than or Equal (LE) | Less than or equal? | quantity <= 10 |
| Greater Than (GT) | Is the first number greater than the second? | score > 80 |
| Greater Than or Equal (GE) | Greater than or equal? | balance >= 1000 |
| In (IN) | Is it in the specified number list? | statusCode IN [200, 201, 204] |
| Not In (NOT_IN) | Is it not in the specified number list? | errorCode NOT_IN [404, 500] |
Date (DATE) Operators
Date format must be specified for date comparisons (e.g.,yyyy-MM-dd, dd/MM/yyyy).
| Operator | Description | Usage Scenario |
|---|---|---|
| Equals (EQ) | Are two dates the same? | expiryDate == "2024-12-31" |
| Not Equals (NE) | Are two dates different? | startDate != "2024-01-01" |
| Less Than (LT) | Is the first date before the second? | createdDate < "2024-06-01" |
| Less Than or Equal (LE) | Before or the same? | deadline <= "2024-12-31" |
| Greater Than (GT) | Is the first date after the second? | expiryDate > "2024-01-01" |
| Greater Than or Equal (GE) | After or the same? | startDate >= "2024-01-01" |
Existence Check Operators
These operators do not require a second value, they only check the status of the variable:| Operator | Description | Usage Scenario |
|---|---|---|
| Exists (IS_EXISTS) | Is the variable defined and not null? | apiKey IS_EXISTS |
| Not Exists (IS_NOT_EXISTS) | Is the variable not defined or null? | optionalHeader IS_NOT_EXISTS |
| Empty (IS_EMPTY) | Is the variable empty string or null? | queryParam IS_EMPTY |
| Not Empty (IS_NOT_EMPTY) | Is the variable filled? | requestBody IS_NOT_EMPTY |
List Values: For
IN and NOT_IN operators, list values are separated by # character. Example: admin#user#guest or 100#200#300Null Value Handling
Null values are handled specially in conditions:| Status | Operator | Result |
|---|---|---|
| First value null, second value null | EQ | true (null == null) |
| First value null, second value null | NE | false (null != null is not) |
| First value null, second value filled | EQ | false |
| First value null, second value filled | NE | true |
| First value filled, second value null | EQ | false |
| First value filled, second value null | NE | true |
| First value null | GT, LT, GE, LE, CONTAINS, IN, STARTS_WITH, ENDS_WITH | false |
| First value null | NOT_CONTAINS, NOT_IN | true |
Condition Creation Examples
The following examples show how conditions are created with the form-based approach:Example 1: Content-Type Based Validation
Scenario: Different validation policies will be applied based on Content-Type header. Form Configuration:- First Variable: Header variable selected →
Content-Type - Data Type: STRING
- Operator: Equals (EQ)
- Second Value Source: Constant value
- Second Value:
application/json
application/json, JSON Schema Validation Policy runs.
Alternative Conditions (nested conditions):
- Condition 1: If Content-Type is
application/xml→ XML Schema Validation Policy- First Variable: Header →
Content-Type - Operator: EQ
- Second Value:
application/xml
- First Variable: Header →
- Condition 2: Other cases → Reject Request (400 Bad Request)
- ELSE case
Example 2: API Version Based Routing
Scenario: Routing to different backends based on version value in query parameter. Form Configuration:- First Variable: Query Parameter variable selected →
version - Data Type: STRING
- Operator: Equals (EQ)
- Second Value Source: Constant value
- Second Value:
v1
version=v1, routed to Backend v1.
Alternative Conditions:
- Condition 1: If version is
v2→ Route to Backend v2- First Variable: Query Parameter →
version - Operator: EQ
- Second Value:
v2
- First Variable: Query Parameter →
- Condition 2: Other cases → Route to Default Backend
- ELSE case
Example 3: User Role Based Authorization
Scenario: Different policy will be applied based on user role. Form Configuration:- First Variable: Context Variable selected →
context.user.role - Data Type: STRING
- Operator: Equals (EQ)
- Second Value Source: Constant value
- Second Value:
admin
admin, all operations are allowed.
Alternative Conditions:
- Condition 1: If role is
user→ Rate Limiting Policy (100 req/min)- First Variable: Context Variable →
context.user.role - Operator: EQ
- Second Value:
user
- First Variable: Context Variable →
- Condition 2: Other cases → Reject Request (403 Forbidden)
- ELSE case
Example 4: IP Based Rate Limiting
Scenario: Different rate limit values will be applied based on IP address. Form Configuration:- First Variable: Client IP variable selected
- Data Type: STRING
- Operator: In (IN)
- Second Value Source: Constant value
- Second Value:
192.168.1.0/24#10.0.0.0/8(list values separated by #)
- Condition 2: Other cases → Rate Limiting Policy (100 req/min)
- ELSE case
Example 5: Time-based Access Control (Nested Conditions)
Scenario: Access permission will be given during business hours. Form Configuration (Nested conditions with AND logic):- Logical Operator: AND selected
- First Sub-Condition:
- First Variable: System Variable →
current.time - Data Type: STRING or DATE (according to time format)
- Operator: Greater Than or Equal (GE)
- Second Value:
09:00
- First Variable: System Variable →
- Second Sub-Condition:
- First Variable: System Variable →
current.time - Data Type: STRING or DATE
- Operator: Less Than or Equal (LE)
- Second Value:
17:00
- First Variable: System Variable →
Nested Conditions (Condition Combinations)
You can create nested conditions for complex logic. In the form-based approach, you combine conditions by adding sub-conditions:AND Operator
Form Configuration:- Logical Operator: AND selected
- First Sub-Condition:
- First Variable: Header →
Content-Type - Data Type: STRING
- Operator: Equals (EQ)
- Second Value:
application/json
- First Variable: Header →
- Second Sub-Condition:
- First Variable: Header →
X-API-Key - Data Type: STRING
- Operator: Equals (EQ)
- Second Value:
premium-key
- First Variable: Header →
OR Operator
Form Configuration:- Logical Operator: OR selected
- First Sub-Condition:
- First Variable: Client IP
- Data Type: STRING
- Operator: In (IN)
- Second Value:
192.168.1.0/24#10.0.0.0/8
- Second Sub-Condition:
- First Variable: Header →
X-API-Key - Data Type: STRING
- Operator: Equals (EQ)
- Second Value:
internal-key
- First Variable: Header →
NOT Operator
Form Configuration:- Logical Operator: NOT selected
- Sub-Condition:
- First Variable: Client IP
- Data Type: STRING
- Operator: In (IN)
- Second Value:
10.0.0.0/8
Variable Comparison
In conditions, you can perform variable-to-variable comparison as well as comparisons with constant values: Form Configuration:- First Variable: Header →
Content-Type - Data Type: STRING
- Operator: Equals (EQ)
- Second Value Source: Variable selected
- Second Variable: Context Variable →
context.preferred.contentType
Conditional Policy Best Practices
Variable Selection
- Use existing Variables, don’t create unnecessary new variables
- Select variable types correctly (Header, Query Parameter, Message Content, Context Variable)
- Write and test JSONPath/XPath expressions correctly
Data Type Selection
- Use STRING for text comparisons
- Use NUMERIC for numeric comparisons
- Use DATE for date comparisons and specify date format
Condition Ordering
- Add most specific conditions first
- Leave general conditions last
- Always define ELSE case
Nested Conditions
- Use nested conditions for complex logic
- Select AND/OR/NOT operators correctly
- Keep condition depth reasonable (for readability)
Value Source
- Use constant value option for constant values
- Use variable option for dynamic comparisons
- Ensure data types are compatible when doing variable-to-variable comparison
List Values
- Separate list values with
#character for IN and NOT_IN operators - Example:
admin#user#guestor100#200#300
Existence Check
- Use IS_EXISTS or IS_NOT_EXISTS to check variable existence
- Use IS_EMPTY or IS_NOT_EMPTY for empty value check
- These operators do not require a second value
Performance
- Check simple conditions first
- Leave heavy operations (JSONPath/XPath) last
- Consider cache usage
Test and Validation
- Test all condition branches
- Test edge cases (empty values, null, etc.)
- Ensure variable values are available at runtime
- Understand how null values are handled

