Specify Docs
Specify ↗︎Changelog ↗︎Guide ↗︎
V2
V2
  • Getting started
    • Introduction
    • What is Specify?
    • Pulling your first tokens with the CLI
    • Glossary
  • Collect
    • What is a Source?
    • Available sources
      • Figma Variables & Styles
      • Tokens Studio
  • Distribute
    • What is a Destination?
    • Available destinations
      • GitHub
      • Specify CLI
      • Specify SDK
      • HTTP API
  • Concepts
    • Overview
    • Parsers Engine
    • SDTF Client
      • SDTF Engine
    • Specify Design Token Format
  • Guides
    • Configuration file 101
    • Specify CLI usage 101
      • Getting started
      • Authentication
      • Generate Files
    • Specify SDK usage 101
      • Getting started
      • Retrieving and working with the tokens
      • Updating tokens
      • Converting a token to XXX
      • Executing generation parsers
    • Specify SDK Cheatsheet
    • Manage font files
    • Querying a SDTF graph
  • Reference
    • Parsers Engine
    • Parsers
      • change-case
      • convert-color
      • convert-dimension
      • make-line-height-relative
      • filter
      • register-view
      • select-modes
      • prefix-by
      • suffix-by
      • replace-string
      • to-css-custom-properties
      • to-css-text-style
      • to-css-font-import
      • to-flutter
      • to-javascript
      • to-json
      • to-json-list
      • to-kotlin
      • to-react-native
      • to-scss-mixin-text-style
      • to-scss-map
      • to-sdtf
      • to-style-dictionary
      • to-swift
      • to-tailwind
      • to-typescript
      • svgo
      • svg-to-jsx
      • svg-to-tsx
      • to-svg-file
      • to-bitmap-file
      • to-file
    • Specify SDK
      • SpecifyClient
      • SDTFClient
      • Converters
        • CSS
      • ParsersEngineResults
    • SDTF Engine
      • Query API
      • Mutation API
      • SDTF Query Language
      • SDTF QueryResult
      • TokenState
        • Stateful Value
    • HTTP API
      • POST /parsers-engine-rpc
    • Specify CLI
  • Resources
    • Parser Rules templates
      • CSS Custom Properties
      • Tailwind
      • React Native
      • Flutter
      • SDTF
      • JSON
    • Specify CLI VS Specify SDK
    • Playground
    • Best practices
  • Useful links
    • Discord
    • YouTube
    • Twitter
    • Help Center
    • Canny
Powered by GitBook
On this page
  • Creating Specify and SDTF clients
  • Updating tokens
  • Convert a token to XXX
  • Execute a parser
  • Retrieving data
  • Get a specific token
  • Get all the tokens
  • Map over all the tokens
  • Get a specific group
  • Get all the groups
  • Map over all the groups
  • Get a specific collection
  • Get all collections
  • Map over all the collections
  • Filtering data
  • Keeping a sub-graph from a path
  • Keeping the children of a path
  • Query a specific set of data
  • Remove tokens from the SDTF
  • Renaming tokens
  • Rename a node
  • Rename a collection
  • Rename a group
  • Rename a token

Was this helpful?

Export as PDF
  1. Guides

Specify SDK Cheatsheet

On this page you'll find a lot of common actions you'll probably want to perform when using the SDK

PreviousExecuting generation parsersNextManage font files

Last updated 1 year ago

Was this helpful?

Creating Specify and SDTF clients

You will first create a Specify client, then authenticate, then create a SDTF client by fetching the repository token tree.

import { createSpecifyClient } from "@specifyapp/sdk";

const specifyClient = createSpecifyClient();
await specifyClient.authenticate("<YOUR_PERSONAL_ACCESS_TOKEN_VAR>");

const sdtfClient = 
  await specifyClient.getSDTFClientByRepositoryName("<YOUR_SPECIFY_REPO_NAME>");
  
  console.log('Repository name',sdtfClient.repository.name)

Updating tokens

The following example is only an overview, if you need more details you can have a look .

import { updaters } from '@specifyapp/sdk'

sdtfClient.update(
  updaters.color({ toFormat: 'hex' }, { where: { token: '^color-' }}),
);

Convert a token to XXX

import {
  dimensionToCss,
  breakpointToCss,
  colorToCss,
  createResolveAliasStrategy 
} from '@specify/sdk/css'

const strategy = createResolveAliasStrategy();

const outputs = sdtfClient.mapTokenStates(tokenState => 
  tokenState.matchByType(
    {
      dimension: dimensionToCss(strategy) // Output example: '12px'
      breakpoint: breakpointToCss(strategy) // Output example: '12px'
      color: colorToCss(strategy) // Output example: '#ff00ff'
    }, 
    _ => undefined
  )
);

Execute a parser

import { parsers } from '@specify/sdk'

const executePipelines = sdtfClient
  .createParsersPipelines(
    parsers.toCssCustomProperties({ filePath: 'myFile.css' }),
  );
const parsersEngineResults = await executePipelines();

Retrieving data

Get a specific token

const tokenState = sdtfClient.getTokenState(['path', 'to', 'token']);

Get all the tokens

const tokenStates = sdtfClient.getAllTokenStates();

Map over all the tokens

const results = sdtfClient.mapTokenStates(tokenState => ...);

Get a specific group

const group = sdtfClient.getGroupState(['path', 'to', 'myGroup']);

Get all the groups

const groups = sdtfClient.getAllGroupStates();

Map over all the groups

const results = sdtfClient.mapGroupStates(tokenState => ...);

Get a specific collection

const collection = sdtfClient.getCollectionState(['path', 'to', 'myCollection']);

Get all collections

const collections = sdtfClient.getAllCollectionStates();

Map over all the collections

const results = sdtfClient.mapCollectionStates(tokenState => ...);

Filtering data

Keeping a sub-graph from a path

If you only want a specific portion of the graph, you can cherry-pick using the pick function:

const colors = sdtfClient.pick(['colors']);

Keeping the children of a path

If you only want the children of a specific portion of the graph you can use the pick function this way:

const colors = sdtfClient.pick(['colors', '*']);

Query a specific set of data

You can use the query method of the SDTFClient to create a copy of the current SDTF with only the tokens that'll match your query:

const colorAndTextStyleOnlySdtf = sdtfClient.query({
  where: {
    token: '.*',
    withTypes: {
      include: ['color', 'textStyle']
    }
  }
});

Remove tokens from the SDTF

You can use the remove method to delete tokens based on a query:

sdtfClient
  .remove({ where: { token: '^Blue$', select: true } })

Renaming tokens

Rename a node

By not specifying any type, the method will rename any node as long as the path is valid.

sdtfClient.renameNode({ atPath: ['my', 'path' ], name: 'newName' });

Rename a collection

By specifying the type, the method will rename only a collection node.

sdtfClient.renameNode({ 
  atPath: ['my', 'path' ],
  name: 'newName',
  type: 'collection'
});

Rename a group

By specifying the type, the method will rename only a group node.

sdtf.renameNode({ 
  atPath: ['my', 'path' ],
  name: 'newName',
  type: 'group'
});

Rename a token

By specifying the type, the method will rename only a token node.

sdtf.renameNode({ 
  atPath: ['my', 'path' ],
  name: 'newName',
  type: 'token'
});

The following example is only an overview, if you need more details you can have a look .

The following example is only an overview, if you need more details you can have a look .

You can find all the details about querying the data .

here
here
here
here