Data api in javascript


(Improviz) #1

I would like to use the data api with javascript, but I am having a hard time figuring out how to build the request and send it to the proper api. Im sure I can figure out what to do with the response JSON that comes back. A simple example that comes to mind is how to get my collections. https://docs.sketchfab.com/data-api/v3/index.html#!/collections/get_v3_me_collections

Is there someone in the community that can provide an example of how to accomplish this?

Thanks!


(Shaderbytes) #2

like this :

function getCollections(){
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();    
    xmlhttp.open("GET", "https://api.sketchfab.com/v3/me/collections" , false);
    xmlhttp.setRequestHeader( "Authorization", "Token yourtokenvalue")
    xmlhttp.send(null);
     console.log(xmlhttp.responseText);

}
}
getCollections();

You can get your token from your profile settings. Also this is a simple copy paste edit of some online code , the false value in the open method should be true instead (Synchronous call) and you should use an eventlistener on the XMLHttpRequest instance. You can see a sample of this listener on wikipedia or w3schools or all over the web :slight_smile:

https://www.w3schools.com/js/js_json_http.asp


(Improviz) #3

Thanks for the quick response @shaderbytes. I will give this a shot.


#4

If you're using jQuery, it's even easier (although it's really just doing the same thing)

https://api.jquery.com/jquery.get/