How to call supervisor API correctly

I want to implement some functions of HA.
And I can call the Restful API via python, but I need some advanced functions.
So I try to call the supervisor API via python and curl like below:

token="my_token"
headers = {
	"Authorization": f"Bearer {token}",
	"content-type": "application/json",
}
url = "http://supervisor/core/info"
ret = requests.get(url, headers=headers)
print(ret)
curl -X GET -H "Authorization: Bearer ${MY_TOKEN}" -H "Content-Type: application/json" http://supervisor/core/api/config

And the error of python will return:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='supervisor', port=80): Max retries exceeded with url: /core/api (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb372011280>: Failed to establish a new connection: [Errno -2] Name or service not known'))

The error of curl will return:

curl: (6) Could not resolve host: supervisor

I follow the document Home Assistant Core and Supervisor API Access
I have already installed Home Assistant Supervised and get the supervisor token, but if I add either of “homeassistant_api: true” or “hassio_api: true” in configuration.yaml - the configuration will checker fails.

Any help would be appreciated! It seems like there are some steps that I’m missing to enable the Supervisor API to be called.

Where are you running this from? In the same environment can you try

nslookup supervisor

That should probably be:

url = "http://IP_ADDRESS:8123/api/core/info"

Where IP_ADDRESS is the IP address of your home assistant server.

See:

https://developers.home-assistant.io/docs/api/rest

and

https://developers.home-assistant.io/docs/api/supervisor/endpoints#core

I run the curl in the host terminate.
and nslookup return
server can't find supervisor: NXDOMAIN

Sorry, I have tried it before,
It seems like that URL format is for Restful API not for supervisor API.

You mentioned a supervised install, so the host terminal will not access the HA dns addon. Try in a terminal provided by the SSH & Web Terminal addon.

I just looked up one of my restful sensors. It uses:

resource: http://homeassistant:8123/api/hassio/core/stats

So I guess you need:

http://homeassistant:8123/api/hassio/core/info

There is something being served by port 80 in the supervisor container:

 nmap supervisor
Starting Nmap 7.92 ( https://nmap.org ) at 2022-09-06 18:00 NZST
Nmap scan report for supervisor (172.30.32.2)
Host is up (0.000016s latency).
rDNS record for 172.30.32.2: hassio
Not shown: 999 closed tcp ports (reset)
PORT   STATE SERVICE
80/tcp open  http
MAC Address: 02:42:AC:1E:20:02 (Unknown)

Thanks for reply,

Yes, I can use Supervisor API via SSH, but I want to expose its API externally and using python to call it, so I use remote API addon to get supervisor token and try to call it by using curl for the test, if it can work, then I can develop my own add-on in next step, then I can implement my project. According to this dev say in here temporarily expose its API externally on a dev system like Then it will listen on a port on the host and proxy requests made through it to the supervisor API., it seems like it can work, but I stuck here…

I have also tried this before, it will return Could not resolve host: homeassistant

Yes, so I have tried

curl -sSL -H "Authorization: Bearer $MY_SUPERVISOR_TOKEN" http://MY_HA_IP:remoteAPI_port(80)/core/api

The error will change to 401: Unauthorized, but I confirmed that the supervisor token is correct.

Where do you get the supervisor token (I can’t seem to find it to experiment)

It have to install remote API addon, follow the step below this document.
And this add-on must based on Home Assistant Supervised rather Home Assistant Container

Us the IP address then.

It return nothing…by using curl

This works

nick@hass:~$ cat testapi.py
import requests
token="my_token"
headers = {
        "Authorization": f"Bearer {token}",
        "content-type": "application/json",
}
url = "http://172.30.32.2/core/info"
ret = requests.get(url, headers=headers)
print(ret)

then invoking it

nick@hass:~$ python3 testapi.py
<Response [200]>

It works!
So it has to use the IP of hassio without the port,
I’m an idiot to use the IP of docker and add the port to it.
You’re really something! Thank you for your help.

I don’t think though that the IP address of supervisor will be always the same. So it may fail at the point if the IP address changes.

I have no idea whether it will change or not when the home-assistant restart or reinstall, maybe I can extract IP by bash to avoid the inconsistency when I need to call API. I think the big problem for me is how to get the supervisor token by myself without the remote API addon.