Discuss Scratch
- TheGamer-
-
Scratcher
100+ posts
New(er) API documentation
I've been trying to find some documentation for the new(er) api (api.scratch…), but what I could find didn't have many of the features of the old one shown (ie posting comments, following etc). Is there a topic for this somewhere or have they just not been migrated (yet?). Sorry if I've just managed to miss it somehow
- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
@_nix Is there any way to read/write data to/from the backpack?
- blob8108
-
Scratcher
1000+ posts
New(er) API documentation
Do the new APIs have CORS? The scratchblocks generator is sad 

- herohamp
-
Scratcher
1000+ posts
New(er) API documentation
Do the new APIs have CORS? The scratchblocks generator is sadJust self host crossorigin.me, or use http://crossorigin.mooredrive.pw/
- jokebookservice1
-
Scratcher
1000+ posts
New(er) API documentation
@_nix Is there any way to read/write data to/from the backpack?Not with the new API, I don't think… but you can use the old API.
GET https://scratch.mit.edu/internalapi/USERNAME/get/
with session cookie
and
POST https://scratch.mit.edu/internalapi/USERNAME/set/
with X-CSRFToken and session cookie and JSON payload as body
- blob8108
-
Scratcher
1000+ posts
New(er) API documentation
Just self host crossorigin.meThanks, but that didn’t answer my question. I am aware of crossorigin.me

- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
@_nix Is there any way to read/write data to/from the backpack?Not with the new API, I don't think… but you can use the old API.GET https://scratch.mit.edu/internalapi/USERNAME/get/
with session cookie
andPOST https://scratch.mit.edu/internalapi/USERNAME/set/
with X-CSRFToken and session cookie and JSON payload as body
I cant seem to get this to work. How could i accomplish this with javascript?
Last edited by NitroCipher (Dec. 11, 2017 20:48:02)
- jokebookservice1
-
Scratcher
1000+ posts
New(er) API documentation
Which one are you using, get or set? What code are you currently trying? Where are you running the code?
- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
Which one are you using, get or set? What code are you currently trying? Where are you running the code?
alert("test"); $.get("https://scratch.mit.edu/internalapi/NitroCipher/get/", function(data, status){ alert("Data: " + data + "\nStatus: " + status); }); alert("test2");
I have a button event that triggers this in a userscript. Both alerts surrounding the get function properly.
- jokebookservice1
-
Scratcher
1000+ posts
New(er) API documentation
Excuse me, that was a mistake on my part
The get request is actually
The get request is actually
GET https://scratch.mit.edu/internalapi/backpack/NitroCipher/get/
- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
Excuse me, that was a mistake on my part
The get request is actuallyGET https://scratch.mit.edu/internalapi/backpack/NitroCipher/get/
How would I add this:
{"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]}
- jokebookservice1
-
Scratcher
1000+ posts
New(er) API documentation
Okay, so you'll want to extract the CSRF middleware token. You can find it in document.cookie, and do some RegExes or something to extract it.
The body you will want is your JSON.
You will need to add this header
And that should work.
POST https://scratch.mit.edu/internalapi/backpack/NitroCipher/set/
The body you will want is your JSON.
You will need to add this header
X-CSRFToken: TOKENHERE
And that should work.
- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
Okay, so you'll want to extract the CSRF middleware token. You can find it in document.cookie, and do some RegExes or something to extract it.POST https://scratch.mit.edu/internalapi/backpack/NitroCipher/set/
The body you will want is your JSON.
You will need to add this headerX-CSRFToken: TOKENHERE
And that should work.
How would all of this look in JS? (I am a noob dev)
- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
How would all of this look in JS? (I am a noob dev)
Halp
- jokebookservice1
-
Scratcher
1000+ posts
New(er) API documentation
Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.
I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js
$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js
- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})
I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Will this add it to the backpack? Or replace the current backpack data?
- MathWizz
-
Scratcher
100+ posts
New(er) API documentation
Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.Uh, it shouldn't be sending a request to the CDN server. I have no idea how that happened. It works though so I'm not gonna change it right now.$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})
I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js

Will this add it to the backpack? Or replace the current backpack data?It will replace the entire backpack.
- NitroCipher
-
Scratcher
500+ posts
New(er) API documentation
Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.Uh, it shouldn't be sending a request to the CDN server. I have no idea how that happened. It works though so I'm not gonna change it right now.$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})
I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.jsWill this add it to the backpack? Or replace the current backpack data?It will replace the entire backpack.
I can't seem to get it to post
- jokebookservice1
-
Scratcher
1000+ posts
New(er) API documentation
Whoops! You need to stringify an array containing the backpack, I think; and not the individual item. Perhaps that's the reason why it's not working?
However, I'm interested what you mean by “won't post”. Do you mean it returns a status code other than 200; or literally a POST request isn't being made. If the latter, is another verb being used, or is no request being sent at all?
However, I'm interested what you mean by “won't post”. Do you mean it returns a status code other than 200; or literally a POST request isn't being made. If the latter, is another verb being used, or is no request being sent at all?