Point to an element to see the design tokens behind it.
Back to work

FigmaVars Hooks

Typed React 19 hooks and a CLI for the official Figma Variables REST API. Keeps Figma and React apps in sync, with 100% test coverage.

My role
Creator & Maintainer
Timeline
2024 to present
Built with
  • React 19
  • TypeScript
  • Figma REST API
  • Vitest
  • Node.js
  • CLI
  • npm

Bring Figma Variables into React

Design systems teams were copying tokens from Figma into code by hand, and the variable versions drifted between the two. I built FigmaVars Hooks so React apps could read Figma Variables from the official API.

Token synchronization problems
  • Manual copy-paste workflows to sync design tokens from Figma to code

  • Version drift between Figma variables and application code

  • No way to validate variables at runtime in React apps

  • No way to load Figma variables live in Storybook or dev tools

  • Token export tools that didn't use the official APIs

Keep credentials out of the browser

FigmaVars Hooks is a React 19 hooks library with typed access to the Figma Variables REST API, plus a CLI that exports variables in CI/CD pipelines.

In the browser, the hooks read approved token data exported as JSON in the Figma Variables API response format. Authentication stays on the server or in the build and export step, so credentials never reach the browser.

Teams without Enterprise can export the JSON once and pass it to fallbackFile for static or local environments. Storybook and design system dashboards stay in sync that way, with no live API calls.

Type guards and error helpers let apps validate the data and handle API failures by status code.

Offline fallback

tsx
import exportedVariables from './figma-variables.json'

<FigmaVarsProvider
  token={null}
  fileKey={null}
  fallbackFile={exportedVariables}
>
  <App />
</FigmaVarsProvider>
Hooks, CLI, Storybook, and validation examples

Hooks and provider

tsx
import { FigmaVarsProvider, useVariables } from '@figma-vars/hooks'

import exportedVariables from './figma-variables.json'

export function App() {
  return (
    <FigmaVarsProvider
      token={null}
      fileKey={null}
      fallbackFile={exportedVariables}
    >
      <Variables />
    </FigmaVarsProvider>
  )
}

function Variables() {
  const { data, isLoading, error } = useVariables()
  if (isLoading) return <div>Loading...</div>
  if (error) return <div>Error: {error.message}</div>
  const variables = Object.values(data?.meta.variables ?? {})
  return <pre>{JSON.stringify(variables, null, 2)}</pre>
}

Hooks API

The hooks fetch and cache Figma variables at runtime:

  • useVariables() fetches all variables from a Figma file

  • useVariableById() gets one variable by ID

  • useVariableCollections() reads variable collections

  • Built-in caching and request deduplication

  • Exported response types and type guards

  • SWR configuration for revalidation, retry, and cache control

CLI export

The figma-vars-export CLI exports Figma variables as JSON in CI/CD pipelines:

  • Runs in GitHub Actions or any CI environment

  • Writes JSON that Style Dictionary can read

  • Filters by collection, mode, or variable name

  • Validates exported data with runtime guards before writing

bash
FIGMA_TOKEN=your_token npx figma-vars-export --file-key YOUR_FILE_KEY --out ./figma-variables.json

FIGMA_TOKEN=your_token figma-vars-export --file-key YOUR_FILE_KEY --out ./figma-variables.json

figma-vars-export --help

Storybook integration

Wrap stories once and every component doc reads from the approved JSON export. Re-export when the approved variables change:

tsx
// .storybook/preview.tsx
import { FigmaVarsProvider } from '@figma-vars/hooks'
import type { Preview } from '@storybook/react'

import exportedVariables from './figma-variables.json'

const preview: Preview = {
  decorators: [
    Story => (
      <FigmaVarsProvider
        token={null}
        fileKey={null}
        fallbackFile={exportedVariables}
      >
        <Story />
      </FigmaVarsProvider>
    ),
  ],
}

export default preview

Runtime guards and errors

  • isLocalVariablesResponse() validates local responses

  • isPublishedVariablesResponse() validates published data

  • isFigmaApiError() and helpers for handling status codes

  • redactToken() for safe logging and support flows

100% test coverage

The Vitest suite covers:

  • Every hook, including edge cases

  • Mocked API requests and responses, and error handling

  • Runtime validation for all data structures

  • CLI command execution and file output

Use the same data contract outside React

A separate core build covers non-React use, such as server scripts, TanStack Query, or custom fetchers, with the same data contracts.

React integration, security, and supported workflows

React 19 features

The React 19 integration uses:

  • Server Components for initial data fetching when used in Next.js

  • React's use hook for async data loading

  • Caching through React's cache function

Developer experience

The package includes:
  • Clear error messages with suggestions for common issues

  • TypeScript types exported from the package

  • A README with code examples and an API reference

  • Very little configuration to get started

  • ESM-first build with CJS interop and Node 20 toolchain

Security and rate limits

For tokens and rate limits, the package has:

  • Redaction helpers for safe token logging

  • Retry utilities with exponential backoff for 429 responses

  • Documented scopes for read/write access

Use cases

It's built for workflows like:
  • Storybook showing the live Figma variables

  • Design token validation tools that check Figma against the codebase

  • Admin panels for managing design tokens across teams

  • CI/CD pipelines that auto-sync Figma changes to code repos

Package capabilities and status

The library is on npm and I still maintain it. It's meant for token pipelines, Storybook dashboards, and Figma-to-code workflows.

It became the starting point for Primitree, which adds architecture rules, a token graph, generated output, and change reports on top of the variable data.

Project notes

100%

Vitest coverage for hooks, API errors, runtime validation, and CLI output

Type-Safe

TypeScript types plus runtime guards and error helpers

Open Source

MIT licensed and actively maintained