Skip to content

Instantly share code, notes, and snippets.

@HarlemSquirrel
Last active May 1, 2022 12:44
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save HarlemSquirrel/0be679d756b1391a61919f2b03699201 to your computer and use it in GitHub Desktop.
Thingiverse API Examples using JavaScript
// 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