Last active
May 1, 2022 12:44
Star
You must be signed in to star a gist
Thingiverse API Examples using JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.thingiverse.com/developers/rest-api-reference | |
/******************************************************************************* | |
* The headers needed for authenticating with the API. | |
* Get the SECRET_TOKEN using the browser tools of Firefox or Chrome: | |
* 1. Open the browser tools with F12 or ctrl+shift+i | |
* 2. Navigate to the Network tab | |
* 3. Reload a Thingiverse page | |
* 4. Locate a and select a request to api.thingiverse.com | |
* 5. Find the request headers and copy the token next to Authorization: Bearer | |
* 6. Replace SECRET_TOKEN below with that token. | |
********************************************************************************/ | |
var headers = { | |
"Authorization": "Bearer SECRET_TOKEN" | |
} | |
/************************** | |
* List user collections | |
*************************/ | |
async function getCollections(username) { | |
const response = await fetch( | |
"https://api.thingiverse.com/users/" + username + "/collections/", | |
{ | |
"credentials": "same-origin", | |
"headers": headers, | |
"method": "GET", | |
"mode": "cors" | |
} | |
) | |
return response.json() | |
} | |
// Example listing user collections | |
getCollections('harlemsquirrel') | |
.then(collections => { | |
collections.forEach(collection => { | |
console.log(collection.name + ': ' + collection.id) | |
}); | |
}) | |
/******************************* | |
* Add a thing to a collection | |
******************************/ | |
async function addThingToCollection(collectionID, thingID) { | |
const response = await fetch( | |
"https://api.thingiverse.com/collections/" + collectionID + "/thing/" + thingID, | |
{ | |
"credentials": "same-origin", | |
"headers": headers, | |
"method": "POST", | |
"mode": "cors" | |
}); | |
return response.json() | |
} | |
// Example adding thing to collection | |
var deskCollectionID = 8800327 | |
addThingToCollection(deskCollectionID, 607518) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment