developers / Getting started
hematite SDK for JavaScript and TypeScript
How to use @hematite/sdk, the official JavaScript and TypeScript client for the hematite API: install it, sign in with a token, upload and edit images.
This page as Markdown@hematite/sdk is the client the website itself uses. It's generated from the same API manifest as this reference, adds a cache, and turns answers into objects with their own methods.
Install
It needs Node.js 22.12 or newer.
npm install @hematite/sdkUse it
Make a client with a token (authentication); without one it sees only what's public.
import { Hematite } from "@hematite/sdk";
const hematite = new Hematite(process.env.HEMATITE_TOKEN);
const image = await hematite.images.fetch(1);
await hematite.images.edit(image, { title: "Clutch on Ascent" });
const me = await hematite.users.fetchMe();
console.log(me.username, me.storageUsed);Upload
hematite.images.create uploads a file and returns the image with the links the API made for it.
import { readFile } from "node:fs/promises";
const created = await hematite.images.create({
file: new File([await readFile("screenshot.png")], "screenshot.png", {
type: "image/png",
}),
visibility: "Unlisted",
});
console.log(created.shareUrl, created.deleteUrl);image.url() is the picture as everyone sees it: with its owner's watermark when they set one (the image's treatment, which the API signs for that picture). Pass a width, format and quality for a smaller copy, and { watermark: false } for the picture as it's stored, as an owner's own gallery shows it.
const tile = image.url({ width: 480, format: "webp", quality: 80 });
const asStored = image.url({ watermark: false });Enums
The API's enums are strings, and the SDK exports each as an object of its values and as a type, so UploadVisibility.Private and "Private" are the same.
import { UploadVisibility } from "@hematite/sdk";
await hematite.images.edit(image, { visibility: UploadVisibility.Private });The raw client
Underneath the methods, hematite.$api sends the API's requests as they are: a property for each part of the path, .$(id) for an id in it, and the HTTP method at the end, with query, body or file where the endpoint takes them. It's typed from the same manifest as this reference, but it isn't the SDK's way: it returns the API's plain answer (its data), without the SDK's objects, cache or live updates.
const storage = await hematite.$api.users.$(1).storage.get();Use it only for an endpoint the SDK has no public method for yet; the API reference shows those under Raw client in their SDK tab. Where a method exists, prefer it.
Live updates
The client keeps its cache current through hematite's realtime server, but it connects there only once something
asks for live updates: subscribing to a channel, or calling connect(). A client that only calls the API never opens
a connection.
await hematite.$websocket.subscribe(`images:${image.id}`);
hematite.on("image:update", (before, after) => {
console.log(`${before.title} is now ${after.title}`);
});After a drop the client reconnects by itself and subscribes again. A short drop loses nothing: within 2 minutes,
the server sends what was missed, in order, before anything new. After a longer one, or when the realtime server
restarted, what was missed is gone: the client says resync, once, and what you show should be asked for again.
hematite.on("resync", () => {
void hematite.images.fetch(image.id, { force: true });
});When you're done with a client, destroy() closes its connection for good; calling it twice does nothing more. A
client on a server or in a script that should never connect says so with websocket: false:
const server = new Hematite(token, { websocket: false });The connection tries a WebSocket first and falls back to HTTP long-polling where a WebSocket can't get through.
Elsewhere
The client talks to production unless told otherwise; apiUrl, cdnUrl and socketUrl point it somewhere else, a local stack for one.
const local = new Hematite(token, { apiUrl: "http://127.0.0.1:3101" });What changed in each version is in the package's changelog on npm.