v1

latestOpenAPI 3.0.3Apache 2.02026-07-13161446.9 KB
Compile API

Compile

This API endpoint allows you to partially evaluate Rego queries and obtain a simplified version of the policy. The example below assumes that OPA has been given the following policy (use PUT /v1/policies/{path}):

package example
allow {
  input.subject.clearance_level >= data.reports[_].clearance_level
}

Compile API request body so that it contain the following fields:

FieldTypeRequiredDescription
querystringYesThe query to partially evaluate and compile.
inputanyNoThe input document to use during partial evaluation (default: undefined).
unknownsarray[string]NoThe terms to treat as unknown during partial evaluation (default: ["input"]]).

For example:

{
  "query": "data.example.allow == true",
  "input": {
    "subject": {
      "clearance_level": 4
    }
  },
  "unknowns": [
    "data.reports"
    ]
}

Partial evaluation

In some cases, the result of partial valuation is a conclusive, unconditional answer. See the guidance for details.

post/v1/compile

Query parameters

prettyboolean

If true, response will be in a human-readable format.

explainstring

If set to full, response will include query explanations in addition to the result.

metricsboolean

If true, compiler performance metrics will be returned in the response.

instrumentboolean

If true, response will return additional performance metrics in addition to the result and the standard metrics.

Caution: This can add significant overhead to query evaluation. The recommendation is to only use this parameter if you are debugging a performance problem.

Request body

PartialQuerySchema required

Response

Success

Example response

{
  "result": [
    {
      "id": "example2",
      "raw": "package opa.examples\\n\\nimport data.servers\\n\\nviolations[server] {\\n\\tserver = servers[_]\\n\\tserver.protocols[_] = \\\"http\\\"\\n\\tpublic_servers[server]\\n}\\n",
      "ast": {
        "package": {
          "path": [
            {
              "type": "import",
              "value": "data.opa.example"
            }
          ]
        },
        "rules": [
          {
            "head": {
              "name": "violations",
              "key": {
                "type": "var",
                "value": "$0"
              }
            },
            "body": [
              {
                "index": 1,
                "terms": [
                  {
                    "type": "var"
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  ],
  "metrics": {
    "timer_rego_input_parse_ns": 69994,
    "timer_rego_query_parse_ns": 4096,
    "timer_rego_query_compile_ns": 4096,
    "timer_rego_query_eval_ns": 69994,
    "timer_rego_module_parse_ns": 12345,
    "timer_rego_module_compile_ns": 69994,
    "timer_server_handler_ns": 631000,
    "timer_server_read_bytes_ns": 631000,
    "timer_query_compile_stage_build_comprehension_index_ns": 43000,
    "timer_query_compile_stage_check_safety_ns": 852000,
    "timer_query_compile_stage_check_types_ns": 577000,
    "timer_query_compile_stage_check_undefined_funcs_ns": 426000,
    "timer_query_compile_stage_check_unsafe_builtins_ns": 14000,
    "timer_query_compile_stage_resolve_refs_ns": 3000,
    "timer_query_compile_stage_rewrite_comprehension_terms_ns": 329000,
    "timer_query_compile_stage_rewrite_dynamic_terms_ns": 27000,
    "timer_query_compile_stage_rewrite_expr_terms_ns": 40000,
    "timer_query_compile_stage_rewrite_local_vars_ns": 187000,
    "timer_query_compile_stage_rewrite_to_capture_value_ns": 1178000,
    "timer_query_compile_stage_rewrite_with_values_ns": 23000
  },
  "explanation": [
    {
      "op": "enter",
      "type": "expr",
      "node": {
        "terms": [
          {
            "type": "var",
            "value": "$term1"
          }
        ]
      }
    }
  ]
}