REST API

Home Assistant provides a RESTful API on the same port as the web frontend. (default port is port 8123).

If you are not using the frontend in your setup then you need to add the api component to your configuration.yaml file.

  • http://IP_ADDRESS:8123/ is an interface to control Home Assistant.
  • http://IP_ADDRESS:8123/api/ is a RESTful API.

The API accepts and returns only JSON encoded objects.

All API calls have to be accompanied by the header Authorization: Bearer ABCDEFGH, where ABCDEFGH is replaced by your token. You can obtain a token ("Long-Lived Access Token") by logging into the frontend using a web browser, and going to your profile http://IP_ADDRESS:8123/profile.

There are multiple ways to consume the Home Assistant Rest API. One is with curl:

curl -X GET \
-H "Authorization: Bearer ABCDEFGH" \
-H "Content-Type: application/json" \
http://IP_ADDRESS:8123/ENDPOINT

Another option is to use Python and the Requests module.

from requests import get
url = "http://localhost:8123/ENDPOINT"
headers = {
"Authorization": "Bearer ABCDEFGH",
"content-type": "application/json",
}
response = get(url, headers=headers)
print(response.text)

Another option is to use the Restful Command integration in a Home Assistant automation or script.

turn_light_on:
url: http://localhost:8123/api/states/light.study_light
method: POST
headers:
authorization: 'Bearer ABCDEFGH'
content-type: 'application/json'
payload: '{"state":"on"}'

Successful calls will return status code 200 or 201. Other status codes that can return are:

  • 400 (Bad Request)
  • 401 (Unauthorized)
  • 404 (Not Found)
  • 405 (Method not allowed)

Actions

The API supports the following actions:

get
/api
๐Ÿ”’
get
/api/config
๐Ÿ”’
get
/api/discovery_info
๐Ÿ”“
get
/api/events
๐Ÿ”’
get
/api/services
๐Ÿ”’
get
/api/history/period/<timestamp>
๐Ÿ”’
get
/api/logbook/<timestamp>
๐Ÿ”’
get
/api/states
๐Ÿ”’
get
/api/states/<entity_id>
๐Ÿ”’
get
/api/error_log
๐Ÿ”’
get
/api/camera_proxy/<camera entity_id>
๐Ÿ”’
post
/api/states/<entity_id>
๐Ÿ”’
post
/api/events/<event_type>
๐Ÿ”’
post
/api/services/<domain>/<service>
๐Ÿ”’
post
/api/template
๐Ÿ”’
post
/api/config/core/check_config
๐Ÿ”’
Last updated on