Questions about Rest API

Hello the community,
I would like to make a REST request to gladys, but I am blocked by authentication, (error 401)

I cannot find the information about the type of authentication used or which password to use? (raspberry account or gladys admin)

For information, I am using python and the requests module (I am a beginner in programming)

Thank you

Hello,

I don’t know if you’ve seen it, but in the Gladys GitHub repository, there is an export of the Insomnia Core software that allows you to test requests on the Gladys API.
Once the software is installed and the export file is imported into Insomnia, you just need to change the environment variables once to change the URL of your Gladys server.

Then you just need to log in via Insomnia (User → login) with the credentials/password of your Gladys admin to retrieve an access token (access_token) that you can go modify in the environment variables again.

This access_token will be useful once logged in via Python to make your REST calls.

ok thanks I’ll look into that (I’ll look into gotify because I don’t know it at all)

Apologies, I meant to type Gladys and not Gotify. I’m working on a possible integration of this Gotify service, I must be a bit conditioned by it :wink:

Hello @Ook! For now, the Gladys 4 REST API is more for « internal Â» use within Gladys. There is not yet a long-term token mechanism to use the API externally (with an API key).

This is, however, a planned development, which is listed here:

You can vote for the feature if you want to see it in Gladys :slight_smile:

Out of curiosity, why would you like to use the API externally?

In the meantime, two possibilities

1) The MQTT API

If you want to send new device values, you can use the Gladys MQTT API (more explanations in the documentation), which is the cleanest option, in my opinion :slight_smile:

2) Use a « user Â» token as if you were the frontend

If you really want to use the API in a script before API tokens are available, you can use the login flow in your script to retrieve an access_token and a refresh_token, then call the Gladys API with an access_token in the « Authorization Â» header.

It’s less clean than using future API keys, but it gets the job done in the meantime :slight_smile:

My goal is twofold: I’m working on a voice recognition and synthesis script, and I wanted to run a scenario (basically turn everything off when leaving).

Secondly, for educational purposes, as I’m learning Python mostly on my own and have already managed to consume a REST API from a software at my job, and I want to try with another one to get used to this technology.

So I think the front-end equivalent would suit me.

Otherwise, I’ll tinker with MQTT if I can send a presence departure indicator… Thanks for the info!

Top :slight_smile: Indeed, the frontend option is the best for you.

What I recommend to help you understand how it works is to open your browser’s inspector « Request Â» tab while on Gladys’ frontend.

You will see all the requests passing by, and you can do the same in your Python script.

Don’t hesitate if you have any questions :slight_smile:

I did it! Thanks for the advice! My voice recognition can now launch scenarios! So happy! Now I just need to try it on the Raspberry!

Great! :clap::clap: Out of curiosity, what does your script actually do?

Don’t hesitate to share your setup with the community :slightly_smiling_face:

This is a Python script that listens to a microphone and performs voice recognition on a keyword. (pocketsphynx module)

So far, it has launched my applications on demand, and now it executes a scene in Gladys. (turning off the lights)

For the moment, it’s on my Windows 10 Pro PC, I need to redo it for my Raspberry Pi. That will be the next step!

Hello, I’m following up on the topic to ask a question I haven’t found the answer to. Is it possible to retrieve electricity consumption information from a smart plug via the API, or is this not possible? Thanks in advance.

Hello @desdamo, do you want the instant consumption or consumption history?

Be careful, there is no API key mechanism in Gladys 4 yet (planned but still under development), so the API can only be consumed « as if your script is the frontend Â».

If you want instant (and if your outlet reports consumption information of course), you can access the consumption via this GET I think: Gladys Assistant REST API documentation

Hello @pierre-gilles, I want to retrieve via a front-end the consumption of several connected outlets. No snapshot as I want to know the average consumption for the week or day. Is this possible?

@desdamo currently not, but we have a PR that goes in this direction (https://github.com/GladysAssistant/Gladys/pull/966)