Skip to main content
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

  1. Request Arrival: For every HTTP/HTTPS request arriving at the API Gateway, the source IP address of the request is detected.
  2. 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?
  3. 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.
  4. 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.
  5. 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

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

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 Sync is selected, script runs synchronously in gateway pipeline
  • Async triggers 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

Step 8: Error Message Customization (Optional)

Go to Error Message Customization tab and customize the message to be returned when access is denied. Default:
Custom:

Step 9: Save

Click [Save] button in upper right. Checklist:
  • Unique name
  • Required fields filled
  • At least one script body line present
Result:
  • Policy is added to list
  • Can be connected to APIs
  • Automatically applied if global policy
For explanation of Conditions and Error Message Customization panels, see Conditions and Error Message Customization sections on the What is a Policy? page. For a complete guide on all layers, priority order and scenario examples of the error message configuration system, see the Error Message Configuration Guide page.

Flow Variables

Flow variables and properties that you can use within Script Policy are explained in detail in the tables below.

Request Variables (Client → Apinizer)

Request Variables (Apinizer → Backend API)

Response Variables (Backend API → Apinizer)

Response Variables (Apinizer → Client)

Detailed Usage Examples

Form URL-Encoded Usage

Form Data Usage

Backend URL Modification

Example Scenario: Current routing address: “https://apinizer.com/api Incoming request context path: “/findByStatus?param=value” In this case, the request goes to: “https://apinizer.com/api/findByStatus?param=value When the following code is written:
The request goes to: “https://apinizer.com/api/new/path/value?p=v When the following code is written:
The request goes to: “https://apinizer.com/api

Important Notes

If Script type is Groovy:
  • JsonSlurper for JSON message body,
  • XMLSlurper for XML message body
Using these makes message processing much easier.
When the request is blocked with error message variables, the error message returned to the client is whatever is written to this variable’s value instead of the Error Message Template.

Message Variables

Message variables and properties that you can use within Script Policy are explained in detail in the tables below.

Request Pipeline Variables

Response Pipeline Variables

Behavior of Encoding Variables and Routing Settings

In the script policy, the data format variables (gzip, deflate, br, zstd, compress, identity) on the request or response pipeline can be written. These variables work in combination with the encoding override settings in the routing configuration, and the precedence between them is important.

Execution Order

A request’s data flow goes through the following steps:
  1. Client-side headers are read — The data format header sent by the client is evaluated.
  2. Routing override settings are applied — Encoding override values defined on the API Proxy or Routing override the client-supplied header.
  3. Body is decompressed — The compressed request body is decompressed according to the resolved settings.
  4. Backend-side compression override is applied — The format to be used when forwarding to the backend is determined.
  5. Request pipeline policies run — The script policy kicks in at this stage. If an encoding variable is written inside the script, it overrides all previous settings.
  6. Body is forwarded to the backend — Compression is applied based on the final value left by the script.
The response pipeline works the same way; routing settings are applied first to the backend response, and the response pipeline script policy has the final say.
When an encoding variable is written inside the script, the override values defined in the routing settings are silently overridden. If the script policy and routing settings are used together on the same API Proxy, the order of precedence must be planned deliberately.

One Format at a Time

Only one data format should be active at a time for a given request or response. If a format is set to true inside the script, all other format variables must explicitly be set to false. Otherwise, multiple formats may remain active simultaneously and cause unexpected results on the backend or client side. Recommended usage:

Header Consistency

When an encoding variable is changed, the Content-Encoding value in the request or response headers must also be set to the same value. A mismatch between the format variable and the header value will cause the client or backend to fail when decompressing the body.

When to Use Script, When to Use Routing Settings

  • Routing setting (override): Use when a fixed rule is to be applied for all requests (e.g., every request must go to the backend as gzip). This is the safer, less error-prone option.
  • Script: Use when the decision must be made dynamically at runtime (e.g., use a different format based on the client’s IP address or a header value). Inside the script, all format variables and the corresponding headers must be managed together.
If both methods are used at the same time, the script has the final say.

Message Variables

Environment Variables

Credential Variables

Custom Variables

There may be a need to temporarily define variables with policies on the Request or Response pipeline and use them in the next policy. In this case, the customVariableMap variable is used.

Important Restrictions

Pipeline Restrictions:
  • Script Policy added to Request pipeline cannot access variables in Response pipeline.
  • Script Policy added to Response pipeline can only read variables in Request pipeline.

Deleting the Policy

For steps to delete this policy and operations to be performed when it is in use, see the Removing Policy from Flow section on the Policy Management page.

Exporting/Importing the Policy

For export and import steps of this policy, see the Export/Import page.

Connecting Policy to API

For the process of how to connect this policy to APIs, see the Connecting Policy to API section on the Policy Management page.

Advanced Features

Best Practices

Things to Do and Best Practices

Security Best Practices

Things to Avoid

Performance Tips

Frequently Asked Questions (FAQ)