Rest Call against SmartThings to turn on switch

So, I’m just trying to turn a virtual switch on/off via an HTTP rest call. I don’t know why this is so frickin’ difficult, and I can’t find a clear code example. I’ve been using PostMan and going with SmartThings documentation and just keep getting request malformed errors. I’m really hoping someone can just show me a working example URL call so I can alter it for my needs.

https://api.smartthings.com/v1/devices/deviceIDhere/commands?component=“main”&capability=“switch”&command=“on”

Now I noticed this doesn’t have the bearer token in it, not sure how you include the bearer token in the URL as PostMan doesn’t show me that part.

Really hoping someone can help me out on this, it’s the last major roadblock I have to my home theater automation setup. Thanks in advance!

Hello @Matthew_Freestone,

You can see this document to check the request structure to execute a command. If you are using postman, you need to configure the Bearer Token in the “authorization” tab and the command in the “body” tab in JSON format (see the pictures below).
The URL is:
https://api.smartthings.com/v1/devices/{deviceId}/commands

2 Likes

I don’t want to use postman for it, I want just the entire URL format example.

Hello @Matthew_Freestone,

You need to use the complete POST request structure (URL, header, and body). Here is an example using axios:

let data = JSON.stringify({
        "commands": [
          {
            "component": "main",
            "capability": "switch",
            "command": "on",
            "arguments": []
          }
        ]
    });

    let requestConfig = {
        method: 'post',
        url: 'https://api.smartthings.com/v1/devices/deviceID/commands',
        headers: { 
          'Authorization': 'Bearer xxx-xxxx-xxxx', 
          'Content-Type': 'application/json'
        },
        data : data
    };
    
    axios(requestConfig).then(response => {
        console.log(JSON.stringify(response.data));
    }).catch(error =>{
        console.log(error);
    });

If this answers your question, can you mark it as Solved, please? If not, let me know what’s going on and we can dig in further.

This doesn’t exist. You’re wanting a single URL which includes the API endpoint, the authorization, and payload. Can you elaborate on how you intend to use the URL?

I am just simply trying to get a piston to execute via a URL commend submitted by my INSTEON system. I found out I can do this via webCore, so I’m good doing it that way.

1 Like