How to get Cloud Endpoint

Can you provide details on how to retrieve the cloud endpoint within an App?

It seems that getApiServerUrl() and apiServerUrl("some/path") are both available and return a string:
https://cloud.hubitat.com/api (and https://cloud.hubitat.com/api/some/path respectively)

@patrick’s example in the Mobile App thread seemed to expose HTTP endpoints, but it wasn’t clear to me what the right URL structure was for these endpoints or how to access them?

https://community.hubitat.com/t/is-there-a-mobile-app/111/15?u=josh&source_topic_id=266

I also saw mention of access tokens. It seems that the standard createAccessToken() and state.accessToken features are there… I assume these are used in the standard ways and suppose I can try it / test it once I understand what the URL structure is.


Similarly is there a method for determining the local HTTP endpoint for a SmartApp?

2 Likes

Hi Josh,

Over here we just call them Apps :slight_smile:

For the cloud endpoint you can use something like this to build the url:
"${getApiServerUrl()}/${hubUID}/apps/${app.id}/<PATH>?access_token=${state.accessToken}"

just replace <PATH> with the endpoint you are trying to hit.

For the local enpoint , we are going to add a method for this, but right now you can access it like this:
http://<HUB IP>/apps/api/<APP ID>/<PATH>?access_token=<ACCESS TOKEN>
just replace the <> values with the ones that match your Hub and App

5 Likes

Thanks, Chuck! I’ve been so good about calling them Apps and slipped up! Old habits die hard, I guess. :stuck_out_tongue:

Also, how is progress coming on the OAuth flow?

This looks like just what I need. Where do I get the App ID? Is that the same as the OAUTH ClientID? I guess I'll have to browse over to @patrick 's mobile app thread.

It’s just app.id - see the cloud string for reference.

well… i still don’t quite get it. I can’t see the log from the app so how do i get app.id

I’m calling from a php script.
Edit never mind. I think I have it figured out. I Can just make a http post from groovy app to my php app and save it over there in a cookie or session variable. Unless anyone know of an easier way? Also with both now local on the same network I can subscribe to events and post changes instead of JavaScript polling I had to do in ST.

you should be able to do a simple:

log.debug app.id

from the app and it will show up in the log window.

Otherwise in the UI from the main page, click on Apps, then click on the app you want to know the id of. The id will be in the address bar of your browser.

1 Like

Nothing shows in the log at all for me - ever…

Even after you leave it open? It only shows live events not a history.

Yup - I’m getting nothing
EDIT: Never mind… you were right… it works in real time.
Got the app.id and the state.accessToken this way… all set

1 Like

Progress… pulling the things from the HousePanel app in groovy on Hubitat and mixing successfully with those from SmartThings… I am writing the panel to read from both systems so they can co-exist into the same HousePanel instance since most people will migrate gradually.

1 Like

This worked (sort of). To get it to work I had to use the Authorization Bearer call before invoking curl in PHP. Here is what the code looks like:

$host = HUBITAT_HOST . "/apps/api/" . HUBITAT_ID . "/" . $path;
$headertype = array("Authorization: Bearer " . HUBITAT_ACCESS_TOKEN);
$nvpreq = "access_token=" . HUBITAT_ACCESS_TOKEN;
$response = curl_call($host, $headertype, $nvpreq, "POST");

where curl_call is a simple function call to invoke curl in PHP. I grabbed the id and access_token from a log and stored it in a PHP constant. In the future I will use a post function to send it over to the code. I think this is how AT works. That way a user will only need to enter the URL of their HP installation.

1 Like

Just a quick tip on how to dynamically build a local endpoint in an App. Just replace the <YOUR_PATH_HERE> with your app’s path. The only change I made to what was posted earlier in this thread is a method to retrieve the hub’s local IP address on the fly.

	  	def localEndPoint = "http://${location.hubs[0].getDataValue("localIP")}/apps/api/${app.id}/<YOUR_PATH_HERE>?access_token=${state.accessToken}"

Hopefully this helps someone else. The more local-to-local integrations we can create, the better!

2 Likes

Thanks Dan ... very useful for pushing the hub settings over to my PHP script to eliminate need for user setup.

Ken,

Which Hubitat firmware version are you currently running? I believe version .678 has some additional helper functions for generating OAUTH endpoint strings which make this even easier. I just learned of them from Josh (@josh). My hub did update successfully to .678 before it was pulled due to a few users having issues with the update process.

If you have .678, you may want to give these a try:

Cloud

  • getApiServerUrl()
  • getFullApiServerUrl()
  • apiServerUrl(String url)
  • fullApiServerUrl(String url)

Local

  • getLocalApiServerUrl()
  • getFullLocalApiServerUrl()
  • localApiServerUrl(String url)
  • fullLocalApiServerUrl(String url)

So, my previous post would change to simply be:

    def localEndPoint = fullLocalApiServerUrl("<YOUR_PATH_HERE>") + "?access_token=${state.accessToken}"
4 Likes

I’m not sure —- not home right now but will check. I’m calling Hubitat from PHP so I have to use authorization bearer header calls for the access token. Rest should work fine. Thanks for the tip.

Sorry about resurrecting such an old thread, but I am looking for how to properly get the access_token and endpoiint using OAUTH2 flow. I have been using the methods described here and I now understand that OAUTH2 flow is fully implemented and returns this information if one has a client_id and a client_secret just like it works on SmartThings. Do I have this right? If so can someone advise if the calls are the same with cloud.hubitat as the url to use to make the oauth calls?

I still don't understand how to get the access token for an app. Do you have to do that within the app? I'm very confused.

Which app? If it's a custom app, then yes, you would do it within the app itself.

Something like:

if(!state.accessToken){	
    createAccessToken() //be sure to enable OAuth in the app settings or this call will fail
}
def localUri = getFullLocalApiServerUrl("optional/path") + "/?access_token=${state.accessToken}"
1 Like

Okay...but how do I get that token to use? That's the part that I'm missing.