Using ThoughtSpot's REST API from Python

ThoughtSpot's documentation at docs.thoughtspot.com lists JavaScript as a method of calling the REST API. In this article, we take a look at how to invoke the REST APIs from Python.

Why Python

Personally, I prefer Python to JavaScript as a programming language. Python is also better suited to creating helper utilities.

Requirements

To follow along and code, you need the following:

import requests

What are the REST endpoints available?

ThoughtSpot REST endpoints vary by release, and are documented at https://docs.thoughtspot.com/5.2/app-integrate/reference/public-api-reference.html

The endpoints of your existing installation can be accessed by going to your browser (Chrome, Firefox) :

<tspath>/external/swagger/

 

(assuming <tspath> is an example installation like

Creating your connection: Login and State

ThoughtSpot has APIs for login and logout as documented at https://docs.thoughtspot.com/5.2/app-integrate/reference/session-api.html

Here is how you use it

        username = 'me'
        password = 'mysecurepassword'
        login = requests.post(self.thoughtspot_host + "/callosum/v1/session/login",
                              data={'username':username,
                                'password':password,
                                'rememberme':True},
                              headers={'Accept': 'application/json', 'X-Requested-By': 'ThoughtSpot'}
                              )

If the login is successful, we can store the returned state into a variable called cookie_jar.

        if login.status_code == 200:
            print("Login Success")
            cookie_jar = login.cookies

Invoking other APIs

Post requests to other APIs can now use this cookie information.

  • API to create user
        create_data = {}
        create_data['name'] = 'anotheruser'
        create_data['password'] = 'anotherusersecurepassword'
        create_data['displayname'] = 'Another User'
        create_data['groups'] = 'GROUP101'
        create_data['usertype'] = 'LOCAL_USER'
        create_data['tenant_id'] = '1'
        create_data['visibility'] = 'DEFAULT'

        createuser = requests.post(self.thoughtspot_host + "/callosum/v1/session/user/create",
                              data=create_data,
                              headers={'Accept': 'application/json', 'X-Requested-By': 'ThoughtSpot',
                                       'Content-Type':'application/x-www-form-urlencoded'},
                              cookies=cookie_jar
                              )

Check the return status code of the request, 200 indicates success.

  • API to get pinboard data

Using the example from https://docs.thoughtspot.com/5.2/app-integrate/data-api/use-data-api-read.html, we can get data for a pinboard id and visualization by giving the IDs

 

        id = "05a69dc0-c557-4a8d-9bf1-24e1138e2747"
        vizid = "9c6b98e0-beac-4be5-b30e-68d18cc1a8a1"
        pin_data = requests.post(self.thoughtspot_host + '/callosum/v1/tspublic/v1/pinboarddata?id=' + id + '&vizid=[' + vizid + ']&batchsize=-1&pagenumber=-1&offset=-1&formattype=COMPACT',
                               headers={'Accept': 'application/json', 'X-Requested-By': 'ThoughtSpot',
                              'Content-Type':'application/x-www-form-urlencoded'},
                               cookies=cookie_jar
                               )

If the requests succeeds, you can get the JSON data.

        if pin_data.status_code == 200:
            print("get pin data Success")
            print(pin_data.text)

Conclusion

Hope this helps all the developers/power users trying to use the REST API, and allow them to use the power of the REST APIs from Python.

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like Follow
  • 2 wk agoLast active
  • 33Views
  • 1 Following