The home assistant API can be used to build external tools to manipulate information in home assistant and control elements in it. This can be useful for triggering automations with custom code. It can even be triggered using curl on linux, allowing for simple scripts executing home assistant actions.
Enabling API Integration
Enable the RESTful API in home assistant is oddly straight forward. Adding one line to configuration.yaml in home assistant turns it on.
# Example configuration.yaml entry
api:
The HTTP settings can also be adjusted (though not necessary) to improve the security of the API.
http:
server_port: 8123
login_attempts_threshold: 5
Once all changes are done, restart home assistant core.
Adding a Long Lived Access Token
The process for adding a long lived access token is straight forward and is needed to authenticate to the API. Its done from your profile page near the bottom of the page.
Give the token a name and continue. This name will be viewable in the Home Assistant GUI as the identification for removing old tokens that are no longer needed.
Make sure to copy the WHOLE STRING (mine ended up being 183 characters). I tried just double-clicking and copying on my first attempt, which picked up one grouping of characters (probably 20-30 but not the whole 183). So make sure to click in the field and CTRL-A and copy all of the text.
Testing
The API will return 404’s unless you include authentication in your header, so you need that long lived access token to check to make sure everything is working correctly. An example curl command is below showing the format and can be used for testing the API. Make sure to use the correct FQDN or IP address for the curl command.
curl -X GET -H "Authorization: Bearer ABCDEFG" -H "Content-Type: application/json" http://hass.local:8123/api/
This will get you a working API and getting a bit started with using the API.