Skip to main content
5-Regular Member
August 20, 2015
Question

can someone point me to a sample python script that sets thingworx properties (through Rest API?)

  • August 20, 2015
  • 1 reply
  • 6462 views

can someone point me to a sample python script that sets thingworx properties (through Rest API?)

 

I am a thingworx starter and looking for some reference material on how to use python to communicate with thingworx server to get me started.

1 reply

5-Regular Member
August 21, 2015

Hi Bhaskar,

The Requests HTTP library for Python makes interacting with the ThingWorx API a breeze. The following sample code uses this library to set two Properties (stringProperty and numberProperty) defined on a Thing:


import requests

import json

url = 'https://localhost/Thingworx'

headers = { 'Content-Type': 'application/json', 'appKey': 'ed18a4a1-c6d5-46b3-ace8-b55d148e2384' }

payload = { 'stringProperty': 'ThingWorx', 'numberProperty': 14.3 }

response = requests.put(url + '/Things/ExampleThing/Properties/*', headers=headers, json=payload, verify=False)

Since Tomcat is configured with a self-signed certificate, False must be passed as an argument to the verify parameter. In a production environment, you will want to verify the SSL certificate.

You can find information about the ThingWorx API here.

bwalke5-Regular MemberAuthor
5-Regular Member
August 21, 2015

Thanks a LOT Adam for the detailed reply.

Can you please also provide information on how to “get” thingworx property value using python?

Thanks in advance,

Bhaskar Walke

5-Regular Member
August 21, 2015

You're welcome : )

To retrieve a Property value, you need to modify the headers:

headers = { 'Accept': 'application/json', 'appKey': 'ed18a4a1-c6d5-46b3-ace8-b55d148e2384' }

and use the method that corresponds to a GET:

response = requests.get(url + '/Things/ExampleThing/Properties/stringProperty', headers=headers, verify=False)