Search code examples
javascriptapithemestumblr

Call Tumblr API from within a custom theme


I'm writing a custom tumblr theme for a client who needs to use the platform.

The functionality required goes beyond that offered by the Custom Theme tags, so I need to incorporate API calls to add an improved experience.

I'm a fairly experienced developer but totally new to Tumblr. Is there a way I can make calls to their API from within my theme without requiring OAuth callbacks?

Currently my theme comprises of a HTML file and an external JS file that does a whole bunch of other things (Non-Ajax type stuff).

Ideally somewhat like the following:

  request = new XMLHttpRequest();
  request.open('GET', 'http://api.tumblr.com/v2/blog/fdsfdsf.tumblr.com/posts', true);
  request.onerror = _handleError;
  request.onload = _handleResponse;
  request.send();

Solution

  • Helped partially by @Adam's response. My final solution was to create an app here tumblr.com/oauth/apps and then use my consumer key in my API requests as such:

    http://api.tumblr.com/v2/blog/<MY_BLOG>/posts/photo?api_key=<MY_KEY>&jsonp=callback" and use JSONP to handle the callback function.

    My final code:

    var apiKey = "gdfgdfgdfgdsfgdfhgsdfghsfdh";
    var requestURL = "http://api.tumblr.com/v2/blog/dfsdf.tumblr.com/posts/photo?api_key=" + apiKey + "&jsonp=callback";
    
    var script = document.createElement('script');
    script.src = requestURL;
    
    document.getElementsByTagName('head')[0].appendChild(script);
    // or document.head.appendChild(script) in modern browsers
    
    window.callback = function (data) {
        console.log(data);
    }