[App][Pro] Local API

Hi all!
I’ve just created an app: Local API

Local API create a local API server on your Homey.

You can create Endpoints to the app simply by adding a flow card and use it to start a new flow or communicate a variable state in the JSON response.

The Response is always a JSON object.

HOW TO USE:

  1. Install the app
  2. Set up the app in the settings page and restart it
  3. Create a new flow with the Local API app: remember to add the trigger card
  4. Personalize the flow as you like
  5. End the flow with at least one of the following cards:
    Respond with … (remember to add the JSON response)
    Respond with 200
  6. Save the flow and test it with a browser. The URL is http://homey-ip:<PORT>/endpoint-name

NOTE:
Don’t use this app to expose your Homey to the internet. Its use is intended for local network only.

4 Likes

[reserved for future use]

can you give an example how and where to use it exactly for?

Homey already has webhooks, i believe it does simular or exactly the same…

Without the “Respond with …” part, which is very useful.

1 Like

Hello @B3rt ,
(thanks Robert for answering before me) as @robertklep mentioned, this app has 2 major advantages over the original webhooks: being able to completely decide the path to call on a given server port and the possibility of answering a given call with some text or with a JSON containing information saved on Flow Homey.

This means that not only can you use webhooks to start a flow, but at the same time you can expect a response to the same webhook (like a normal REST API).

In the future I will add a formal check on the JSON written by the user and the possibility to customize the port used (at the moment by default it occupies 3000).

Example:

2 Likes

Thx…

The idea is great so i am gonna try, test and use it (if it works for me)

I installed it and tried to use it, currently i use 1 or 2 webhooks to process data i send from my NAS to Homey, but how do i proces the data what is received/send to Homey using this local API exactly?

For example, i created a new test flow with the IF card:
A GET request received on url /test
The i send to homey the url http://ip.my.homey:3000/test?result=true&msg=testing

How do i get the data what was send to /test
So how do i proces the ?result=true&msg=testing
Or simply only 1param: /test/simple or /test/result=true or /test/true etc

I cannot see any AND cards of the local API to proces the data, there is also no tag card available to use with a logic card to filter/proces the data which was send.

At the moment it seems i only can trigger something but no process any data send, it would be great if it was possible to also send data to homey which is put in a tag card, using logic it would be possible to proces this data and respond using your local API

Can you explain it further if this possible and to do this?

edit:
tip: add (via settings) an option to limit access, for example only local network (only IP range or specific ip 192.168.1.x 192.168.1.1,192.168.1.2, etc)

Consider that it is a first version, surely there will still be a lot of things to implement and improve.

The API at the moment is only “woken up” by the request on the URL (at low level it takes the exact match between URL and what is set in the Flow), this means that (in this first version) the App is not able to read a variable from the address, which is used only as an Entry Point.

The entry point can be used to trigger a Flow and the corresponding response can be used to export and expose data (in JSON or direct response) already present on the homey as a tag (in combination with other flows).

Any improvement proposal is welcome, as the next step I will first make the server port settable, limit access to only some IPs.
Before add the possibility of reading variables from the GET or from the POST body, I have to understand how to make the data “safe”.

2 Likes

I am experimenting at the moment. Your app could be very useful in interaction with Home Assistant, especially if some HA apps don’t migrate to SDK3 in time.

  • Triggering Homey Flows via HA works very well and without delay. I have created a template where I only need to change the name of the API request in the automation.

  • At the moment I’m still struggling to process the value reported back in HA. But I will get that right in the end.

It would be great if your app is able to receive json at some point. It could then also process values that are sent to Homey.

…but that will take time.

1 Like

I have now also found the solution how to create a virtual sensor with this app in Home Assiatant without going the diversions via MQTT.

As an example, a temperature:
Create the following flow in Homey. Please use your own name for the api. Use the value of the sensor as the tag:

Then create a virtual sensor in configuration.yaml in HA:

sensor:

  - platform: rest
    resource: http://192.168.1.168:3000/test
    name: temperature
    unit_of_measurement: "°C"
    value_template: "{{ value_json.data }}"

Here you also use the IP of your homey:3000/your_name
Use yor own name.
Use your own unit_of_measurement.

Please restart HA, thats it.

4 Likes

nice…

But how often does HA check this virtual sensor on updates?

It checks every 30 seconds.

and does HA this by itself by just adding the sensor code in your config or is there also an automation what does the check interval?

Automatically (it’s part of how RESTful sensors are implemented).

Thank you very much! This is exactly what i’ve been looking for! I’m trying to implement a dashboard on micro web server homey app and since there’s no access to backend i can only use javascript (or equivalent frontend language) for fetching data. Problem is that the requests halt on CORS error. With a little googling this seems to be a backend setting. I could of course run my dashboard on a raspberry and do the api calls in the backend and wouldn’t have this issue, but i’m trying to find a way to run a dashboard without needing to have another device for the task. I noticed that there is already issue open for this in github and I’d like to know if this can be fixed in the application near future (meaning also if homey even allows app developers to modify this setting)? Also would be great if anyone knows of a workaround for doing the api call from javascript (browser, not node.js backend) without getting the CORS error.

Hello,
I’m working on a version that has a CORS option, it will certainly be in the next update.

However I have to create the settings page first, which doesn’t exist today, in order to give the possibility to enable or disable them.

The same update will allow you to customize the connection port (today it’s hardcoded to 3000).

1 Like

Hi all,
I’ve published an update (in Test at the moment, if you want to try it you can go here: Local API | Homey )
so, what’s new?
I’ve added the settings page,
you can now decide on what port the localAPI is listening,
CORS support (please, note: by default is disabled, you can enable it from the settings page).

I’ll try it for a few days, if it’s all ok I proceed to submit the Test version for publish.

Paolo

1 Like

Thank you very much! I’m still however getting the same error when trying to fetch the results with browser side javascript: “Access to fetch at ‘http://192.168.1.10:3000/test6’ from origin ‘null’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.” I tried with CORS disabled on the Local API app and with setting it to enabled and using value *. Am I doing something wrong?

Hi jumey,
It’s a possible bug (that’s why I have publish it in test only)
have you restarted the app after save settings?

Hi! I understand that it’s still a test version and I’m also trying to help with testing :slight_smile: Tried restarting, but didn’t help. Moved the webpage from my computer to homey micro web server and got the origin to show the ip address of my homey instead of null, but still the error remained.

1 Like

Thanks for the feedback,
in these evenings I try to debug to investigate the problem, I’ll keep you updated!

1 Like