Class FigmaClient

java.lang.Object
com.codename1.art.FigmaClient

public final class FigmaClient extends Object

Fetches a file from Figma's REST API and reads it into an ArtDocument.

The token is a personal access token (or OAuth token) the user supplies. It is kept only in this object, sent only to api.figma.com in the X-Figma-Token header, and never stored or logged. Requests block until they finish, so call from a background thread or through invokeAndBlock.

String token = "PERSONAL_ACCESS_TOKEN";   // typed by the user, never stored
Display.getInstance().startThread(() -> {
    try {
        ArtDocument doc = new FigmaClient(token).fetch("FILE_KEY_FROM_THE_URL");
        Log.p("Layers: " + doc.getRoot().getChildren().size());
    } catch (IOException err) {
        Log.e(err);
    }
}, "figma").start();
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The API this client talks to.
    static final int
    The most image fills downloaded for one file.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a client.
  • Method Summary

    Modifier and Type
    Method
    Description
    fetch(String fileKey)
    Fetches a file with its geometry and image fills and reads it.
    byte[]
    fetchJson(String fileKey, Map<String,byte[]> images)
    Downloads a file without reading it: its JSON is returned and the pictures its fills name are put in images, keyed by image ref.
    byte[]
    get(String path)
    The raw JSON of any API path, such as files/KEY/nodes?ids=1:2&geometry=paths.
    setOAuth(boolean oauth)
    Sends the token as an OAuth bearer token instead of a personal access token.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • FigmaClient

      public FigmaClient(String token)

      Creates a client.

      Parameters
      • token: a personal access token
  • Method Details

    • setOAuth

      public FigmaClient setOAuth(boolean oauth)

      Sends the token as an OAuth bearer token instead of a personal access token.

      Parameters
      • oauth: true for an OAuth token
    • fetch

      public ArtDocument fetch(String fileKey) throws IOException

      Fetches a file with its geometry and image fills and reads it.

      Parameters
      • fileKey: the key in the file's URL, figma.com/design/KEY/...
      Throws:
      IOException
    • fetchJson

      public byte[] fetchJson(String fileKey, Map<String,byte[]> images) throws IOException

      Downloads a file without reading it: its JSON is returned and the pictures its fills name are put in images, keyed by image ref. FigmaReader.read(json, images) turns the pair into the same document fetch returns, so a caller can keep a copy of a file and read it again without asking Figma, whose quota on whole-file reads is small.

      Parameters
      • fileKey: the key in the file's URL, figma.com/design/KEY/...

      • images: receives the pictures the file uses

      Throws:
      IOException
    • get

      public byte[] get(String path) throws IOException

      The raw JSON of any API path, such as files/KEY/nodes?ids=1:2&geometry=paths.

      Parameters
      • path: the path after /v1/
      Throws:
      IOException