usage.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. Filtering by tree position (pages only)
  210. ---------------------------------------
  211. Pages can additionally be filtered by their position of the tree. For this,
  212. there are two parameters you can use: ``?child_of`` and ``?descendant_of``.
  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 ``?descendant_of`` filter also takes the id of a page but includes all
  252. descendants (children of children) instead of just directly children.
  253. Search
  254. ------
  255. Passing a query to the ``?search`` parameter will perform a full-text search on
  256. the results.
  257. The query is split into "terms" (by word boundary), then each term is normalised
  258. (lowercased and unaccented).
  259. For example: ``?search=James+Joyce``
  260. Search operator
  261. ^^^^^^^^^^^^^^^
  262. The ``search_operator`` specifies how multiple terms in the query should be
  263. handled. There are two possible values:
  264. - ``and`` - All terms in the search query (excluding stop words) must exist in
  265. each result
  266. - ``or`` - At least one term in the search query must exist in each result
  267. The ``or`` operator is generally better than ``and`` as it allows the user to be
  268. inexact with their query and the ranking algorithm will make sure that
  269. irrelevant results are not returned at the top of the page.
  270. The default search operator depends on whether the search engine being used by
  271. the site supports ranking. If it does (Elasticsearch), the operator will default
  272. to ``or``. Otherwise (database), it will default to ``and``.
  273. For the same reason, it's also recommended to use the ``and`` operator when
  274. using ``?search`` in conjunction with ``?order`` (as this disables ranking).
  275. For example: ``?search=James+Joyce&order=-first_published_at&search_operator=and``
  276. .. _apiv2_i18n_filters:
  277. Special filters for internationalised sites
  278. -------------------------------------------
  279. When ``WAGTAIL_I18N_ENABLED`` is set to ``True`` (see
  280. :ref:`enabling_internationalisation` for more details) two new filters are made
  281. available on the pages endpoint.
  282. Filtering pages by locale
  283. ^^^^^^^^^^^^^^^^^^^^^^^^^
  284. The ``?locale=`` filter is used to filter the listing to only include pages in
  285. the specified locale. For example:
  286. .. code-block:: text
  287. GET /api/v2/pages/?locale=en-us
  288. HTTP 200 OK
  289. Content-Type: application/json
  290. {
  291. "meta": {
  292. "total_count": 5
  293. },
  294. "items": [
  295. {
  296. "id": 10,
  297. "meta": {
  298. "type": "standard.StandardPage",
  299. "detail_url": "http://api.example.com/api/v2/pages/10/",
  300. "html_url": "http://www.example.com/usa-page/",
  301. "slug": "usa-page",
  302. "first_published_at": "2016-08-30T16:52:00Z",
  303. "locale": "en-us"
  304. },
  305. "title": "American page"
  306. },
  307. ...
  308. ]
  309. }
  310. Getting translations of a page
  311. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  312. The ``?translation_of`` filter is used to filter the listing to only include
  313. pages that are a translation of the specified page ID. For example:
  314. .. code-block:: text
  315. GET /api/v2/pages/?translation_of=10
  316. HTTP 200 OK
  317. Content-Type: application/json
  318. {
  319. "meta": {
  320. "total_count": 2
  321. },
  322. "items": [
  323. {
  324. "id": 11,
  325. "meta": {
  326. "type": "standard.StandardPage",
  327. "detail_url": "http://api.example.com/api/v2/pages/11/",
  328. "html_url": "http://www.example.com/gb-page/",
  329. "slug": "gb-page",
  330. "first_published_at": "2016-08-30T16:52:00Z",
  331. "locale": "en-gb"
  332. },
  333. "title": "British page"
  334. },
  335. {
  336. "id": 12,
  337. "meta": {
  338. "type": "standard.StandardPage",
  339. "detail_url": "http://api.example.com/api/v2/pages/12/",
  340. "html_url": "http://www.example.com/fr-page/",
  341. "slug": "fr-page",
  342. "first_published_at": "2016-08-30T16:52:00Z",
  343. "locale": "fr"
  344. },
  345. "title": "French page"
  346. },
  347. ]
  348. }
  349. Fields
  350. ------
  351. By default, only a subset of the available fields are returned in the response.
  352. The ``?fields`` parameter can be used to both add additional fields to the
  353. response and remove default fields that you know you won't need.
  354. Additional fields
  355. ^^^^^^^^^^^^^^^^^
  356. Additional fields can be added to the response by setting ``?fields`` to a
  357. comma-separated list of field names you want to add.
  358. For example, ``?fields=body,feed_image`` will add the ``body`` and ``feed_image``
  359. fields to the response.
  360. This can also be used across relationships. For example,
  361. ``?fields=body,feed_image(width,height)`` will nest the ``width`` and ``height``
  362. of the image in the response.
  363. All fields
  364. ^^^^^^^^^^
  365. Setting ``?fields`` to an asterisk (``*``) will add all available fields to the
  366. response. This is useful for discovering what fields have been exported.
  367. For example: ``?fields=*``
  368. Removing fields
  369. ^^^^^^^^^^^^^^^
  370. Fields you know that you do not need can be removed by prefixing the name with a
  371. ``-`` and adding it to ``?fields``.
  372. For example, ``?fields=-title,body`` will remove ``title`` and add ``body``.
  373. This can also be used with the asterisk. For example, ``?fields=*,-body``
  374. adds all fields except for ``body``.
  375. Removing all default fields
  376. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  377. To specify exactly the fields you need, you can set the first item in fields to
  378. an underscore (``_``) which removes all default fields.
  379. For example, ``?fields=_,title`` will only return the title field.
  380. Detail views
  381. ------------
  382. You can retrieve a single object from the API by appending its id to the end of
  383. the URL. For example:
  384. - Pages ``/api/v2/pages/1/``
  385. - Images ``/api/v2/images/1/``
  386. - Documents ``/api/v2/documents/1/``
  387. All exported fields will be returned in the response by default. You can use the
  388. ``?fields`` parameter to customise which fields are shown.
  389. For example: ``/api/v2/pages/1/?fields=_,title,body`` will return just the
  390. ``title`` and ``body`` of the page with the id of 1.
  391. .. _apiv2_finding_pages_by_path:
  392. Finding pages by HTML path
  393. --------------------------
  394. You can find an individual page by its HTML path using the ``/api/v2/pages/find/?html_path=<path>`` view.
  395. This will return either a ``302`` redirect response to that page's detail view, or a ``404`` not found response.
  396. For example: ``/api/v2/pages/find/?html_path=/`` always redirects to the homepage of the site
  397. Default endpoint fields
  398. =======================
  399. Common fields
  400. -------------
  401. These fields are returned by every endpoint.
  402. .. glossary::
  403. ``id`` (number)
  404. The unique ID of the object
  405. .. note::
  406. Except for page types, every other content type has its own id space
  407. so you must combine this with the ``type`` field in order to get a
  408. unique identifier for an object.
  409. ``type`` (string)
  410. The type of the object in ``app_label.ModelName`` format
  411. ``detail_url`` (string)
  412. The URL of the detail view for the object
  413. Pages
  414. -----
  415. .. glossary::
  416. ``title`` (string)
  417. ``meta.slug`` (string)
  418. ``meta.show_in_menus`` (boolean)
  419. ``meta.seo_title`` (string)
  420. ``meta.search_description`` (string)
  421. ``meta.first_published_at`` (date/time)
  422. These values are taken from their corresponding fields on the page
  423. ``meta.html_url`` (string)
  424. If the site has an HTML frontend that's generated by Wagtail, this
  425. field will be set to the URL of this page
  426. ``meta.parent``
  427. Nests some information about the parent page (only available on detail
  428. views)
  429. Images
  430. ------
  431. .. glossary::
  432. ``title`` (string)
  433. The value of the image's title field. Within Wagtail, this is used in
  434. the image's ``alt`` HTML attribute.
  435. ``width`` (number)
  436. ``height`` (number)
  437. The size of the original image file
  438. ``meta.tags`` (list of strings)
  439. A list of tags associated with the image
  440. Documents
  441. ---------
  442. .. glossary::
  443. ``title`` (string)
  444. The value of the document's title field
  445. ``meta.tags`` (list of strings)
  446. A list of tags associated with the document
  447. ``meta.download_url`` (string)
  448. A URL to the document file
  449. Changes since v1
  450. ================
  451. Breaking changes
  452. ----------------
  453. - The results list in listing responses has been renamed to ``items`` (was
  454. previously either ``pages``, ``images`` or ``documents``)
  455. Major features
  456. --------------
  457. - The ``fields`` parameter has been improved to allow removing fields, adding
  458. all fields and customising nested fields
  459. Minor features
  460. --------------
  461. - ``html_url``, ``slug``, ``first_published_at``, ``expires_at`` and
  462. ``show_in_menus`` fields have been added to the pages endpoint
  463. - ``download_url`` field has been added to the documents endpoint
  464. - Multiple page types can be specified in ``type`` parameter on pages endpoint
  465. - ``true`` and ``false`` may now be used when filtering boolean fields
  466. - ``order`` can now be used in conjunction with ``search``
  467. - ``search_operator`` parameter was added