Reference
The package exports ScratchSession, the resource classes (Users, Projects,
Studios, Search), the cloud classes (Cloud, CloudRequests,
CloudEvents, CloudStorage), the database adapters (MemoryDatabase,
JsonDatabase, SqlDatabase), the Encoding helpers (encode, decode) and
ScratchAPIError. In normal use you only construct a ScratchSession —
everything else is reached through it.
ScratchSession
The entry point. Owns the cookie jar, holds auth state, and exposes the resource groups.
Construction
options:
Properties
Methods
Lower-level helpers
apiGet(path, params?), authedJson(url, options?, extraHeaders?),
authedFetch(url, options?, extraHeaders?), authHeaders() and requireAuth()
are used internally by the resource classes and are available if you need to call
an endpoint that doesn't have a dedicated method yet.
session.users
page is { limit?, offset? } (Scratch caps limit at 40).
session.projects
Reads (no login required for shared content):
Writes (require login + ownership):
save() accepts a scratch4js Project or any { json, assets }, where
assets is a Map/object of md5ext → Uint8Array or an array of
[md5ext, bytes] pairs. See Editing projects.
session.studios
session.search
Each takes { mode?, language?, limit?, offset? }, where mode is 'popular'
(default) or 'trending'.
session.cloud(projectId, options?)
Returns a Cloud. For Scratch (the default host) this requires login
and attaches the session cookie; pass a custom host and it's treated as
unauthenticated (no login, no cookie). options overrides the defaults:
Other ways to construct one, no session needed:
Constants: Cloud.SCRATCH_HOST, Cloud.TURBOWARP_HOST. The Cloud.isScratch
getter reports whether a connection targets Scratch.
Cloud
CloudRequests
Built with cloud.requests({ requestVar?, usedCloudVars? }).
The handler ctx is { name, args, requestId, requester }.
CloudEvents
Built with cloud.events({ source?, interval?, limit? }). source is 'logs'
(Scratch — polls the public log, reports the user and create/delete, works
without login) or 'websocket' (TurboWarp/custom — listens on the socket; only
set fires). It defaults to logs on Scratch and websocket elsewhere.
Activity events receive { user, verb, name, value, timestamp } (user and
timestamp are null in websocket mode).
CloudStorage
Built with cloud.storage(options?) — a cloud-backed key-value store
(scratchattach's Cloud Storage protocol). Serves get, set, keys,
database_names and ping requests over the cloud.
Databases implement { name, get(key), set(key, value), keys() }. Bundled:
SqlDatabase needs no bundled driver: pass a query(sql, params) wrapping your
client (better-sqlite3, mysql2, pg, …) that resolves to an array of row objects.
See Cloud variables & requests.
ScratchAPIError
Thrown on any non-2xx response (or a failed request). Extends Error.