API Calls from outside network

Do you have HASS and AppDaemon installed on the same host? If yes, I don’t get how the system knows which port to use for the AppDaemon API… Do you also access HASS remotely on 443?

yup, i got everything on the same device.
and no virtual environments, docker or whatsoever.

everything just with plain pip.

yes i also access hass remotely on 433.
even with the same external domain.

Hummm… Maybe I’m complicating things more than necessary. I will do some tests and report back. Thanks again,

a lot off people do :wink:

in nginx i forward like this:

https://mydomain.duckdns.org > to HA
https://mydomain.duckdns.org/api/appdaemon > to AD api

i have my dashboards not outside but i would set that up like:
https://mydomain.duckdns.org/dash* > dashboard
(i start all my dashboards with dash in the name anyway)

Ok, that explains how the systems knows about the port for the API. Thanks!

1 Like

No matter what I try, I can’t get the API to respond on the port specified (ie 3030). I also tried different ports. I get a “Connection refused” Message both using cURL and the following URL in a browser: http://myLocalIP:3030/api/appdaemon/hello_world?api_password=myPassword. It acts just like if the API was not listening on port 3030, yet the logs tell me it’s started. NGINX is not even in the picture at this point since I’m inside my network using local IP addresses. I should at least get an answer of some sort. The hello_world app compiles correctly according to logs.

I don’t know what to try next… I’m stuck.

Other than the HASSio Add-on logs, are there any logs anywhere else that I can check?

in the appdaemon.yaml you set the logs for appdaemon.

i know the url from the browser wont work, and i cant help you with the curl command at this moment.
maybe that i have the time to find it out on saturday.

This is the cURL command I’m using:

curl -i -X POST -H “Content-Type: application/json” http://myLocalIP:3030/api/appdaemon/hello_world?api_password=myPassord -d ‘{“type”: “Hello World Test”}’

and I get:

Failed to connect to myLocalIP port 3030: Connection refused

This SHOULD work !!! It’s like the API wasn’t listening on port 3030, yet the logs tell me it’s started. Very frustrating to say the least… ARGHHHHHH…

your command did the same by me. nothing and i have a working api.
so this shouldnt work at al. the pasword needs to go outside the url.

i tried:
curl -X POST -H “api_password: my_pw” -H “Content-Type: application/json” -d ‘{“just_a_var”: “Hello world”}’ \http://my_ip:my_port/api/appdaemon/justapi
and created this app:

import appdaemon.appapi as appapi

class justapi(appapi.AppDaemon):

    def initialize(self):
        self.register_endpoint(self.api_call)        

    def api_call(self, data):
      self.log(data)

with the name justapi

i get a 401 unauthorized error. so i know i do something wrong with the password, but in my logs i see an unautorized api call also.

so i am on the right way.
@aimc do you know what i need to change in the curl command to get access?

found it:
a few things:

  1. the curl should be like:
    curl -X POST -H “x-ad-access:YOUR_PW” -H “Content-Type: application/json” -d ‘{“state”: “hello”}’ \http://YOUR_IP:YOUR_PORT/api/appdaemon/YOUR_APP
  2. YOUR_APP in NOT the name from the py file(module) but the name from the instance you set in apps.yaml!!
  3. the app i used looked like:
import appdaemon.appapi as appapi

class justapi(appapi.AppDaemon):

    def initialize(self):
        self.register_endpoint(self.api_call)        

    def api_call(self, data):
      self.log(data)
      return "ok",200

with that url on that app i get state hello in my log and “ok” in the terminal.

the curl:

curl -X POST -H “Content-Type: application/json” -d ‘{“state”: “hello”}’ \http://YOUR_IP:YOUR_PORT/api/appdaemon/YOUR_APP?api_password=YOUR_PW

also works

in the browser the url gets a 405 method not allowed.

Thanks a lot for trying to help. I tried multiple variations of what you proposed and I always get “Connection refused”. I also corrected the single and double quote marks after cut-paste from your message. I even created a new app as a test from your “justapi” class above. All compiles properly in the logs.

I quit, this is not working at all! It should, but it’s not! It may be related to the fact I’m running this in HASSio and the plug-in wasn’t made for this… I really don’t know.

what did you put in your apps.yaml?

What would the the right netstat command (or equivalent) to know which services are listening on which port? I have zero experience with Dockers…

i have even less experience with dockers :wink:

I added this to my apps.yaml file:

test:
  module: test
  class: justapi

and I am calling “test” in my curl command. Of course, the file (module) is called test.py. Everything compiles properly and everything matches. No errors in logs when starting appdaemon

i only get connection refused if i use a port that i didnt configure.
if i try the dashboard port i get a different response.

please show me your appdaemon.yaml again.

appdaemon.yaml:

  AppDaemon:
    logfile: STDOUT
    errorfile: STDERR
    threads: 10
    app_dir: /config/appdaemon/apps
    api_port: 3030
    api_key: !secret api_key
  HASS:
    ha_url: http://myLocalIP:8123
    ha_key: !secret http_password
  HADashboard:
    dash_url: http://127.0.0.1:5050
    dash_dir: /config/appdaemon/dashboards

The Dashboard works well on port 5050.

BTW, I work in IT, so I kinda know what I’m doing…

Thanks for all your help so far…

there where others that had trouble with 3030 (dont know why)
please try to change the api port to 5051

off course a full restart etc.

working in IT can be an advantage but also a disadvantage :wink:
sometimes we get stuck because of what we know, and then its helpfull to have the view of an clueless man :wink:

for hass you got mylocalIP and for dashboard 127.0.0.1
why are they different (or did you just edit different?
and if they are different which one did you use in the curl?