introduction.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. .. _api_introduction:
  2. API Introduction
  3. ============
  4. This document details the Application Programming Interface (API) of the FlexMeasures web service. The API supports user automation for flexibility valorisation in the energy sector, both in a live setting and for the purpose of simulating scenarios. The web service adheres to the concepts and terminology used in the Universal Smart Energy Framework (USEF).
  5. All requests and responses to and from the web service should be valid JSON messages.
  6. For deeper explanations on how to construct messages, see :ref:`api_notation`.
  7. .. _api_versions:
  8. Main endpoint and API versions
  9. ------------------------------
  10. All versions of the API are released on:
  11. .. code-block:: html
  12. https://<flexmeasures-root-url>/api
  13. So if you are running FlexMeasures on your computer, it would be:
  14. .. code-block:: html
  15. https://localhost:5000/api
  16. Let's assume we are running a server for a client at:
  17. .. code-block:: html
  18. https://company.flexmeasures.io/api
  19. where `company` is a client of ours. All their accounts' data lives on that server.
  20. We assume in this document that the FlexMeasures instance you want to connect to is hosted at https://company.flexmeasures.io.
  21. Let's see what the ``/api`` endpoint returns:
  22. .. code-block:: python
  23. >>> import requests
  24. >>> res = requests.get("https://company.flexmeasures.io/api")
  25. >>> res.json()
  26. {'flexmeasures_version': '0.9.0',
  27. 'message': 'For these API versions endpoints are available. An authentication token can be requested at: /api/requestAuthToken. For a list of services, see https://flexmeasures.readthedocs.io',
  28. 'status': 200,
  29. 'versions': ['v3_0']
  30. }
  31. So this tells us which API versions exist. For instance, we know that the latest API version is available at:
  32. .. code-block:: html
  33. https://company.flexmeasures.io/api/v3_0
  34. Also, we can see that a list of endpoints is available on https://flexmeasures.readthedocs.io for each of these versions.
  35. .. note:: Sunset API versions are still documented there, simply select an older version.
  36. .. _api_auth:
  37. Authentication
  38. --------------
  39. Service usage is only possible with a user access token specified in the request header, for example:
  40. .. code-block:: json
  41. {
  42. "Authorization": "<token>"
  43. }
  44. A fresh "<token>" can be generated on the user's profile after logging in:
  45. .. code-block:: html
  46. https://company.flexmeasures.io/logged-in-user
  47. or through a POST request to the following endpoint:
  48. .. code-block:: html
  49. https://company.flexmeasures.io/api/requestAuthToken
  50. using the following JSON message for the POST request data:
  51. .. code-block:: json
  52. {
  53. "email": "<user email>",
  54. "password": "<user password>"
  55. }
  56. which gives a response like this if the credentials are correct:
  57. .. code-block:: json
  58. {
  59. "auth_token": "<authentication token>",
  60. "user_id": "<ID of the user>"
  61. }
  62. .. note:: Each access token has a limited lifetime, see :ref:`api_auth`.
  63. .. _api_deprecation:
  64. Deprecation and sunset
  65. ----------------------
  66. When an API feature becomes obsolete, we deprecate it.
  67. Deprecation of major features doesn't happen a lot, but when it does, it happens in multiple stages, during which we support clients and hosts in adapting.
  68. For more information on our multi-stage deprecation approach and available options for FlexMeasures hosts, see :ref:`Deprecation and sunset for hosts<api_deprecation_hosts>`.
  69. .. _api_deprecation_clients:
  70. Clients
  71. ^^^^^^^
  72. Professional API users should monitor API responses for the ``"Deprecation"`` and ``"Sunset"`` response headers [see `draft-ietf-httpapi-deprecation-header-02 <https://datatracker.ietf.org/doc/draft-ietf-httpapi-deprecation-header/>`_ and `RFC 8594 <https://www.rfc-editor.org/rfc/rfc8594>`_, respectively], so system administrators can be warned when using API endpoints that are flagged for deprecation and/or are likely to become unresponsive in the future.
  73. The deprecation header field shows an `IMF-fixdate <https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1>`_ indicating when the API endpoint was deprecated.
  74. The sunset header field shows an `IMF-fixdate <https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1>`_ indicating when the API endpoint is likely to become unresponsive.
  75. More information about a deprecation, sunset, and possibly recommended replacements, can be found under the ``"Link"`` response header. Relevant relations are:
  76. - ``"deprecation"``
  77. - ``"successor-version"``
  78. - ``"latest-version"``
  79. - ``"alternate"``
  80. - ``"sunset"``
  81. Here is a client-side code example in Python (this merely prints out the deprecation header, sunset header and relevant links, and should be revised to make use of the client's monitoring tools):
  82. .. code-block:: python
  83. def check_deprecation_and_sunset(self, url, response):
  84. """Print deprecation and sunset headers, along with info links.
  85. Reference
  86. ---------
  87. https://flexmeasures.readthedocs.io/en/latest/api/introduction.html#deprecation-and-sunset
  88. """
  89. # Go through the response headers in their given order
  90. for header, content in response.headers:
  91. if header == "Deprecation":
  92. print(f"Your request to {url} returned a deprecation warning. Deprecation: {content}")
  93. elif header == "Sunset":
  94. print(f"Your request to {url} returned a sunset warning. Sunset: {content}")
  95. elif header == "Link" and ('rel="deprecation";' in content or 'rel="sunset";' in content):
  96. print(f"Further info is available: {content}")
  97. .. _api_deprecation_hosts:
  98. Hosts
  99. ^^^^^
  100. FlexMeasures versions go through the following stages for deprecating major features (such as API versions):
  101. - :ref:`api_deprecation_stage_1`: status 200 (OK) with :ref:`relevant headers<api_deprecation_clients>`, plus a toggle to 410 (Gone) for blackout tests
  102. - :ref:`api_deprecation_stage_2`: status 410 (Gone), plus a toggle to 200 (OK) for sunset rollbacks
  103. - :ref:`api_deprecation_stage_3`: status 410 (Gone)
  104. Let's go over these stages in more detail.
  105. .. _api_deprecation_stage_1:
  106. Stage 1: Deprecation
  107. """"""""""""""""""""
  108. When upgrading to a FlexMeasures version that deprecates an API version (e.g. ``flexmeasures==0.12`` deprecates API version 2), clients will receive ``"Deprecation"`` and ``"Sunset"`` response headers [see `draft-ietf-httpapi-deprecation-header-02 <https://datatracker.ietf.org/doc/draft-ietf-httpapi-deprecation-header/>`_ and `RFC 8594 <https://www.rfc-editor.org/rfc/rfc8594>`_, respectively].
  109. Hosts should not expect every client to monitor response headers and proactively upgrade to newer API versions.
  110. Please make sure that your users have upgraded before you upgrade to a FlexMeasures version that sunsets an API version.
  111. You can do this by checking your server logs for warnings about users who are still calling deprecated endpoints.
  112. In addition, we recommend running blackout tests during the deprecation notice phase.
  113. You (and your users) can learn which systems need attention and how to deal with them.
  114. Be sure to announce these beforehand.
  115. Here is an example of how to run a blackout test:
  116. If a sunset happens in version ``0.13``, and you are hosting a version which includes the deprecation notice (e.g. ``0.12``), FlexMeasures will simulate the sunset if you set the config setting ``FLEXMEASURES_API_SUNSET_ACTIVE = True`` (see :ref:`Sunset Configuration<sunset-config>`).
  117. During such a blackout test, clients will receive ``HTTP status 410 (Gone)`` responses when calling corresponding endpoints.
  118. .. admonition:: What is a blackout test
  119. :class: info-icon
  120. A blackout test is a planned, timeboxed event when a host will turn off a certain API or some of the API capabilities.
  121. The test is meant to help developers understand the impact the retirement will have on the applications and users.
  122. `Source: Platform of Trust <https://design.oftrust.net/api-migration-policies/blackout-testing>`_
  123. .. _api_deprecation_stage_2:
  124. Stage 2: Preliminary sunset
  125. """""""""""""""""""""""""""
  126. When upgrading to a FlexMeasures version that sunsets an API version (e.g. ``flexmeasures==0.13`` sunsets API version 2), clients will receive ``HTTP status 410 (Gone)`` responses when calling corresponding endpoints.
  127. In case you have users that haven't upgraded yet, and would still like to upgrade FlexMeasures (to the version that officially sunsets the API version), you can.
  128. For a little while after sunset (usually one more minor version), we will continue to support a "sunset rollback".
  129. To enable this, just set the config setting ``FLEXMEASURES_API_SUNSET_ACTIVE = False`` and consider announcing some more blackout tests to your users, during which you can set this setting to ``True`` to reactivate the sunset.
  130. .. _api_deprecation_stage_3:
  131. Stage 3: Definitive sunset
  132. """"""""""""""""""""""""""
  133. After upgrading to one of the next FlexMeasures versions (e.g. ``flexmeasures==0.14``), clients that call sunset endpoints will receive ``HTTP status 410 (Gone)`` responses.