usage.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. ==========================
  2. Wagtail API v2 Usage Guide
  3. ==========================
  4. .. note::
  5. This is the usage guide for version 2 of the API. For version 1, see
  6. :doc:`/reference/contrib/api/usage`
  7. The Wagtail API module exposes a public, read only, JSON-formatted API which
  8. can be used by external clients (such as a mobile app) or the site's frontend.
  9. This document is intended for developers using the API exposed by Wagtail. For
  10. documentation on how to enable the API module in your Wagtail site, see
  11. :doc:`/advanced_topics/api/v2/configuration`
  12. .. contents::
  13. Fetching content
  14. ================
  15. To fetch content over the API, perform a ``GET`` request against one of the
  16. following endpoints:
  17. - Pages ``/api/v2/pages/``
  18. - Images ``/api/v2/images/``
  19. - Documents ``/api/v2/documents/``
  20. .. note::
  21. The available endpoints and their URLs may vary from site to site, depending
  22. on how the API has been configured.
  23. Example response
  24. ----------------
  25. Each response contains the list of items (``items``) and the total count
  26. (``meta.total_count``). The total count is irrespective of pagination.
  27. .. code-block:: text
  28. GET /api/v2/endpoint_name/
  29. HTTP 200 OK
  30. Content-Type: application/json
  31. {
  32. "meta": {
  33. "total_count": "total number of results"
  34. },
  35. "items": [
  36. {
  37. "id": 1,
  38. "meta": {
  39. "type": "app_name.ModelName",
  40. "detail_url": "http://api.example.com/api/v2/endpoint_name/1/"
  41. },
  42. "field": "value"
  43. },
  44. {
  45. "id": 2,
  46. "meta": {
  47. "type": "app_name.ModelName",
  48. "detail_url": "http://api.example.com/api/v2/endpoint_name/2/"
  49. },
  50. "field": "different value"
  51. }
  52. ]
  53. }
  54. .. _apiv2_custom_page_fields:
  55. Custom page fields in the API
  56. -----------------------------
  57. Wagtail sites contain many page types, each with their own set of fields. The
  58. ``pages`` endpoint will only expose the common fields by default (such as
  59. ``title`` and ``slug``).
  60. To access custom page fields with the API, select the page type with the
  61. ``?type`` parameter. This will filter the results to only include pages of that
  62. type but will also make all the exported custom fields for that type available
  63. in the API.
  64. For example, to access the ``published_date``, ``body`` and ``authors`` fields
  65. on the ``blog.BlogPage`` model in the :ref:`configuration docs <apiv2_page_fields_configuration>`:
  66. .. code-block:: text
  67. GET /api/v2/pages/?type=blog.BlogPage&fields=published_date,body,authors(name)
  68. HTTP 200 OK
  69. Content-Type: application/json
  70. {
  71. "meta": {
  72. "total_count": 10
  73. },
  74. "items": [
  75. {
  76. "id": 1,
  77. "meta": {
  78. "type": "blog.BlogPage",
  79. "detail_url": "http://api.example.com/api/v2/pages/1/",
  80. "html_url": "http://www.example.com/blog/my-blog-post/",
  81. "slug": "my-blog-post",
  82. "first_published_at": "2016-08-30T16:52:00Z"
  83. },
  84. "title": "Test blog post",
  85. "published_date": "2016-08-30",
  86. "authors": [
  87. {
  88. "id": 1,
  89. "meta": {
  90. "type": "blog.BlogPageAuthor",
  91. },
  92. "name": "Karl Hobley"
  93. }
  94. ]
  95. },
  96. ...
  97. ]
  98. }
  99. .. note::
  100. Only fields that have been explicitly exported by the developer may be used
  101. in the API. This is done by adding a ``api_fields`` attribute to the page
  102. model. You can read about configuration :ref:`here <apiv2_page_fields_configuration>`.
  103. This doesn't apply to images/documents as there is only one model exposed in
  104. those endpoints. But for projects that have customised image/document models,
  105. the ``api_fields`` attribute can be used to export any custom fields into the
  106. API.
  107. Pagination
  108. ----------
  109. The number of items in the response can be changed by using the ``?limit``
  110. parameter (default: 20) and the number of items to skip can be changed by using
  111. the ``?offset`` parameter.
  112. For example:
  113. .. code-block:: text
  114. GET /api/v2/pages/?offset=20&limit=20
  115. HTTP 200 OK
  116. Content-Type: application/json
  117. {
  118. "meta": {
  119. "total_count": 50
  120. },
  121. "items": [
  122. pages 20 - 40 will be listed here.
  123. ]
  124. }
  125. .. note::
  126. There may be a maximum value for the ``?limit`` parameter. This can be
  127. modified in your project settings by setting ``WAGTAILAPI_LIMIT_MAX`` to
  128. either a number (the new maximum value) or ``None`` (which disables maximum
  129. value check).
  130. Ordering
  131. --------
  132. The results can be ordered by any field by setting the ``?order`` parameter to
  133. the name of the field to order by.
  134. .. code-block:: text
  135. GET /api/v2/pages/?order=title
  136. HTTP 200 OK
  137. Content-Type: application/json
  138. {
  139. "meta": {
  140. "total_count": 50
  141. },
  142. "items": [
  143. pages will be listed here in ascending title order (a-z)
  144. ]
  145. }
  146. The results will be ordered in ascending order by default. This can be changed
  147. to descending order by prefixing the field name with a ``-`` sign.
  148. .. code-block:: text
  149. GET /api/v2/pages/?order=-title
  150. HTTP 200 OK
  151. Content-Type: application/json
  152. {
  153. "meta": {
  154. "total_count": 50
  155. },
  156. "items": [
  157. pages will be listed here in descending title order (z-a)
  158. ]
  159. }
  160. .. note::
  161. Ordering is case-sensitive so lowercase letters are always ordered after
  162. uppercase letters when in ascending order.
  163. Random ordering
  164. ^^^^^^^^^^^^^^^
  165. Passing ``random`` into the ``?order`` parameter will make results return in a
  166. random order. If there is no caching, each request will return results in a
  167. different order.
  168. .. code-block:: text
  169. GET /api/v2/pages/?order=random
  170. HTTP 200 OK
  171. Content-Type: application/json
  172. {
  173. "meta": {
  174. "total_count": 50
  175. },
  176. "items": [
  177. pages will be listed here in random order
  178. ]
  179. }
  180. .. note::
  181. It's not possible to use ``?offset`` while ordering randomly because
  182. consistent random ordering cannot be guarenteed over multiple requests
  183. (so requests for subsequent pages may return results that also appeared in
  184. previous pages).
  185. Filtering
  186. ---------
  187. Any field may be used in an exact match filter. Use the filter name as the
  188. parameter and the value to match against.
  189. For example, to find a page with the slug "about":
  190. .. code-block:: text
  191. GET /api/v2/pages/?slug=about
  192. HTTP 200 OK
  193. Content-Type: application/json
  194. {
  195. "meta": {
  196. "total_count": 1
  197. },
  198. "items": [
  199. {
  200. "id": 10,
  201. "meta": {
  202. "type": "standard.StandardPage",
  203. "detail_url": "http://api.example.com/api/v2/pages/10/",
  204. "html_url": "http://www.example.com/about/",
  205. "slug": "about",
  206. "first_published_at": "2016-08-30T16:52:00Z"
  207. },
  208. "title": "About"
  209. },
  210. ]
  211. }
  212. Filtering by tree position (pages only)
  213. ---------------------------------------
  214. Pages can additionally be filtered by their position of the tree. For this,
  215. there are two parameters you can use: ``?child_of`` and ``?descendant_of``.
  216. The ``?child_of`` filter takes the id of a page and filters the list of results
  217. to contain only direct children of that page.
  218. For example, this can be useful for constructing the main menu, by passing the
  219. id of the homepage to the filter:
  220. .. code-block:: text
  221. GET /api/v2/pages/?child_of=2&show_in_menus=true
  222. HTTP 200 OK
  223. Content-Type: application/json
  224. {
  225. "meta": {
  226. "total_count": 5
  227. },
  228. "items": [
  229. {
  230. "id": 3,
  231. "meta": {
  232. "type": "blog.BlogIndexPage",
  233. "detail_url": "http://api.example.com/api/v2/pages/3/",
  234. "html_url": "http://www.example.com/blog/",
  235. "slug": "blog",
  236. "first_published_at": "2016-09-21T13:54:00Z"
  237. },
  238. "title": "About"
  239. },
  240. {
  241. "id": 10,
  242. "meta": {
  243. "type": "standard.StandardPage",
  244. "detail_url": "http://api.example.com/api/v2/pages/10/",
  245. "html_url": "http://www.example.com/about/",
  246. "slug": "about",
  247. "first_published_at": "2016-08-30T16:52:00Z"
  248. },
  249. "title": "About"
  250. },
  251. ...
  252. ]
  253. }
  254. The ``?descendant_of`` filter also takes the id of a page but includes all
  255. descendants (children of children) instead of just directly children.
  256. Search
  257. ------
  258. Passing a query to the ``?search`` parameter will perform a full-text search on
  259. the results.
  260. The query is split into "terms" (by word boundary), then each term is normalised
  261. (lowercased and unaccented).
  262. For example: ``?search=James+Joyce``
  263. Search operator
  264. ^^^^^^^^^^^^^^^
  265. The ``search_operator`` specifies how multiple terms in the query should be
  266. handled. There are two possible values:
  267. - ``and`` - All terms in the search query (excluding stop words) must exist in
  268. each result
  269. - ``or`` - At least one term in the search query must exist in each result
  270. The ``or`` operator is generally better than ``and`` as it allows the user to be
  271. inexact with their query and the ranking algorithm will make sure that
  272. irrelevant results are not returned at the top of the page.
  273. The default search operator depends on whether the search engine being used by
  274. the site supports ranking. If it does (Elasticsearch), the operator will default
  275. to ``or``. Otherwise (database), it will default to ``and``.
  276. For the same reason, it's also recommended to use the ``and`` operator when
  277. using ``?search`` in conjunction with ``?order`` (as this disables ranking).
  278. For example: ``?search=James+Joyce&order=-first_published_at&search_operator=and``
  279. Fields
  280. ------
  281. By default, only a subset of the available fields are returned in the response.
  282. The ``?fields`` parameter can be used to both add additional fields to the
  283. response and remove default fields that you know you won't need.
  284. Additional fields
  285. ^^^^^^^^^^^^^^^^^
  286. Additional fields can be added to the response by setting ``?fields`` to a
  287. comma-separated list of field names you want to add.
  288. For example, ``?fields=body,feed_image`` will add the ``body`` and ``feed_image``
  289. fields to the response.
  290. This can also be used across relationships. For example,
  291. ``?fields=body,feed_image(width,height)`` will nest the ``width`` and ``height``
  292. of the image in the response.
  293. All fields
  294. ^^^^^^^^^^
  295. Setting ``?fields`` to an asterisk (``*``) will add all available fields to the
  296. response. This is useful for discovering what fields have been exported.
  297. For example: ``?fields=*``
  298. Removing fields
  299. ^^^^^^^^^^^^^^^
  300. Fields you know that you do not need can be removed by prefixing the name with a
  301. ``-`` and adding it to ``?fields``.
  302. For example, ``?fields=-title,body`` will remove ``title`` and add ``body``.
  303. This can also be used with the asterisk. For example, ``?fields=*,-body``
  304. adds all fields except for ``body``.
  305. Removing all default fields
  306. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  307. To specify exactly the fields you need, you can set the first item in fields to
  308. an underscore (``_``) which removes all default fields.
  309. For example, ``?fields=_,title`` will only return the title field.
  310. Detail views
  311. ------------
  312. You can retrieve a single object from the API by appending its id to the end of
  313. the URL. For example:
  314. - Pages ``/api/v2/pages/1/``
  315. - Images ``/api/v2/images/1/``
  316. - Documents ``/api/v2/documents/1/``
  317. All exported fields will be returned in the response by default. You can use the
  318. ``?fields`` parameter to customise which fields are shown.
  319. For example: ``/api/v2/pages/1/?fields=_,title,body`` will return just the
  320. ``title`` and ``body`` of the page with the id of 1.
  321. Default endpoint fields
  322. =======================
  323. Common fields
  324. -------------
  325. These fields are returned by every endpoint.
  326. .. glossary::
  327. ``id`` (number)
  328. The unique ID of the object
  329. .. note::
  330. Except for page types, every other content type has its own id space
  331. so you must combine this with the ``type`` field in order to get a
  332. unique identifier for an object.
  333. ``type`` (string)
  334. The type of the object in ``app_label.ModelName`` format
  335. ``detail_url`` (string)
  336. The URL of the detail view for the object
  337. Pages
  338. -----
  339. .. glossary::
  340. ``title`` (string)
  341. ``meta.slug`` (string)
  342. ``meta.show_in_menus`` (boolean)
  343. ``meta.seo_title`` (string)
  344. ``meta.search_description`` (string)
  345. ``meta.first_published_at`` (date/time)
  346. These values are taken from their corresponding fields on the page
  347. ``meta.html_url`` (string)
  348. If the site has an HTML frontend that's generated by Wagtail, this
  349. field will be set to the URL of this page
  350. ``meta.parent``
  351. Nests some information about the parent page (only available on detail
  352. views)
  353. Images
  354. ------
  355. .. glossary::
  356. ``title`` (string)
  357. The value of the image's title field. Within Wagtail, this is used in
  358. the image's ``alt`` HTML attribute.
  359. ``width`` (number)
  360. ``height`` (number)
  361. The size of the original image file
  362. ``meta.tags`` (list of strings)
  363. A list of tags associated with the image
  364. Documents
  365. ---------
  366. .. glossary::
  367. ``title`` (string)
  368. The value of the document's title field
  369. ``meta.tags`` (list of strings)
  370. A list of tags associated with the document
  371. ``meta.download_url`` (string)
  372. A URL to the document file
  373. Changes since v1
  374. ================
  375. Breaking changes
  376. ----------------
  377. - The results list in listing responses has been renamed to ``items`` (was
  378. previously either ``pages``, ``images`` or ``documents``)
  379. Major features
  380. --------------
  381. - The ``fields`` parameter has been improved to allow removing fields, adding
  382. all fields and customising nested fields
  383. Minor features
  384. --------------
  385. - ``html_url``, ``slug``, ``first_publised_at``, ``expires_at`` and
  386. ``show_in_menus`` fields have been added to the pages endpoint
  387. - ``download_url`` field has been added to the documents endpoint
  388. - Multiple page types can be specified in ``type`` parameter on pages endpoint
  389. - ``true`` and ``false`` may now be used when filtering boolean fields
  390. - ``order`` can now be used in conjunction with ``search``
  391. - ``search_operator`` parameter was added