Script
This document explains the detailed usage of a specific policy. If you are using the Apinizer policy structure for the first time or want to learn about the general working principles of policies, we recommend reading the What is a Policy? page first.
Overview
What is its Purpose?
- Script Policy aims to solve integration requirements without writing code by applying custom business rules and data transformations in the API Proxy request pipeline.
- Script Policy enables centralized management of operations such as masking, enriching incoming data, or adapting error messages in the response pipeline.
- Script Policy makes it possible to create a global/local shared script library for consistent behavior across different environments.
- Script Policy activates only for specific endpoint or header combinations through its condition engine, maintaining performance.
Working Principle
- Request Arrival: For every HTTP/HTTPS request arriving at the API Gateway, the source IP address of the request is detected.
- Policy Check: If Script Policy is active, the system checks in the following order:
- Is a Condition defined? If so, is the condition met?
- Is the policy active (active=true)?
- Is a Variable being used or Apinizer default?
- Script Engine Execution: According to the selected executionType (SYNC/ASYNC) and scriptLanguage (Groovy/Javascript) values, the script runs in the specified pipeline region; request/response body, headers, and parameter maps can be updated.
- Decision Making:
- Match Found: Message components updated as a result of the script are written back to the pipeline; in case of error, the defined statusCode and message are returned.
- No Match: Script is skipped, request/response continues to default flow.
- Error Handling: Customizable HTTP status code and error message are returned for requests that do not comply with policy rules.
Features and Capabilities
Core Features
- ExecutionType Management (Sync/Async): Determines whether the script will run synchronously or in the background; asynchronous mode does not block the endpoint during long-running operations.
- Dual Script Language Support: Allows teams to use the language they are proficient in by choosing between Groovy and Javascript.
- Context Variable Library: Provides ready-made variable maps for From Client, To Backend, From Backend, and To Client flows; readable/writable fields are clearly separated.
- Active/Passive Status Control: Easily switch the policy's active or passive state (active/passive toggle). Policy is not applied when passive, but configuration is retained.
- Condition-Based Application: Create complex conditions with Query Builder to determine when the policy will be applied (e.g., only for specific endpoints or header values).
Advanced Features
- Script Test Laboratory: Run scripts with sample header/param/body data for different pipeline segments using the integrated test window and examine results.
- Dependency Tracking: See which API Proxy or groups the policy is used in through Used Proxies/Policy Groups sections and perform change impact analysis.
- Dynamic Context Value Selection: Automatically copy date, environment, or proxy metadata information from EnumScriptContextValue for use within scripts.
- Export/Import Feature: Export policy configuration as a ZIP file. Import to different environments (Development, Test, Production). Version control and backup capability.
- Policy Group and Proxy Group Support: Manage multiple policies within Policy Groups. Assign policies in bulk to Proxy Groups. Centralized update and deploy operations.
- Deploy and Versioning: Deploy policy changes to production environment. View which API Proxies use it (Policy Usage). Proxy Group and Policy Group usage reports.
Usage Scenarios
| Scenario | Situation | Solution (Policy Application) | Expected Behavior / Result |
|---|---|---|---|
| Request Header Injection | External system requests additional header | Header is added via requestHeaderMapToTargetAPI in From Client → To Backend script | Backend call is made with required header |
| Sensitive Field Masking | Response contains ID number | Sensitive field is masked with regex in To Client script | Masked data is returned to client |
| Dynamic Endpoint Routing | Some customers need to be routed to different backend URL | Script sets requestBackendUrlToTargetAPI field conditionally | Request is routed to appropriate target service |
| Conditional Error Return | Access must be blocked for specific API keys | Script sets responseErrorMessageToClient and statusCodeToClient=403 | Client receives 403 and customized message |
| JWT Enrichment | Need to generate context based on JWT claim value | Script reads claim and writes to customVariableMap | Subsequent policies use enriched value |
Configuring Policy Parameters
At this step, users can create a new policy or configure existing policy parameters to define access rules.
Creating New Script Policy
Configuration Steps
Step 1: Navigate to Creation Page
Go to Development → Global Settings → Global Policies → Script Policy from the left menu and click the [+ Create] button in the upper right.
Step 2: Enter Basic Information
Policy Status: Shows Active/Passive status. New policies are active by default.
Name - Required: Enter unique name (example: Production_ScriptPolicy). System checks automatically. Green check: available, Red cross: existing name.
Description: Explain the policy's purpose (Max. 1000 characters). Example: "Adds campaign header in request pipeline."
Step 3: Variable Usage
- In the action button area at the top of the page, you can use the [<> Variable] button to select dynamic values.
- Using context/global variable expressions, you can manage policy parameters with variable-based values instead of fixed values.
- This reduces manual update effort when values change and provides operational convenience.
- For detailed information, review Dynamic Variables.
Step 4: ExecutionType Selection
Select Sync or Async in Execution Type section:
- When
Syncis selected, script runs synchronously in gateway pipeline Asynctriggers side channel to avoid blocking client during long operations
Step 5: Script Language Configuration
Select Javascript or Groovy under Script Language. Selection determines code editor syntax and IntelliSense.
Step 6: Script Body and Variable Management
- Write or paste your script in the code editor
- Copy fields like requestHeaderMapToTargetAPI, responseBodyTextToClient from variable tags with one click
- You can transfer data to other policies via customVariableMap
- Open test dialog with Try It button and run script with sample inputs
Step 7: Define Condition (Optional)
Switch to Condition tab. Conditions determine when the policy will be active.
Examples:
- Environment-based:
Header = X-Environment, Operator = Equals, Value = production - API Key-based:
Header = X-API-Key, Starts With = PROD- - Endpoint-based:
Path = /api/admin/*
If no condition is defined, policy is always active.
For details see: Conditions