Figma REST API Client
The `figma-api` package provides a thin, typed JavaScript/TypeScript wrapper around the official Figma REST API. Currently in its 2.1.2-beta version, this library offers a client-side implementation designed for both browser and Node.js environments. It leverages Promises via Axios for HTTP requests and is fully typed with TypeScript, aligning its API methods directly with the official Figma REST API specifications. A significant rewrite occurred in version 2.x, standardizing endpoint method names and argument structures to precisely match Figma's documentation, ensuring future consistency. The package differentiates itself by closely mirroring the official API spec, providing a robust, type-safe interface for interacting with Figma files and resources programmatically, making it easier for developers to build integrations without constantly referring to external documentation for method signatures.
Common errors
-
TypeError: Figma.Api is not a constructor
cause Attempting to import `Api` as a named import (e.g., `import { Api }`) instead of using a namespace import, or incorrectly accessing it after a CommonJS `require`.fixUse a namespace import: `import * as Figma from 'figma-api';` and then access the class as `new Figma.Api(...)`. If using CommonJS, which is not officially supported for modern versions, module interop might be needed or consider transpiling. -
Property 'getFile' does not exist on type 'Api' or 'TypeError: api.getFile is not a function'
cause Attempting to call an API method with the old v1.x signature (e.g., `api.getFile('file-key')`) or on an outdated instance after upgrading to v2.x.fixEnsure your code uses the v2.x API method names and argument structures, which expect parameters to be passed within objects (e.g., `api.getFile({ file_key: '...' })`). Consult the official Figma REST API documentation for correct endpoint signatures. -
Error: Request failed with status code 403
cause The provided `personalAccessToken` or `oAuthToken` is invalid, expired, or lacks the necessary permissions for the requested operation.fixVerify that your Figma token is correct, not expired, and has the required 'file_read' or other necessary scopes for the API call you are making. Generate a new token from Figma settings if unsure. -
Error: Network Error (or) Cross-Origin Request Blocked by CORS policy
cause Client-side browser environment is blocking the request due to CORS policies, or there's a general network connectivity issue preventing the request from reaching Figma's API.fixIf in a browser, ensure your application's origin is allowed by Figma (which it generally is for standard API calls) or that your browser environment isn't imposing stricter local policies. For Node.js, check network connectivity. If using a direct CDN link in the browser, consider bundling with npm to mitigate CORS, or implement a proxy.
Warnings
- breaking Version 2.x is a complete rewrite with significant breaking changes from 1.x. All endpoint methods have been renamed, and their arguments now consistently use objects (e.g., `pathParams`, `queryParams`, `requestBody`) instead of direct individual values.
- gotcha The library is currently distributed as a beta version (e.g., 2.1.2-beta). While actively maintained, users should be aware of its pre-stable status and potential for further minor adjustments before a stable 2.0.0 release.
- gotcha When using the browser version directly via CDN links (e.g., `figma-api.min.js`), you might encounter Cross-Origin Resource Sharing (CORS) limitations if the API requests are not made from a whitelisted origin.
- gotcha Authentication requires either a `personalAccessToken` or an `oAuthToken`. Misconfiguring or providing an invalid token will result in API errors (e.g., HTTP 403 Forbidden).
Install
-
npm install figma-api -
yarn add figma-api -
pnpm add figma-api
Imports
- Api
import { Api } from 'figma-api'; const api = new Api({ personalAccessToken: process.env.FIGMA_TOKEN });import * as Figma from 'figma-api'; const api = new Figma.Api({ personalAccessToken: process.env.FIGMA_TOKEN }); - oAuthLink
const Figma = require('figma-api'); const oauthUrl = Figma.oAuthLink(...);import * as Figma from 'figma-api'; const oauthUrl = Figma.oAuthLink('CLIENT_ID', 'REDIRECT_URI', 'file_read', 'STATE_STRING', 'code'); - oAuthToken
import { oAuthToken } from 'figma-api'; const tokenResponse = await oAuthToken(...);import * as Figma from 'figma-api'; const tokenResponse = await Figma.oAuthToken('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI', 'CODE', 'authorization_code');
Quickstart
import * as Figma from 'figma-api';
const personalAccessToken = process.env.FIGMA_PERSONAL_ACCESS_TOKEN ?? '';
const fileKey = process.env.FIGMA_FILE_KEY ?? '';
if (!personalAccessToken || !fileKey) {
console.error('Please set FIGMA_PERSONAL_ACCESS_TOKEN and FIGMA_FILE_KEY environment variables.');
process.exit(1);
}
export async function getFigmaFile() {
try {
console.log('Initializing Figma API client...');
const api = new Figma.Api({
personalAccessToken: personalAccessToken,
});
console.log(`Fetching file with key: ${fileKey}...`);
// The getFile method in v2.x expects an object with parameters
const file = await api.getFile({ file_key: fileKey });
console.log('Successfully fetched Figma file structure.');
// Accessing basic file data
console.log(`File Name: ${file.name}`);
console.log(`Last modified: ${file.lastModified}`);
console.log(`Number of top-level children: ${file.document?.children?.length ?? 0}`);
return file;
} catch (error) {
console.error('Error fetching Figma file:', error.message);
if (error.response) {
console.error('Response data:', error.response.data);
console.error('Response status:', error.response.status);
}
throw error;
}
}
// Execute the function when the script runs
getFigmaFile().catch(err => {
console.error('Figma API operation failed:', err);
});