
The aim offigma package is to provide an easy-to-useclient/wrapper for theFigma API. It allows youto bring all data from a Figma file to your R session. This includes thedata of all objects that you have drawn in this file, and theirrespective canvas/page metadata.
With this kind of data, you can maybe build a custom and automatedlayout for documents, or create an automated pipeline to build designcontent for your clients.
Key features of the package:
Get all data of a Figma file, or a specific canvas/page in theFigma file, or just the metadata about the file;
Functions fromfigma package can output the datainto atibble::tibble() object;
Returns the raw contents of the HTTP response by default, to givemore freedom and information to the user;
To get the current version from CRAN:
install.packages("figma")To get the current development version from github:
devtools::install_github("pedropark99/figma")A good place to start in thefigma package, is to readthe main vignette (which you can access withvignette("figma")). But let’s give you a brief summary ofits features, shall we?
In order to use the Figma API, you need to collect two key variablesin the Figma platform, which are:
The file key is collected through the URL that appears when youaccess this file in your web browser ( Seevignette("figma") for more details), and you can create apersonal access token in the “Settings” section of the Figma platform(Seevignette("figma") for more details).
After you collected these two variables, you can usefigma::get_figma_file() to collect all data of your Figmafile, like this:
file_key<-"hch8YlkIrYbU3raDzjPvCz"token<-"My secret and personal access token ..."# Returns a `response` object:result<- figma::get_figma_file( file_key, token)By default,figma::get_figma_file() returns the rawcontents of the HTTP response. However, you can ask the function to fitthe data of your Figma file in a tibble object, if you prefer. Just passthe.output_format = "tibble" argument to the function,like this:
# Returns a `tibble` object:result<- figma::get_figma_file( file_key, token,.output_format ="tibble")print(result)# A tibble: 5 × 7 canvas_id canvas_name canvas_type object_id object_name object_type object_attributes <chr> <chr> <chr> <chr> <chr> <chr> <list> 1 0:1 Page 1 CANVAS 1:2 Background RECTANGLE <named list [9]> 2 0:1 Page 1 CANVAS 5:2 Paragraph TEXT <named list [16]>3 0:1 Page 1 CANVAS 5:3 Arrow VECTOR <named list [9]> 4 5:4 Page 2 CANVAS 5:5 BackgroundPagina2 RECTANGLE <named list [9]> 5 5:4 Page 2 CANVAS 5:6 Texto da página 2 TEXT <named list [16]>Instead of getting the data from the entire Figma file, you mightneed to collect the data from a specific page (or a specific set ofpages) of this file. If that’s your case, you can use thefigma::get_figma_page() function.
But in order to use the function, you need to collect a thirdvariable, which is the node ID, or, in other words, the ID thatidentifies the page that you are interested in (Seevignette("figma" for more details on how to collect thisnode ID). After you collected this node ID, you could get this data likethis:
node_id<-"0%3A1"# Returns a `tibble` object:result<- figma::get_figma_page( file_key, token, node_id,.output_format ="tibble")print(result)# A tibble: 3 × 7 canvas_id canvas_name canvas_type object_id object_name object_type object_attributes <chr> <chr> <chr> <chr> <chr> <chr> <list> 1 0:1 Page 1 CANVAS 1:2 Background RECTANGLE <named list [9]> 2 0:1 Page 1 CANVAS 5:2 Paragraph TEXT <named list [16]>3 0:1 Page 1 CANVAS 5:3 Arrow VECTOR <named list [9]>On the other hand, for some reason, you might be not interested inthe contents of your Figma file, just the metadata of it. For this case,you can usefigma::get_document_info() to get this kind ofinformation, like this:
result<- figma::get_document_info( file_key, token)print(str(result))List of 13 $ id : chr "0:0" $ type : chr "DOCUMENT" $ name : chr "Untitled" $ components : Named list() $ componentSets: Named list() $ styles : Named list() $ schemaVersion: int 0 $ lastModified : chr "2022-10-29T23:35:08Z" $ thumbnailUrl : chr "https://s3-alpha-sig.figma.com/thumbnails/446f0181-cfeb-49e7-aec2-36c71aa4b05e?Expires=1667779200&Signature=Mnj"| __truncated__ $ version : chr "2539463517" $ role : chr "owner" $ editorType : chr "figma" $ linkAccess : chr "view"Seevignette("figma") for more details and a morecomplete introduction to the package.