Getting started with appdaemon REST API

I’m following this guide.

I have an app (~/homeassistant/conf/apps/hello.py):

import appdaemon.plugins.hass.hassapi as hass

class HelloWorld(hass.Hass):

    def initialize(self):
        self.register_endpoint(self.my_callback, "test_endpoint")

    def my_callback(self, data):
        self.log(data)
        response = {"message": "ok"}
        return response, 200

Which is being run:

$ sudo systemctl status [email protected][email protected] - AppDaemon
   Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-08-31 17:20:23 UTC; 34min ago
 Main PID: 7987 (appdaemon)
   CGroup: /system.slice/system-appdaemon.slice/[email protected]
           └─7987 /usr/bin/python3 /usr/local/bin/appdaemon -c /home/pi/homeassistant/conf

Aug ~-~ INFO AppDaemon: HASS: Connected to Home Assistant 0.77.0
Aug ~-~ INFO AppDaemon: Got initial state from namespace default
Aug ~-~ INFO AppDaemon: Reading config
Aug ~-~ INFO AppDaemon: /home/pi/homeassistant/conf/apps/apps.yaml added or modified
Aug ~-~ INFO AppDaemon: /home/pi/homeassistant/conf/apps/apps.yaml added or modified
Aug ~-~ INFO AppDaemon: App 'hello_world' added
Aug ~-~ INFO AppDaemon: Adding /home/pi/homeassistant/conf/apps to module import path
Aug ~-~ AppDaemon: Loading App Module: /home/pi/homeassistant/conf/apps/hello.py
Aug ~-~ INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
Aug ~-~ INFO AppDaemon: App initialization complete

But I can’t get appdaemon to respond to a request:

$curl -i -X POST -H 'Content-Type: application/json' -d '{test: "yes"}' http://192.168.0.81:5050/api/appdaemon/test_endpont
curl: (7) Failed to connect to 192.168.0.81 port 5050: Connection refused

My appdaemon.yaml:

appdaemon:
  threads: 10
  ad_port: 5050
  plugins:
    HASS:
      type: hass
      ha_url: http://192.168.0.81:8123
      ha_key: 'ha_key'

I’ve tried adding api_key: 'api_key' to appdaemon.yaml, along with -H "x-ad-access: 'api_key'" in the request, didn’t help.

Telnet can’t reach port 5050 either:

$ telnet 192.168.0.81 5050
Trying 192.168.0.81...
telnet: Unable to connect to remote host: Connection refused

Fixed by using api_port instead of ad_port.

Are the docs wrong?

Hello Bro, Can you kindly specify where in the doc it says ad_port as I can’t see it.

Regards

“The RESTFul API is disabled by default, but is enabled by adding an ad_port directive to the AppDaemon section of the configuration file.”

I guess this line is talking about adding:

appdaemon:
  ad_port: 5050

to configuration.yaml, but that does nothing for this problem either.

This is what my appdaemon.yaml file looks like. I put my source in a separate directory so I can back it up cleanly so your app_dir will be different. For the same reason I exclude anything in .git directories so my github local repositories don’t get executed by mistake.

appdaemon:
  threads: 20
  app_dir: /home/appdaemon/code/appdaemon
  api_key: <API Password>
  api_port: 5051
  exclude_dirs:
   - .git
  plugins:
    HASS:
      type: hass
      ha_url: http://hass:8123
      ha_key: <HA_KEY>
      namespace: default

please can you give a link and line where we can find that?

if i look in the docs here:
https://appdaemon.readthedocs.io/en/latest/INSTALL.html

all i can see is talking about api_port
i cant find the line you posted

https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html#restful-api-support

thanks i found it.
thats a small doc error. will be corrected.