stylehub.cloud

API documentation

Reading the design tokens

On this page

The manual's design tokens as one document in the W3C design tokens format: colors, type, spacing, motion, radii, shadows and themes, each with a value and a type. The same document the export page offers as a file, so a build can fetch it instead of somebody downloading it after every change.

Request

Endpoint
GET https://stylehub.cloud/api/v1/manuals/{slug}/tokens.json

The slug and the headers are the same as for the manual. See Reading a manual.

Example

GET acme/tokens.json
curl -sS https://stylehub.cloud/api/v1/manuals/acme/tokens.json -o tokens.json

The document

JSON
{
  "color": {
    "forest": { "$value": "#0f5132", "$type": "color" },
    "forest-500": { "$value": "#2f8a5a", "$type": "color" }
  },
  "dimension": {
    "8": { "$value": "8px", "$type": "dimension" },
    "breakpoint-md": { "$value": "768px", "$type": "dimension" }
  },
  "fontFamily": { "body": { "$value": "Inter", "$type": "fontFamily" } },
  "fontSize": { "md": { "$value": "16px", "$type": "dimension" } },
  "duration": { "fast": { "$value": "150ms", "$type": "duration" } },
  "cubicBezier": { "standard": { "$value": "cubic-bezier(0.2, 0, 0, 1)", "$type": "cubicBezier" } },
  "radius": { "card": { "$value": "12px", "$type": "dimension" } },
  "shadow": { "raised": { "$value": "0 2px 8px rgba(0,0,0,0.12)", "$type": "shadow" } },
  "theme": {
    "dark": { "background": { "$value": "#111111", "$type": "color" } }
  }
}

Every token is an object with $value and $type, grouped by kind. A group that has nothing in it is present and empty, so a build can rely on the keys being there.

The groups

color
Every color the manual defines, by its name. A swatch whose value is not a hex color is left out. A color that shows its tints and shades carries them beside it as name-50 to name-950.
dimension
The spacing scale, each step by its number with a point written as a hyphen (2-5), in px when the step has no unit, and the breakpoints as breakpoint-name.
fontFamily
The typefaces, each by the usage typed beside it in the typography block, headings or body for instance. A font with no usage is font.
fontSize
The type scale, 3xs to 6xl with md as the base size.
duration
The motion durations.
cubicBezier
The motion easings.
radius
The corner radii.
shadow
The elevation levels.
theme
One object per theme, by its slug, with a color for each role it fills: background, surface, text, text-muted, primary and border. A theme with no color in it is left out.

Which block of the manual fills which group is in Design tokens, in thirteen formats. Only the W3C format is served here; the other twelve are files from the export page.

Using it in a build

The usual shape: fetch the document in the build step, write it next to the source, and let the tool that reads it, Style Dictionary or a script of your own, turn it into whatever the platform wants.

  • Send the ETag back as If-None-Match. Most builds run far more often than the manual changes, and an unchanged manual is a 304 with no body. See Caching.
  • Read the token from the environment of the build, never from a file in the repository.
  • Token names are the names in the manual, lower cased and hyphenated. Renaming a color in the manual renames the token.

In a browser

A public or unlisted manual's tokens can be fetched from a page directly, no proxy needed. This paints the colors onto the document as CSS variables.

JavaScript
const res = await fetch("https://stylehub.cloud/api/v1/manuals/acme/tokens.json");
const tokens = await res.json();

for (const [name, token] of Object.entries(tokens.color ?? {})) {
  document.documentElement.style.setProperty("--brand-" + name, token.$value);
}

Not with a token

Only a public or unlisted manual belongs in a browser page. A token in a page is a token every reader has, and it reads every manual of the brand, the private ones included.

Something the API does that this page does not say? Write to hello@stylehub.cloud with the request and the answer you got.

Reading the design tokens - API - Stylehub