Connecting to APIs from Spotfire

Just wanted to share this development journey with you trying to build a template. I wanted to create a WordPress connector for DataShopTalk so that I could download all the articles and get a good idea of the content of the blog. Connecting directly to the MySQL database was not really ideal, I wanted to setup an API from WordPress so that a data function could connect to it.

Here’s a high-level list of the things we need:

  • A REST API opened up on that spits out JSONs.
  • A data function that can send authenticated GET requests to the REST API and can parse JSON content.
  • A Spotfire file which hosts the data function and the inputs for credentials


First I needed an exposed API on the WordPress site. I setup the plugin WP REST API so that I could get JSON results back from an authenticated query. Here is the developer handbook for connecting to the interface:

Today we’re only going to grab a list of posts from the API. Theoretically, this API is two-directional, so you could post to WordPress using Spotfire. However let’s look at the posts endpoint:

$ curl -X OPTIONS -i

This is what we are given to connect to the API to retrieve a list of posts. This will be enough to get a list and then we can find out the author, the status, and the date. Here are the objects we’ll be interested in:

string, datetime (ISO8601)
The date the object was published, in the site’s timezone.
Context: vieweditembed
A named status for the object.
Context: edit
One of: publishfuturedraftpendingprivate
The title for the object.
Context: vieweditembed
The ID for the author of the object.
Context: vieweditembed


There is plenty more to explore in that schema so take a look yourself.

Data Function

First, the script:

fullDomain <- paste("http://",dfDomain,"/wp-json/wp/v2/posts",sep="")
fullCreds <- paste("Basic",dfCreds,sep="")
r <- GET(fullDomain, add_headers(Authentication = fullCreds,per_page = 200))

document <- fromJSON(content(r,"text"))
result <- cbind(title=document$title$rendered, date=document$date, author=document$author, status=document$status)

Ok, so here we are importing httr. We are going to use httr to make the request to the server. We have one hitch though, we need to be able for users in Spotfire to input their credentials so they can log into the server. Furthermore, using httr with Basic Authentication requires you to encode your credentials in Base64. To get this to work we need to pass it something that looks like this: “BasicYWRtaW46dGVzdHRlc3Q=” which is an encoded version of “username:password”. Here is an example of the inputs in Spotfire:

To encode the credentials in Base64 was a doozy. I couldn’t figure out how to make R’s base64enc package do it. That meant we would have to encode the credentials before the data function, we’ll have to do that in Spotfire.


First I tried using IronPython, because the document properties have an OnChange functionality that I could leverage. (Everytime the username or password input was changed, the base64 encoded credentials would update). To my chagrin, the package for base64 in Python was not available. The last resort was JavaScript which had a Unicode to Base64 method available.

Here is the full HTML for the configuration text box in Spotfire:


$( "#01156c1564f940f796fbd615907385b2" ).keyup(encodeCreds);

$( "#38a171fba4c9495bafb8f14bee175437" ).keyup(encodeCreds);

function encodeCreds() {

dfUserName = $( "#01156c1564f940f796fbd615907385b2" ).val()

dfPassword = $( "#38a171fba4c9495bafb8f14bee175437" ).val()

value = dfUserName + ":" + dfPassword





<h2>Wordpress Connector</h2>

<p>This data function uses the wp-json plugin for WordPress to connect to posts and other objects on the WordPress platform. Use the document properties below to connect to your WordPress instance.</p>

<p>WP REST API Documentation: <a href=""></a>.</p>


<ul><li>R package: httr</li><li>R package: jsonlite</li></ul>

<p>You can install these from the menu above via Tools > TERR Tools > Package Management.</p>


<p><b>Wordpress domain:</b><br><i>http://<SpotfireControl id="6e874f0b35a1414a91d5f511ea50a912" />/wp-json/wp/v2/posts</i></p>

<p><b>Username:</b> <SpotfireControl id="01156c1564f940f796fbd615907385b2" /></p>

<p><b>Password:</b> <SpotfireControl id="38a171fba4c9495bafb8f14bee175437" /></p>

<p><b>Encoded credentials:</b><br>

<SpotfireControl id="e330bf78c7854461be588606939b26e3" /></p>
<pre><p><SpotfireControl id="9147430e218540088d0e588c778e5fae" /></p>

Let me know in the comments if you have questions! Happy to elaborate on any of the components.

Technical Director at

8 thoughts on “Connecting to APIs from Spotfire

  1. Hi – loved the way you broke it down!
    I have a doubt – Spotfire and coding newbie 🙂

    There’s an application that has a API developed.
    It captures the required data from its database and sends it out at intervals.

    My doubt is 2 fold
    1. I have a Spotfire Analysis that needs the info from the above API. What is the best approach for this?
    2. Is there a method in Spotfire that allows only “Incremental” or newer info to be updated?


  2. Hi V, I would start by first trying the script in RStudio and first get the authentication setup. At minimum you will want to be able to get a response back from the server.

    I guarantee that half of the development time is just getting authenticated, once there you are set to consume from json. A great practice for consuming from the JSON would be to try it out in the browser first. Then use the browser developer console to work your way through the json structure. This will help you understand what to grab from your TERR script.

    As for the second question, you can set the data function output to “Add Rows” but that means that you will need to have an additional property that would save the last time that you ran the script.

  3. Hi Lucas I have gotten my script to work in R studio but having problems in TERR and was wondering if you ran into anything similar.

    httr::GET function is giving me this hassle:

    TIBCO Enterprise Runtime for R returned an error: ‘Error in default_ua() : $ operator is invalid for atomic vectors’.

    any thoughts?

Leave a Comment

Your email address will not be published. Required fields are marked *