Topics: 5 min readtime

How I learned to automate Tableau Server tasks with the Rest API

Written by Viktor van Beersel
Tuesday 9 February, 2021

REST API: Tableau Server Task Automation


Hi! I’m Viktor, Analytics Consultant and Team Lead at Biztory. In this blog post, I’ll tell you about my experiences with learning to work with Tableau’s Rest API. So rather than explaining every technical detail of what I’ve learnt, I’ll walk you through my learning path and what knowledge I ended up with. Hopefully, you’ll pick up a few things from my journey that’ll come to use in your automation use cases.

What is there to automate?

Some of you might wonder: "What tasks can we actually automate in Tableau Server?” Well, if you’re familiar with Tableau Server, basically all of the tasks done through the User Interface of Tableau Server can be performed programmatically through the Rest API. Creating sites and projects, adding users to your site(s), adding users to a subscription schedule, etc. All of this (and much more) can be done through Tableau’s Rest API, the complete list can be found on Tableau Help.

 

Why Rest API?

You might ask, why bother writing scripts for these tasks if you can do them through Tableau Server’s UI? Well, imagine you have to set up Tableau Server with numerous sites and about 10-50 projects to add to those sites, as well as hundreds of users to onboard and to add to user groups. I wouldn’t be too excited if my boss asked me to do all of this manually through the UI.

The best part is you don’t have to be a coding genius to use this technique. Combining Tableau’s Rest API with some Excel logic and the use of Google will get you quite far, really.

 

What I knew before using the Rest API

Before getting to know Tableau’s Rest API, the automation tasks were all done through tabcmd. Tabcmd is Tableau’s native command line utility that allows you to perform a number of administrative tasks on Tableau Server. I used tabcmd in combination with Excel and Powershell, which worked quite well for basic administrative tasks, like adding users to a server or site, creating groups, creating extract refreshes, downloading reports as pdf or csv files, etc.

Tabcmd is certainly useful, but it doesn’t allow you to perform interactive administration tasks on a granular level like the Rest API does. With the Rest API, you can write blocks of code that will be used subsequently in order to operate more complex tasks. For example, you could add blocks of users to a server and site based on an Excel spreadsheet, which will afterwards be added to a user group you just created on Tableau Server that will be granted specific permissions to a certain project.

These kinds of granular tasks are certainly possible with the Rest API, whereas tabcmd can’t be used for that, really. Another downside to tabcmd is that it needs to be installed and it’s only available for Windows and Linux. Tableau’s Rest API, unlike tabcmd, is standardized and is easily understood by developers.

 

Getting started with the Rest API

Now off to the challenge, i.e. starting to use the Rest API. There is a lot of documentation to be found on Tableau’s official help page. However, if you’ve never worked with tools like Postman and have never heard of HTTP verbs (PUT, GET, POST,...) and URIs, the documentation is quite difficult to understand. However, there’s a great alternative to use if you’re familiar with Tableau Server’s main functionalities and you have basic programming language knowledge: Tableau Server Client.

Tableau Server Client is a Python library created by Tableau to use the Rest API which is fairly simple to learn to work with. The only prerequisite is having Python installed on your machine (and a Tableau Server instance, obviously). After installing Tableau Server Client through pipinstall, you can start playing around with the API calls.

For a Python rookie like myself, it took me approximately five hours to get used to the programming structure, the sequence of tasks to perform should be clear if you have a clear understanding of how Tableau Server works. Once I got a hold on the basics of the library pandas in combination with Excel and Tableau Server Client, it took me only minutes to learn the different resources, i.e. projects, users, sites, etc.

To give you an idea of the simplicity of it all, a little piece of the code I’ve used is shown below:

import tableauserverclient as tsc
import pandas
from pandas import ExcelFile

excel = "C:/Users/.../ImportFile.xlsx"

tableau_auth = tsc.TableauAuth(username='viktorvanbeersel',password='***')
server = tsc.Server('http://yourserver.com')

#create site


with server.auth.sign_in(tableau_auth):


#new site item

site_item = tsc.SiteItem(name='HR_Analytics',content_url='hr', storage_quota=50, subscribe_others_enabled=True, revision_history_enabled=True)
server.sites.create(site_item)

#create projects


df_int_analytics = pandas.read_excel(excel,sheet_name='analytics')
projectname = df_int_analytics['ProjectName']
publish = df_int_analytics['PublishValue']

with server.auth.sign_in(tableau_auth):

i=0
while i < df_int_analytics.shape[0]:

new_project = tsc.ProjectItem(name=projectname[i], content_permissions=publish[i])


# create the project


new_project = server.projects.create(new_project)
i += 1

 

In the example above, after importing the necessary packages, we firstly define the path to our Tableau Server and the credentials, using the Server and TableauAuth function. Afterwards, a site called HR Analytics is created.

The fun part happens with creating the projects in bulk: firstly, we read the correct sheet from the Excel file and assign a project name and publish value via the Excel sheet. Next, after signing in to Tableau Server, we go through each row in the Excel sheet and create a project with the name and publish value specified in that row. After creating the project, the index is incremented, and as long as it’s smaller or equal to the total number of rows in the Excel sheet, the loop will keep running. Running this script with 100 projects finishes in less than 30 seconds. How long would it take you to do this manually? Hah!

To wrap up, I can only say I’ve learned a bunch of stuff while playing around with Tableau Server Client, that is Python in combination with Tableau’s Rest API. So, if you have deep interests in Tableau Server and automation, seek no further and go wild with Tableau Server Client!

 

- Viktor