usage.rst 18 KB

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