Skip to content

Node.js API ​

Cobalt’s Node.js API is for parsing and validating the Design Tokens Community Group format (DTCG) standard. It can’t output code like the CLI can, but it is a lightweight and fast parser/validator for the DTCG spec that could even be used in client code if desired.

Setup ​

sh
npm install @cobalt-ui/core

Usage ​

Parse a tokens.json file into a JS object

js
import co from "@cobalt-ui/core";

const designTokens = {
  color: {
    red:   {$type: "color", $value: "#e34850"},
    green: {$type: "color", $value: "#2d9d78"},
    blue:  {$type: "color", $value: "#2680eb"},
  },
};

const {errors, warnings, result} = co.parse(designTokens);
NameTypeDescription
resultToken[]Flattened array of all parsed tokens in the schema (this may be incomplete if errors present)
errorsstring[] | undefinedIf present, unrecoverable errors were encountered (you should probably throw with these messages).
warningsstring[] | undefinedIf present, the parser found schema issues that are likely undesirable, but the schema is still usable (you should probably show the user).