Install, eval & the REPL

Run your first query.

Advanced10 min · lesson 2 of 12

OPA ships as a single binary. The fastest way to learn is opa eval (evaluate a query against input and policy from the command line) and the interactive REPL (opa run). You can also run OPA as a long-lived server exposing a REST decision API (covered later), but for authoring and testing policy the CLI is where you live.

terminal
$ opa version
Version: 0.66.0
# evaluate a query against a policy + input file
$ opa eval -d policy.rego -i input.json 'data.example.allow'
{
"result": [ { "expressions": [ { "value": true, ... } ] } ]
}

The REPL and the Playground

opa run with no server starts a REPL where you can load policy and poke at expressions interactively — invaluable for learning how Rego evaluates, because you can type a rule body and see exactly what it produces. The online Rego Playground is the same idea in a browser and is the standard way to share and debug a policy. Start there when a rule behaves unexpectedly.

terminal
$ opa run
> x := [1, 2, 3]
> x[_] > 1 # is any element > 1?
true
> nums := [n | n := x[_]; n > 1] # comprehension: [2, 3]
[2, 3]
Query the right path, or you get undefined
opa eval evaluates a query like data.example.allow — the package name plus the rule. Point it at a path that does not exist and you get an empty/undefined result, which looks like “policy did nothing” rather than an error. Match the query to your policy’s package and rule names exactly; most “my policy is not working” moments are a mismatched query path.