REST API

This document is the authoritative specification of the OPA REST API.

Policy API

The Policy API exposes CRUD endpoints for managing policy modules. Policy modules can be added, removed, and modified at any time.

The identifiers given to policy modules are only used for management purposes. They are not used outside of the Policy API.

List Policies

  1. GET /v1/policies HTTP/1.1

List policy modules.

Status Codes

  • 200 - no error
  • 500 - server error

Example Request

  1. GET /v1/policies HTTP/1.1

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": [
  3. {
  4. "id": "example2",
  5. "raw": "package opa.examples\n\nimport data.servers\n\nviolations[server] {\n\tserver = servers[_]\n\tserver.protocols[_] = \"http\"\n\tpublic_servers[server]\n}\n",
  6. "ast": {
  7. "package": {
  8. "path": [
  9. {
  10. "type": "var",
  11. "value": "data"
  12. },
  13. {
  14. "type": "string",
  15. "value": "opa"
  16. },
  17. {
  18. "type": "string",
  19. "value": "examples"
  20. }
  21. ]
  22. },
  23. "rules": [
  24. {
  25. "head": {
  26. "name": "violations",
  27. "key": {
  28. "type": "var",
  29. "value": "server"
  30. }
  31. },
  32. "body": [
  33. {
  34. "index": 0,
  35. "terms": [
  36. {
  37. "type": "string",
  38. "value": "eq"
  39. },
  40. {
  41. "type": "var",
  42. "value": "server"
  43. },
  44. {
  45. "type": "ref",
  46. "value": [
  47. {
  48. "type": "var",
  49. "value": "data"
  50. },
  51. {
  52. "type": "string",
  53. "value": "servers"
  54. },
  55. {
  56. "type": "var",
  57. "value": "$0"
  58. }
  59. ]
  60. }
  61. ]
  62. },
  63. {
  64. "index": 1,
  65. "terms": [
  66. {
  67. "type": "string",
  68. "value": "eq"
  69. },
  70. {
  71. "type": "ref",
  72. "value": [
  73. {
  74. "type": "var",
  75. "value": "server"
  76. },
  77. {
  78. "type": "string",
  79. "value": "protocols"
  80. },
  81. {
  82. "type": "var",
  83. "value": "$1"
  84. }
  85. ]
  86. },
  87. {
  88. "type": "string",
  89. "value": "http"
  90. }
  91. ]
  92. },
  93. {
  94. "index": 2,
  95. "terms": {
  96. "type": "ref",
  97. "value": [
  98. {
  99. "type": "var",
  100. "value": "data"
  101. },
  102. {
  103. "type": "string",
  104. "value": "opa"
  105. },
  106. {
  107. "type": "string",
  108. "value": "examples"
  109. },
  110. {
  111. "type": "string",
  112. "value": "public_servers"
  113. },
  114. {
  115. "type": "var",
  116. "value": "server"
  117. }
  118. ]
  119. }
  120. }
  121. ]
  122. }
  123. ]
  124. }
  125. },
  126. {
  127. "id": "example1",
  128. "raw": "package opa.examples\n\nimport data.servers\nimport data.networks\nimport data.ports\n\npublic_servers[server] {\n\tserver = servers[_]\n\tserver.ports[_] = ports[k].id\n\tports[k].networks[_] = networks[m].id\n\tnetworks[m].public = true\n}\n",
  129. "ast": {
  130. "package": {
  131. "path": [
  132. {
  133. "type": "var",
  134. "value": "data"
  135. },
  136. {
  137. "type": "string",
  138. "value": "opa"
  139. },
  140. {
  141. "type": "string",
  142. "value": "examples"
  143. }
  144. ]
  145. },
  146. "rules": [
  147. {
  148. "head": {
  149. "name": "public_servers",
  150. "key": {
  151. "type": "var",
  152. "value": "server"
  153. }
  154. },
  155. "body": [
  156. {
  157. "index": 0,
  158. "terms": [
  159. {
  160. "type": "string",
  161. "value": "eq"
  162. },
  163. {
  164. "type": "var",
  165. "value": "server"
  166. },
  167. {
  168. "type": "ref",
  169. "value": [
  170. {
  171. "type": "var",
  172. "value": "data"
  173. },
  174. {
  175. "type": "string",
  176. "value": "servers"
  177. },
  178. {
  179. "type": "var",
  180. "value": "$0"
  181. }
  182. ]
  183. }
  184. ]
  185. },
  186. {
  187. "index": 1,
  188. "terms": [
  189. {
  190. "type": "string",
  191. "value": "eq"
  192. },
  193. {
  194. "type": "ref",
  195. "value": [
  196. {
  197. "type": "var",
  198. "value": "server"
  199. },
  200. {
  201. "type": "string",
  202. "value": "ports"
  203. },
  204. {
  205. "type": "var",
  206. "value": "$1"
  207. }
  208. ]
  209. },
  210. {
  211. "type": "ref",
  212. "value": [
  213. {
  214. "type": "var",
  215. "value": "data"
  216. },
  217. {
  218. "type": "string",
  219. "value": "ports"
  220. },
  221. {
  222. "type": "var",
  223. "value": "k"
  224. },
  225. {
  226. "type": "string",
  227. "value": "id"
  228. }
  229. ]
  230. }
  231. ]
  232. },
  233. {
  234. "index": 2,
  235. "terms": [
  236. {
  237. "type": "string",
  238. "value": "eq"
  239. },
  240. {
  241. "type": "ref",
  242. "value": [
  243. {
  244. "type": "var",
  245. "value": "data"
  246. },
  247. {
  248. "type": "string",
  249. "value": "ports"
  250. },
  251. {
  252. "type": "var",
  253. "value": "k"
  254. },
  255. {
  256. "type": "string",
  257. "value": "networks"
  258. },
  259. {
  260. "type": "var",
  261. "value": "$2"
  262. }
  263. ]
  264. },
  265. {
  266. "type": "ref",
  267. "value": [
  268. {
  269. "type": "var",
  270. "value": "data"
  271. },
  272. {
  273. "type": "string",
  274. "value": "networks"
  275. },
  276. {
  277. "type": "var",
  278. "value": "m"
  279. },
  280. {
  281. "type": "string",
  282. "value": "id"
  283. }
  284. ]
  285. }
  286. ]
  287. },
  288. {
  289. "index": 3,
  290. "terms": [
  291. {
  292. "type": "string",
  293. "value": "eq"
  294. },
  295. {
  296. "type": "ref",
  297. "value": [
  298. {
  299. "type": "var",
  300. "value": "data"
  301. },
  302. {
  303. "type": "string",
  304. "value": "networks"
  305. },
  306. {
  307. "type": "var",
  308. "value": "m"
  309. },
  310. {
  311. "type": "string",
  312. "value": "public"
  313. }
  314. ]
  315. },
  316. {
  317. "type": "boolean",
  318. "value": true
  319. }
  320. ]
  321. }
  322. ]
  323. }
  324. ]
  325. }
  326. }
  327. ]
  328. }

Get a Policy

  1. GET /v1/policies/<id>

Get a policy module.

Query Parameters

  • pretty - If parameter is true, response will formatted for humans.

Status Codes

  • 200 - no error
  • 404 - not found
  • 500 - server error

Example Request

  1. GET /v1/policies/example1 HTTP/1.1

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": {
  3. "id": "example1",
  4. "raw": "package opa.examples\n\nimport data.servers\nimport data.networks\nimport data.ports\n\npublic_servers[server] {\n\tserver = servers[_]\n\tserver.ports[_] = ports[k].id\n\tports[k].networks[_] = networks[m].id\n\tnetworks[m].public = true\n}\n",
  5. "ast": {
  6. "package": {
  7. "path": [
  8. {
  9. "type": "var",
  10. "value": "data"
  11. },
  12. {
  13. "type": "string",
  14. "value": "opa"
  15. },
  16. {
  17. "type": "string",
  18. "value": "examples"
  19. }
  20. ]
  21. },
  22. "rules": [
  23. {
  24. "head": {
  25. "name": "public_servers",
  26. "key": {
  27. "type": "var",
  28. "value": "server"
  29. }
  30. },
  31. "body": [
  32. {
  33. "index": 0,
  34. "terms": [
  35. {
  36. "type": "string",
  37. "value": "eq"
  38. },
  39. {
  40. "type": "var",
  41. "value": "server"
  42. },
  43. {
  44. "type": "ref",
  45. "value": [
  46. {
  47. "type": "var",
  48. "value": "data"
  49. },
  50. {
  51. "type": "string",
  52. "value": "servers"
  53. },
  54. {
  55. "type": "var",
  56. "value": "$0"
  57. }
  58. ]
  59. }
  60. ]
  61. },
  62. {
  63. "index": 1,
  64. "terms": [
  65. {
  66. "type": "string",
  67. "value": "eq"
  68. },
  69. {
  70. "type": "ref",
  71. "value": [
  72. {
  73. "type": "var",
  74. "value": "server"
  75. },
  76. {
  77. "type": "string",
  78. "value": "ports"
  79. },
  80. {
  81. "type": "var",
  82. "value": "$1"
  83. }
  84. ]
  85. },
  86. {
  87. "type": "ref",
  88. "value": [
  89. {
  90. "type": "var",
  91. "value": "data"
  92. },
  93. {
  94. "type": "string",
  95. "value": "ports"
  96. },
  97. {
  98. "type": "var",
  99. "value": "k"
  100. },
  101. {
  102. "type": "string",
  103. "value": "id"
  104. }
  105. ]
  106. }
  107. ]
  108. },
  109. {
  110. "index": 2,
  111. "terms": [
  112. {
  113. "type": "string",
  114. "value": "eq"
  115. },
  116. {
  117. "type": "ref",
  118. "value": [
  119. {
  120. "type": "var",
  121. "value": "data"
  122. },
  123. {
  124. "type": "string",
  125. "value": "ports"
  126. },
  127. {
  128. "type": "var",
  129. "value": "k"
  130. },
  131. {
  132. "type": "string",
  133. "value": "networks"
  134. },
  135. {
  136. "type": "var",
  137. "value": "$2"
  138. }
  139. ]
  140. },
  141. {
  142. "type": "ref",
  143. "value": [
  144. {
  145. "type": "var",
  146. "value": "data"
  147. },
  148. {
  149. "type": "string",
  150. "value": "networks"
  151. },
  152. {
  153. "type": "var",
  154. "value": "m"
  155. },
  156. {
  157. "type": "string",
  158. "value": "id"
  159. }
  160. ]
  161. }
  162. ]
  163. },
  164. {
  165. "index": 3,
  166. "terms": [
  167. {
  168. "type": "string",
  169. "value": "eq"
  170. },
  171. {
  172. "type": "ref",
  173. "value": [
  174. {
  175. "type": "var",
  176. "value": "data"
  177. },
  178. {
  179. "type": "string",
  180. "value": "networks"
  181. },
  182. {
  183. "type": "var",
  184. "value": "m"
  185. },
  186. {
  187. "type": "string",
  188. "value": "public"
  189. }
  190. ]
  191. },
  192. {
  193. "type": "boolean",
  194. "value": true
  195. }
  196. ]
  197. }
  198. ]
  199. }
  200. ]
  201. }
  202. }
  203. }

Create or Update a Policy

  1. PUT /v1/policies/<id>
  2. Content-Type: text/plain

Create or update a policy module.

If the policy module does not exist, it is created. If the policy module already exists, it is replaced.

Query Parameters

  • pretty - If parameter is true, response will formatted for humans.
  • metrics - Return compiler performance metrics in addition to result. See Performance Metrics for more detail.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 500 - server error

Before accepting the request, the server will parse, compile, and install the policy module. If the policy module is invalid, one of these steps will fail and the server will respond with 400. The error message in the response will be set to indicate the source of the error.

Example Request

  1. PUT /v1/policies/example1 HTTP/1.1
  2. Content-Type: text/plain
  1. package opa.examples
  2. import data.servers
  3. import data.networks
  4. import data.ports
  5. public_servers[server] {
  6. some k, m
  7. server := servers[_]
  8. server.ports[_] == ports[k].id
  9. ports[k].networks[_] == networks[m].id
  10. networks[m].public == true
  11. }

cURL’s -d/--data flag removes newline characters from input files. Use the --data-binary flag instead.

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {}

Delete a Policy

  1. DELETE /v1/policies/<id>

Delete a policy module.

Query Parameters

  • pretty - If parameter is true, response will formatted for humans.
  • metrics - Return compiler performance metrics in addition to result. See Performance Metrics for more detail.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 404 - not found
  • 500 - server error

If other policy modules in the same package depend on rules in the policy module to be deleted, the server will return 400.

Example Request

  1. DELETE /v1/policies/example2 HTTP/1.1

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {}

Data API

The Data API exposes endpoints for reading and writing documents in OPA. For an introduction to the different types of documents in OPA see How Does OPA Work?.

Get a Document

  1. GET /v1/data/{path:.+}

Get a document.

The path separator is used to access values inside object and array documents. If the path indexes into an array, the server will attempt to convert the array index to an integer. If the path element cannot be converted to an integer, the server will respond with 404.

Query Parameters

  • input - Provide an input document. Format is a JSON value that will be used as the value for the input document.
  • pretty - If parameter is true, response will formatted for humans.
  • provenance - If parameter is true, response will include build/version info in addition to the result. See Provenance for more detail.
  • explain - Return query explanation in addition to result. Values: full.
  • metrics - Return query performance metrics in addition to result. See Performance Metrics for more detail.
  • instrument - Instrument query evaluation and return a superset of performance metrics in addition to result. See Performance Metrics for more detail.
  • watch - Set a watch on the data reference if the parameter is present. See Watches for more detail.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 404 - not found
  • 500 - server error

The server returns 400 if either:

  • The query requires the input document and the caller does not provide it.
  • The caller provides the input document but the query already defines it programmatically.

The server returns 200 if the path refers to an undefined document. In this case, the response will not contain a result property.

Response Message

  • result - The base or virtual document referred to by the URL path. If the path is undefined, this key will be omitted.
  • metrics - If query metrics are enabled, this field contains query performance metrics collected during the parse, compile, and evaluation steps.
  • decision_id - If decision logging is enabled, this field contains a string that uniquely identifies the decision. The identifier will be included in the decision log event for this decision. Callers can use the identifier for correlation purposes.

Example Request

  1. GET /v1/data/opa/examples/public_servers HTTP/1.1

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": [
  3. {
  4. "id": "s1",
  5. "name": "app",
  6. "ports": [
  7. "p1",
  8. "p2",
  9. "p3"
  10. ],
  11. "protocols": [
  12. "https",
  13. "ssh"
  14. ]
  15. },
  16. {
  17. "id": "s4",
  18. "name": "dev",
  19. "ports": [
  20. "p1",
  21. "p2"
  22. ],
  23. "protocols": [
  24. "http"
  25. ]
  26. }
  27. ]
  28. }

Example Watch Request

If we make the following GET request:

  1. GET /v1/data/servers?watch&pretty=true HTTP/1.1

Followed by these PATCH requests:

  1. PATCH /v1/data/servers HTTP/1.1
  2. Content-Type: application/json-patch+json
  1. [
  2. {"op": "add",
  3. "path": "-",
  4. "value": {
  5. "id": "s5",
  6. "name": "job",
  7. "protocols": ["amqp"],
  8. "ports": ["p3"]
  9. }}
  10. ]
  1. PATCH /v1/data/servers HTTP/1.1
  2. Content-Type: application/json-patch+json
  1. [
  2. {
  3. "op": "remove",
  4. "path": "1"
  5. }
  6. ]

Example Watch Response

The response below represents the response after the chunked encoding has been processed. It is not complete, as further changes to /data/servers would cause more notifications to be streamed.

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  3. Transfer-Encoding: chunked
  1. {
  2. "result": [
  3. {
  4. "id": "s1",
  5. "name": "app",
  6. "ports": [
  7. "p1",
  8. "p2",
  9. "p3"
  10. ],
  11. "protocols": [
  12. "https",
  13. "ssh"
  14. ]
  15. },
  16. {
  17. "id": "s2",
  18. "name": "db",
  19. "ports": [
  20. "p3"
  21. ],
  22. "protocols": [
  23. "mysql"
  24. ]
  25. },
  26. {
  27. "id": "s3",
  28. "name": "cache",
  29. "ports": [
  30. "p3"
  31. ],
  32. "protocols": [
  33. "memcache",
  34. "http"
  35. ]
  36. },
  37. {
  38. "id": "s4",
  39. "name": "dev",
  40. "ports": [
  41. "p1",
  42. "p2"
  43. ],
  44. "protocols": [
  45. "http"
  46. ]
  47. }
  48. ]
  49. }
  50. {
  51. "result": [
  52. {
  53. "id": "s1",
  54. "name": "app",
  55. "ports": [
  56. "p1",
  57. "p2",
  58. "p3"
  59. ],
  60. "protocols": [
  61. "https",
  62. "ssh"
  63. ]
  64. },
  65. {
  66. "id": "s2",
  67. "name": "db",
  68. "ports": [
  69. "p3"
  70. ],
  71. "protocols": [
  72. "mysql"
  73. ]
  74. },
  75. {
  76. "id": "s3",
  77. "name": "cache",
  78. "ports": [
  79. "p3"
  80. ],
  81. "protocols": [
  82. "memcache",
  83. "http"
  84. ]
  85. },
  86. {
  87. "id": "s4",
  88. "name": "dev",
  89. "ports": [
  90. "p1",
  91. "p2"
  92. ],
  93. "protocols": [
  94. "http"
  95. ]
  96. },
  97. {
  98. "id": "s5",
  99. "name": "job",
  100. "ports": [
  101. "p3"
  102. ],
  103. "protocols": [
  104. "amqp"
  105. ]
  106. }
  107. ]
  108. }
  109. {
  110. "result": [
  111. {
  112. "id": "s1",
  113. "name": "app",
  114. "ports": [
  115. "p1",
  116. "p2",
  117. "p3"
  118. ],
  119. "protocols": [
  120. "https",
  121. "ssh"
  122. ]
  123. },
  124. {
  125. "id": "s3",
  126. "name": "cache",
  127. "ports": [
  128. "p3"
  129. ],
  130. "protocols": [
  131. "memcache",
  132. "http"
  133. ]
  134. },
  135. {
  136. "id": "s4",
  137. "name": "dev",
  138. "ports": [
  139. "p1",
  140. "p2"
  141. ],
  142. "protocols": [
  143. "http"
  144. ]
  145. },
  146. {
  147. "id": "s5",
  148. "name": "job",
  149. "ports": [
  150. "p3"
  151. ],
  152. "protocols": [
  153. "amqp"
  154. ]
  155. }
  156. ]
  157. }

Get a Document (with Input)

  1. POST /v1/data/{path:.+}
  2. Content-Type: application/json
  1. {
  2. "input": ...
  3. }

Get a document that requires input.

The path separator is used to access values inside object and array documents. If the path indexes into an array, the server will attempt to convert the array index to an integer. If the path element cannot be converted to an integer, the server will respond with 404.

The request body contains an object that specifies a value for The input Document.

Request Headers

  • Content-Type: application/x-yaml: Indicates the request body is a YAML encoded object.

Query Parameters

  • partial - Use the partial evaluation (optimization) when evaluating the query.
  • pretty - If parameter is true, response will formatted for humans.
  • provenance - If parameter is true, response will include build/version info in addition to the result. See Provenance for more detail.
  • explain - Return query explanation in addition to result. Values: full.
  • metrics - Return query performance metrics in addition to result. See Performance Metrics for more detail.
  • instrument - Instrument query evaluation and return a superset of performance metrics in addition to result. See Performance Metrics for more detail.
  • watch - Set a watch on the data reference if the parameter is present. See Watches for more detail.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 500 - server error

The server returns 400 if either:

  1. The query requires an input document and the client did not supply one.
  2. The query already defines an input document and the client did supply one.

The server returns 200 if the path refers to an undefined document. In this case, the response will not contain a result property.

Response Message

  • result - The base or virtual document referred to by the URL path. If the path is undefined, this key will be omitted.
  • metrics - If query metrics are enabled, this field contains query performance metrics collected during the parse, compile, and evaluation steps.
  • decision_id - If decision logging is enabled, this field contains a string that uniquely identifies the decision. The identifier will be included in the decision log event for this decision. Callers can use the identifier for correlation purposes.

The examples below assume the following policy:

  1. package opa.examples
  2. import input.example.flag
  3. allow_request { flag == true }

Example Request

  1. POST /v1/data/opa/examples/allow_request HTTP/1.1
  2. Content-Type: application/json
  1. {
  2. "input": {
  3. "example": {
  4. "flag": true
  5. }
  6. }
  7. }

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": true
  3. }

Example Request

  1. POST /v1/data/opa/examples/allow_request HTTP/1.1
  2. Content-Type: application/json
  1. {
  2. "input": {
  3. "example": {
  4. "flag": false
  5. }
  6. }
  7. }

Example Response

  1. HTTP/1.1 200 OK
  1. {}

Get a Document (Webhook)

  1. POST /v0/data/{path:.+}
  2. Content-Type: application/json

Get a document from a webhook.

Use this API if you are enforcing policy decisions via webhooks that have pre-defined request/response formats. Note, the API path prefix is /v0 instead of /v1.

The request message body defines the content of the The input Document. The request message body may be empty. The path separator is used to access values inside object and array documents.

Request Headers

  • Content-Type: application/x-yaml: Indicates the request body is a YAML encoded object.

Query Parameters

  • pretty - If parameter is true, response will formatted for humans.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 404 - not found
  • 500 - server error

If the requested document is missing or undefined, the server will return 404 and the message body will contain an error object.

The examples below assume the following policy:

  1. package opa.examples
  2. import input.example.flag
  3. allow_request { flag == true }

Example Request

  1. POST /v0/data/opa/examples/allow_request HTTP/1.1
  2. Content-Type: application/json
  1. {
  2. "example": {
  3. "flag": true
  4. }
  5. }

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. true

Create or Overwrite a Document

  1. PUT /v1/data/{path:.+}
  2. Content-Type: application/json

Create or overwrite a document.

If the path does not refer to an existing document, the server will attempt to create all of the necessary containing documents. This behavior is similar in principle to the Unix command mkdir -p.

The server will respect the If-None-Match header if it is set to *. In this case, the server will not overwrite an existing document located at the path.

Status Codes

  • 204 - no content (success)
  • 304 - not modified
  • 400 - bad request
  • 404 - write conflict
  • 500 - server error

If the path refers to a virtual document or a conflicting base document the server will respond with 404. A base document conflict will occur if the parent portion of the path refers to a non-object document.

Example Request To Initialize Document With If-None-Match

  1. PUT /v1/data/us-west/servers HTTP/1.1
  2. Content-Type: application/json
  3. If-None-Match: *
  1. {}

Example Response If Document Already Exists

  1. HTTP/1.1 304 Not Modified

Example Response If Document Does Not Exist

  1. HTTP/1.1 204 No Content

Patch a Document

  1. PATCH /v1/data/{path:.+}
  2. Content-Type: application/json-patch+json

Update a document.

The path separator is used to access values inside object and array documents. If the path indexes into an array, the server will attempt to convert the array index to an integer. If the path element cannot be converted to an integer, the server will respond with 404.

The server accepts updates encoded as JSON Patch operations. The message body of the request should contain a JSON encoded array containing one or more JSON Patch operations. Each operation specifies the operation type, path, and an optional value. For more information on JSON Patch, see RFC 6902.

Status Codes

  • 204 - no content (success)
  • 400 - bad request
  • 404 - not found
  • 500 - server error

The effective path of the JSON Patch operation is obtained by joining the path portion of the URL with the path value from the operation(s) contained in the message body. In all cases, the parent of the effective path MUST refer to an existing document, otherwise the server returns 404. In the case of remove and replace operations, the effective path MUST refer to an existing document, otherwise the server returns 404.

Example Request

  1. PATCH /v1/data/servers HTTP/1.1
  2. Content-Type: application/json-patch+json
  1. [
  2. {"op": "add",
  3. "path": "-",
  4. "value": {
  5. "id": "s5",
  6. "name": "job",
  7. "protocols": ["amqp"],
  8. "ports": ["p3"]
  9. }}
  10. ]

Example Response

  1. HTTP/1.1 204 No Content

Delete a Document

  1. DELETE /v1/data/{path:.+}

Delete a document.

The server processes the DELETE method as if the client had sent a PATCH request containing a single remove operation.

Status Codes

  • 204 - no content (success)
  • 404 - not found
  • 500 - server error

If the path refers to a non-existent document, the server returns 404.

Example Request

  1. DELETE /v1/data/servers HTTP/1.1

Example Response

  1. HTTP/1.1 204 No Content

Query API

Execute a Simple Query

  1. POST /
  2. Content-Type: application/json

Execute a simple query.

OPA serves POST requests without a URL path by querying for the document at path /data/system/main. The content of that document defines the response entirely.

Request Headers

  • Content-Type: application/x-yaml: Indicates the request body is a YAML encoded object.

Query Parameters

  • pretty - If parameter is true, response will formatted for humans.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 404 - not found
  • 500 - server error

If the default decision (defaulting to /system/main) is undefined, the server returns 404.

The policy example below shows how to define a rule that will produce a value for the /data/system/main document. You can configure OPA to use a different URL path to serve these queries. See the Configuration Reference for more information.

The request message body is mapped to the Input Document.

  1. PUT /v1/policies/example1 HTTP/1.1
  2. Content-Type: text/plain
  1. package system
  2. main = msg {
  3. msg := sprintf("hello, %v", input.user)
  4. }

Example Request

  1. POST /
  2. Content-Type: application/json
  1. {
  2. "user": ["alice"]
  3. }

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. "hello, alice"

Execute an Ad-hoc Query

Execute an ad-hoc query and return bindings for variables found in the query.

  1. GET /v1/query

Query Parameters

  • q - The ad-hoc query to execute. OPA will parse, compile, and execute the query represented by the parameter value. The value MUST be URL encoded. Only used in GET method. For POST method the query is sent as part of the request body and this parameter is not used.
  • pretty - If parameter is true, response will formatted for humans.
  • explain - Return query explanation in addition to result. Values: full.
  • metrics - Return query performance metrics in addition to result. See Performance Metrics for more detail.
  • watch - Set a watch on the query if the parameter is present. See Watches for more detail.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 404 - watch ID not found
  • 500 - server error
  • 501 - streaming not implemented

For queries that have large JSON values it is recommended to use the POST method with the query included as the POST body

  1. POST /v1/query HTTP/1.1
  2. Content-Type: application/json
  1. {
  2. "query": "data.servers[i].ports[_] = \"p2\"; data.servers[i].name = name"
  3. }

Example Request

  1. GET /v1/query?q=data.servers[i].ports[_] = "p2"; data.servers[i].name = name HTTP/1.1

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": [
  3. {
  4. "i": 3,
  5. "name": "dev"
  6. },
  7. {
  8. "i": 0,
  9. "name": "app"
  10. }
  11. ]
  12. }

Compile API

Partially Evaluate a Query

  1. POST /v1/compile
  2. Content-Type: application/json

Partially evaluate a query.

The Compile API allows you to partially evaluate Rego queries and obtain a simplified version of the policy. For more details on Partial Evaluation in OPA, see this post on blog.openpolicyagent.org.

Request Body

Compile API requests contain the following fields:

FieldTypeRequriedDescription
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”]]).

Query Parameters

  • pretty - If parameter is true, response will formatted for humans.
  • explain - Return query explanation in addition to result. Values: full.
  • metrics - Return query performance metrics in addition to result. See Performance Metrics for more detail.
  • instrument - Instrument query evaluation and return a superset of performance metrics in addition to result. See Performance Metrics for more detail.

Status Codes

  • 200 - no error
  • 400 - bad request
  • 500 - server error

The example below assumes that OPA has been given the following policy:

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

Example Request

  1. POST /v1/compile HTTP/1.1
  2. Content-Type: application/json
  1. {
  2. "query": "data.example.allow == true",
  3. "input": {
  4. "subject": {
  5. "clearance_level": 4
  6. }
  7. },
  8. "unknowns": [
  9. "data.reports"
  10. ]
  11. }

Example Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": {
  3. "queries": [
  4. [
  5. {
  6. "index": 0,
  7. "terms": [
  8. {
  9. "type": "ref",
  10. "value": [
  11. {
  12. "type": "var",
  13. "value": "gte"
  14. }
  15. ]
  16. },
  17. {
  18. "type": "number",
  19. "value": 4
  20. },
  21. {
  22. "type": "ref",
  23. "value": [
  24. {
  25. "type": "var",
  26. "value": "data"
  27. },
  28. {
  29. "type": "string",
  30. "value": "reports"
  31. },
  32. {
  33. "type": "var",
  34. "value": "i1"
  35. },
  36. {
  37. "type": "string",
  38. "value": "clearance_level"
  39. }
  40. ]
  41. }
  42. ]
  43. }
  44. ]
  45. ]
  46. }
  47. }

Unconditional Results from Partial Evaluation

When you partially evaluate a query with the Compile API, OPA returns a new set of queries and supporting policies. However, in some cases, the result of Partial Evaluation is a conclusive, unconditional answer.

For example, if you extend to policy above to include a “break glass” condition, the decision may be to allow all requests regardless of clearance level.

  1. package example
  2. allow {
  3. input.subject.clearance_level >= data.reports[_].clearance_level
  4. }
  5. allow {
  6. data.break_glass = true
  7. }

In this case, if data.break_glass is true then the query data.example.allow == true will always be true. If the query is always true, the "queries" value in the result will contain an empty array. The empty array indicates that your query can be satisfied without any further evaluation.

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": {
  3. "queries": [
  4. [],
  5. [
  6. {
  7. "index": 0,
  8. "terms": [
  9. {
  10. "type": "ref",
  11. "value": [
  12. {
  13. "type": "var",
  14. "value": "gte"
  15. }
  16. ]
  17. },
  18. {
  19. "type": "number",
  20. "value": 4
  21. },
  22. {
  23. "type": "ref",
  24. "value": [
  25. {
  26. "type": "var",
  27. "value": "data"
  28. },
  29. {
  30. "type": "string",
  31. "value": "reports"
  32. },
  33. {
  34. "type": "var",
  35. "value": "$02"
  36. },
  37. {
  38. "type": "string",
  39. "value": "clearance_level"
  40. }
  41. ]
  42. }
  43. ]
  44. }
  45. ]
  46. ]
  47. }
  48. }

It is also possible for queries to never be true. For example, the original policy could be extended to require that users be granted an exception:

  1. package example
  2. allow {
  3. input.subject.clearance_level >= data.reports[_].clearance_level
  4. exceptions[input.subject.name]
  5. }
  6. exceptions["bob"]
  7. exceptions["alice"]

In this case, if we execute query on behalf of a user that does not have an exception (e.g., "eve"), the OPA response will not contain a queries field at all. This indicates there are NO conditions that could make the query true.

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "result": {}
  3. }

The following table summarizes the behavior for partial evaluation results.

Example QueryUnknownsResultDescription
input.x > 0[“input”]{“result”: {“queries”: [[input.x > 0]]}}The query is partially evaluated and remaining conditions are returned.
input.x > 0Not specified.{“result”: {“queries”: [[input.x > 0]]}}If the set of unknowns is not specified, it defaults to [“input”].
input.x > 0[]{“result”: {}}The query is false/undefined because there are no unknowns.
1 > 0N/A{“result”: {“queries”: [[]]}}The query is always true.
1 < 0N/A{“result”: {}}The query is always false.

The partially evaluated queries are represented as strings in the table above. The actual API response contains the JSON AST representation.

Authentication

The API is secured via HTTPS, Authentication, and Authorization.

Bearer Tokens

When OPA is started with the --authentication=token command line flag, clients MUST provide a Bearer token in the HTTP Authorization header:

  1. GET /v1/data/exempli-gratia HTTP/1.1
  2. Authorization: Bearer my-secret-token

Bearer tokens must be represented with a valid HTTP header value character sequence.

OPA will extract the Bearer token value (which is set to my-secret-token above) and provide it to the authorization component inside OPA that will (i) validate the token and (ii) execute the authorization policy configured by the admin.

Errors

All of the API endpoints use standard HTTP status codes to indicate success or failure of an API call. If an API call fails, the response will contain a JSON encoded object that provides more detail. The errors and location fields are optional:

  1. {
  2. "code": "invalid_parameter",
  3. "message": "error(s) occurred while compiling module(s)",
  4. "errors": [
  5. {
  6. "code": "rego_unsafe_var_error",
  7. "message": "var x is unsafe",
  8. "location": {
  9. "file": "example",
  10. "row": 3,
  11. "col": 1
  12. }
  13. }
  14. ]
  15. }

Method not Allowed

OPA will respond with a 405 Error (Method Not Allowed) if the method used to access the URL is not supported. For example, if a client uses the HEAD method to access any path within “/v1/data/{path:.*}“, a 405 will be returned.

Explanations

OPA supports query explanations that describe (in detail) the steps taken to produce query results.

Explanations can be requested for:

Explanations are requested by setting the explain query parameter to one of the following values:

  • full - returns a full query trace containing every step in the query evaluation process.

By default, explanations are represented in a machine-friendly format. Set the pretty parameter to request a human-friendly format for debugging purposes.

Trace Events

When the explain query parameter is set to full , the response contains an array of Trace Event objects.

Trace Event objects contain the following fields:

  • op - identifies the kind of Trace Event. Values: “Enter”, “Exit”, “Eval”, “Fail”, “Redo”.
  • query_id - uniquely identifies the query that the Trace Event was emitted for.
  • parent_id - identifies the parent query.
  • type - indicates the type of the node field. Values: “expr”, “rule”, “body”.
  • node - contains the AST element associated with the evaluation step.
  • locals - contains the term bindings from the query at the time when the Trace Event was emitted.

Query IDs

Queries often reference rules or contain comprehensions. In both cases, query evaluation involves evaluation of one or more other queries, e.g., the body of the rule or comprehension.

Trace Events from different queries can be distinguished by the query_id field.

Trace Events from related queries can be identified by the parent_id field.

For example, if query A references a rule R, Trace Events emitted as part of evaluating rule R’s body will have the parent_id field set to query A’s query_id.

Types of Events

Each Trace Event represents a step in the query evaluation process. Trace Events are emitted at the following points:

  • enter - before a body or rule is evaluated.
  • exit - after a body or rule has evaluated successfully.
  • eval - before an expression is evaluated.
  • fail - after an expression has evaluated to false.
  • redo - before evaluation restarts from a body, rule, or expression.

By default, OPA searches for all sets of term bindings that make all expressions in the query evaluate to true. Because there may be multiple answers, the search can restart when OPA determines the query is true or false. When the search restarts, a Redo Trace Event is emitted.

Example Trace Event

  1. {
  2. "op": "eval",
  3. "query_id": 20,
  4. "parent_id": 0,
  5. "type": "expr",
  6. "node": {
  7. "index": 1,
  8. "terms": [
  9. {
  10. "type": "var",
  11. "value": "eq"
  12. },
  13. {
  14. "type": "var",
  15. "value": "x"
  16. },
  17. {
  18. "type": "var",
  19. "value": "y"
  20. }
  21. ]
  22. },
  23. "locals": [
  24. {
  25. "key": {
  26. "type": "var",
  27. "value": "x"
  28. },
  29. "value": {
  30. "type": "string",
  31. "value": "hello"
  32. }
  33. }
  34. ]
  35. }

Performance Metrics

OPA can report detailed performance metrics at runtime. Performance metrics can be requested on individual API calls and are returned inline with the API response. To enable performance metric collection on an API call, specify the metrics=true query parameter when executing the API call. Performance metrics are currently supported for the following APIs:

  • Data API (GET and POST)
  • Policy API (all methods)
  • Query API (all methods)

For example:

  1. POST /v1/data/example?metrics=true HTTP/1.1

Response:

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "metrics": {
  3. "timer_rego_query_compile_ns": 69994,
  4. "timer_rego_query_eval_ns": 48425,
  5. "timer_rego_query_parse_ns": 4096
  6. },
  7. "result": {
  8. "some_strings": [
  9. "hello",
  10. "world"
  11. ]
  12. }
  13. }

OPA currently supports the following query performance metrics:

  • timer_rego_input_parse_ns: time taken (in nanoseconds) to parse the input
  • timer_rego_query_parse_ns: time taken (in nanonseconds) to parse the query.
  • timer_rego_query_compile_ns: time taken (in nanonseconds) to compile the query.
  • timer_rego_query_eval_ns: time taken (in nanonseconds) to evaluate the query.
  • timer_rego_module_parse_ns: time taken (in nanoseconds) to parse the input policy module.
  • timer_rego_module_compile_ns: time taken (in nanoseconds) to compile the loaded policy modules.
  • timer_server_handler_ns: time take (in nanoseconds) to handle the API request.

OPA also supports query instrumentation. To enable query instrumentation, specify the instrument=true query parameter when executing the API call. Query instrumentation can help diagnose performance problems, however, it can add significant overhead to query evaluation. We recommend leaving query instrumentation off unless you are debugging a performance problem.

When instrumentation is enabled there are several additional performance metrics for the compilation stages. They follow the format of timer_compile_stage_*_ns and timer_query_compile_stage_*_ns for the query and module compilation stages.

Provenance

OPA can report provenance information at runtime. Provenance information can be requested on individual API calls and are returned inline with the API response. To obtain provenance information on an API call, specify the provenance=true query parameter when executing the API call. Provenance information is currently supported for the following APIs:

  • Data API (GET and POST)

For example:

  1. POST /v1/data/example?provenance=true HTTP/1.1

Response:

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {
  2. "provenance": {
  3. "build_commit": "1955fc4d",
  4. "build_host": "foo.com",
  5. "build_timestamp": "2019-04-29T23:42:04Z",
  6. "revision": "ID-b1298a6c-6ad8-11e9-a26f-d38b5ceadad5",
  7. "version": "0.10.8-dev"
  8. },
  9. "result": true
  10. }

OPA currently supports the following query provenance information:

  • version: The version of this OPA instance.
  • build_commit: The git commit id of this OPA build.
  • build_timestamp: The timestamp when this instance was built.
  • build_host: The hostname where this instance was built.
  • revision: The revision string included in a .manifest file (if present) within a bundle.

Watches

OPA can set watches on queries and report whenever the result of evaluating the query has changed. When a watch is set on a query, the requesting connection will be maintained as the query results are streamed back in HTTP Chunked Encoding format. A notification reflecting a certain change to the query results will be delivered at most once. That is, if a watch is set on data.x, and then multiple writes are made to data.x, say 1, 2 and 3, only the notification reflecting data.x=3 is always seen eventually (assuming the watch is not ended, there are no connection problems, etc). The notifications reflecting data.x=1 and data.x=2 might be seen. However, the notifications sent are guaranteed to be in order (data.x=2 will always come after data.x=1, if it comes).

The notification stream will not be delimited by any value; the client is expected to be able to parse JSON values from the stream one by one, recognizing where one ends and the next begins.

The notification stream is a stream of JSON objects, each of which has the following structure:

  1. {
  2. "result": <result>,
  3. "error": <error>,
  4. "metrics": <metrics>,
  5. "explanation": <explanation>,
  6. }

The error field is optional; it is omitted when no errors occur. If it is present, it is an Error describing any errors encountered while evaluating the query the watch was set on. If the policies on the server are changed such that the query no longer compiles, the contents of the error’s message field will start with the text “watch invalidated:” and will be followed with the reason for invalidation. The watch will be ended by the server and the stream closed.

The metrics field represents Performance Metrics for the evaluation of the query. It will only be present if metrics were requested in the API call that started the watch.

The explanation field represents an Explanation of how the query answer was found. It will only be present if explanations were requested in the API call that started the watch.

If there are no errors, the result field will be a JSON array of the results of evaluating the query. The format of a result is:

  1. {
  2. "expressions": [
  3. {
  4. "value": true,
  5. "text": "a = data.x",
  6. "location":{"row":1,"col":1}
  7. },
  8. ...
  9. ],
  10. "bindings": {...}
  11. }

The expressions field is an array of the results of evaluating each of the expressions in the query. value is the expression’s value, text is the actual expression, and location describes the location of the expression within the query. The values above are examples.

The bindings field is a JSON object mapping strings to JSON values that describe the variable bindings resulting from evaluating the query.

If the watch was set on a data reference instead of a query, the result field will simply be the value of the document requested, instead of an array of values.

Health API

The /health API endpoint executes a simple built-in policy query to verify that the server is operational. Optionally it can account for bundle activation as well (useful for “ready” checks at startup).

Query Parameters

bundle - Boolean parameter to account for bundle activation status in response.

Status Codes

  • 200 - OPA service is healthy. If bundle=true the configured bundle has been activated.
  • 500 - OPA service is not healthy. If bundle=true this can mean the configured bundle has not yet been activated.

Note: The bundle activation check is only for initial startup. Subsequent downloads will not affect the health check. The Status API should be used for more fine-grained bundle status monitoring.

Example Request

  1. GET /health HTTP/1.1

Example Request (bundle activation)

  1. GET /health?bundle=true HTTP/1.1

Healthy Response

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  1. {}

Unhealthy Response

  1. HTTP/1.1 500 Internal Server Error
  2. Content-Type: application/json
  1. {}