Need help with REST API

Forgive me if this is posted in the wrong place … but I just realized all my Webcore stopped working … I have a few devices that rely on my SmartThings Hub and I had a REST API set up to talk to it from my system (OpenHab) … well now I am trying to get the new REST API working and having some trouble … not too much out there on the community I could find and the ST documentation is so thoroughly complete it would take days to absorb.

Basically I can now do the following:

  1. curl -X GET -H "Authorization: Bearer a0b4d89c-d7fa-48f3-xxxxx" -H "Content-Type: application/json" -d '{"command":"on"}' ""

This returns the device status which is:

{"deviceId":"91cc9707-7ab3-4328-a664-218210e71bc5","name":"eWeLink Outlet","label":"FrontRoom_Light","manufacturerName":"SmartThingsCommunity","presentationId":"dc51981a-71db-3f67-a30b-150d778cd741","deviceManufacturerCode":"eWeLink","locationId":"7dc0f22b-0a5c-4520-9ffb-7db6178887d8","roomId":"e78546b5-ba90-4514-ac03-61df7cb18634","components":[{"id":"main","label":"FrontRoom_Light","capabilities":[{"id":"switch","version":1},{"id":"firmwareUpdate","version":1},{"id":"refresh","version":1}],"categories":[{"name":"Switch","categoryType":"manufacturer"}]}],"createTime":"2021-10-17T15:09:54.085Z","parentDeviceId":"483b14be-ef93-4877-82b9-37b227fcce60","profile":{"id":"db6161b1-d26d-3a78-acea-c09dec91c31c"},"zigbee":{"eui":"00124B00226A6311","networkId":"7E53","driverId":"f2e891c6-00cc-446c-9192-8ebda63d9898","executingLocally":true,"hubId":"483b14be-ef93-4877-82b9-37b227fcce60","provisioningState":"PROVISIONED"},"type":"ZIGBEE","restrictionTier":0,"allowed":null}

Now, I would like to send it a command and learned (probably incorrectly) from ChatGPT that I should use:

curl -X PUT -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{"command":"on"}' "{deviceId}/commands"

Well this doesn’t work, and various attempts have failed as well. I get back a:

{"requestId":"1D44CAC2-5BAC-4E18-BAAB-5D89E1A32EC2","error":{"code":"ConstraintViolationError","message":"The request is malformed.","details":[{"code":"SizeError","target":"commands","message":"commands must have a size between 1 and 20.","details":[]}]}}

The “commands must have a size between 1 and 20.” doesn’t make a a lot of sense to me.

Can anyone direct me to a simple example of turning ON and OFF a switch through a cURL?


Well, after a little more digging … I made it work. Here’s what I did:

Instead of using -d '{"command":"on"}'

I used:

-d '{"commands":[{"component":"main","capability":"switch","command":"off"}]}'

curl -X POST -H "Authorization: Bearer a0b4d89c-d7fa-48f3-a3a6-xxxx" -H "Content-Type: application/json" -d '{"commands":[{"component":"main","capability":"switch","command":"off"}]}' ""

Not really understanding why but it’s working :slight_smile:

1 Like

Yes, you have an array of commands because you might want to send more than one at a time. For each command you need to specify the component if it isn’t “main”, and also what capability the command is for.

1 Like

For more follow up on another device … I have a Meross Color Strip … I found that these cURLs will allow setting of the various color attributes:

curl -X POST -H "Authorization: Bearer a0b4d89c-d7fa-48f3-a3a6-xxxx" -H "Content-Type: application/json" -d '{"commands":[{"component":"main","capability":"colorControl","command":"setColor","arguments":[{"saturation": 100, "hue":70}]}]}' ""

curl -X POST -H "Authorization: Bearer a0b4d89c-d7fa-48f3-a3a6-xxxx" -H "Content-Type: application/json" -d '{"commands":[{"component":"main","capability":"switch","command":"on"}]}' ""

curl -X POST -H "Authorization: Bearer a0b4d89c-d7fa-48f3-a3a6-xxxx" -H "Content-Type: application/json" -d '{"commands":[{"component":"main","capability":"switchLevel","command":"setLevel","arguments":[77]}]}' ""

curl -X POST -H "Authorization: Bearer a0b4d89c-d7fa-48f3-a3a6-xxxx" -H "Content-Type: application/json" -d '{"commands":[{"component":"main","capability":"colorTemperature","command":"setColorTemperature","arguments":[2700]}]}' ""

These all work on my set up.