-
Notifications
You must be signed in to change notification settings - Fork 639
RESTAPI
The ESPurna firmware exposes a REST API you can use to query or change relay statuses and query sensor values.
The REST API is disabled by default. You will have to enable it first from the web interface in the "Security" tab. You will also have to take note of the API KEY you must use in every query to the REST API. This key is autogenerated or you can generate a new one click the "Generate" button.
To test the API you can use a normal browser (for GET requests), a GUI tool like Postman for Google Chrome or cURL from the terminal in Linux and MacOS machines.
cURL is used in the examples for simplicity, I hope everyone will get the idea.
The entry points are the URLs you can get or send values from/to. The represent physical entities (like relays) or magnitudes (like "temperature"). The available entry points will depend on the sensors you have enabled in your device.
To know what API entry points you can use there is a special URL that summarizes them:
$ curl http://192.168.1.108/apis?apikey=C62ED7BE7593B658
relay0 -> /api/relay/0
relay1 -> /api/relay/1
temperature -> /api/temperature
humidity -> /api/humidity
You can also enter this URL in the web browser (http://192.168.1.108/apis?apikey=C62ED7BE7593B658).
Also, you can specify you want the output in JSON format (to use it from a web app, for instance):
$ curl -H "Accept: application/json" http://192.168.1.108/apis?apikey=C62ED7BE7593B658
{ "relay0":"/api/relay/0","temperature":"/api/temperature","humidity":"/api/humidity"}
You can query or set the relay status using the REST API. To query the relay status you have to issue a GET request like the previous one to the relay entry point:
$ curl -H "Accept: application/json" http://192.168.1.108/api/relay/0?apikey=C62ED7BE7593B658
{ "relay0": 1}
To change the relay status you have to send a PUT request with the new value in the "value" parameter. This new value can be 0 to turn it off, 1 to turn it on or 2 to toggle it:
$ curl -X PUT -H "Accept: application/json" http://192.168.1.108/api/relay/0 --data "apikey=C62ED7BE7593B658&value=2"
{ "relay0": 0}
$ curl -X PUT -H "Accept: application/json" http://192.168.1.108/api/relay/0 --data "apikey=C62ED7BE7593B658&value=2"
{ "relay0": 1}
Since version 1.6.4 you can also change the relay value via a GET request.
$ curl "http://192.168.1.108/api/relay/0?apikey=C62ED7BE7593B658&value=2"
relay0 => 0
$ curl "http://192.168.1.108/api/relay/0?apikey=C62ED7BE7593B658&value=2"
relay0 => 1
From version 1.7.0 if your device has an RGB light source you can control the color via API too (both GET and PUT, "%23" is the URL encoded value for '#'):
$ curl -X PUT -H "Accept: application/json" http://192.168.1.108/api/color --data "apikey=C62ED7BE7593B658&value=%23FF0000"
{ "color": "#FF0000" }
$ curl "http://192.168.1.108/api/color?apikey=C62ED7BE7593B658&value=%23FF0000"
#FF0000
Sensor values can be queried in the same fashion using GET requests:
$ curl -H "Accept: application/json" http://192.168.1.108/api/temperature?apikey=C62ED7BE7593B658
{ "temperature": 21.5 }
Postman is an App for Google Chrome to test and develop APIs. You can download it and install it from the App Store in your Chrome browser. Here you have a couple of screenshot of Postman querying the ESPurna API.
If you're looking for support:
- Issues: this is the most dynamic channel at the moment, you might find an answer to your question by searching open or closed issues.
- Wiki pages: might not be as up-to-date as we all would like (hey, you can also contribute in the documentation!).
- Gitter channel: you have better chances to get fast answers from project contributors or other ESPurna users. (also available with any Matrix client!)
- Issue a question: as a last resort, you can open new question issue on GitHub. Just remember: the more info you provide the more chances you'll have to get an accurate answer.
- Backup the stock firmware
- Flash a pre-built binary image
- Flash a virgin Itead Sonoff device without opening
- Flash TUYA-based device without opening
- Flash Shelly device without opening
- Using PlatformIO
- from Visual Studio Code
- Using Arduino IDE
- Build the Web Interface
- Over-the-air updates
- Two-step updates
- ESPurna OTA Manager
- NoFUSS
- Troubleshooting
- MQTT
- REST API
- Domoticz
- Home Assistant
- InfluxDB
- Prometheus metrics
- Thingspeak
- Alexa
- Google Home
- Architecture
- 3rd Party Plugins
- Coding style
- Pull Requests