Free AI web copilot to create summaries, insights and extended knowledge, download it at here
4697
Abstract
can change <code>figmaFileFetch()</code> inside <code>server.js</code> to look like this:</p>
<figure id="5d0d">
<div>
<div>
<iframe class="gist-iframe" src="/gist/danhollick/48dba1ecc102bed0933b1b80d167ed21.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="b3e1">This code is quite confusing at first. Lets break it down:</p><ul><li><i>//1</i>
Our big chunk of JSON is stored in <code>figmaFileStruct</code> much like before, but now instead of returning that, we create a new variable called <code>figmaFrames</code> and make it equal to something.
By declaring a variable on the left side of an <code>=</code> like this, we are assigning its value to whatever is on the right side of the <code>=</code>. In this case we are making it equal to the <code>children</code> array inside of the <code>document</code> object that we described in the diagram above.</li></ul><figure id="4681"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FTnU4qJK2wyUxDShVpcweQ.png"><figcaption></figcaption></figure><ul><li><i>// 2</i>
Before this whole equation can resolve, we are using the <code>.filter()</code> method to find the parts of the <code>children</code> array we want. <code>.filter()</code> is a built in array method that literally filters through each item of an array based on the arguments we give it.
It’s like saying “Hey, go look through that list and make a new list with every item that has the letter p in it!”
In our code we filtering any items that have the <code>type</code> of <code>CANVAS</code>. That should give us all the canvases in our doc but we know we only have one. So we use the <code>[0]</code> on the end of <code>filter</code> to access the first child of the array. And <code>.children</code> to access the <code>children</code> array inside our <code>frame</code> object. In terms of the tree we used before:</li></ul><figure id="901c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rCjlcpGBMzOGilFPDKwRSA.png"><figcaption></figcaption></figure><ul><li><i>//3</i>
Once again, we use <code>.filter()</code>. Remember that filter makes a <i>new</i> array with the results, and doesn’t change the original one. So now we are filtering the array we just created on the line above. This time we want all the items in this array with a <code>type</code> of <code>FRAME</code>. In our case there are 4 items that match that description — leaving us with an array of 4 frame objects:</li></ul><figure id="a075"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*BRMHaF3kOF9pTUx0S7lhLQ.png"><figcaption></figcaption></figure><ul><li><i>//4</i>
Next we use the <code>.map()</code> method. This is another built in array method and it allows us to <i>iterate</i> through an array<i>.
</i>This is like saying “Hey, for every item in that list that contains the letter p, make a new list and replace it with the letter d”.
We are using this step to structure the array we are going to return. Instead of the 4 whole frame objects, we only need to know the frame’s <code>name</code> and <code>id.</code> So for every frame object, we make a new simpler object that looks like this <code>{ name: frame.name, id: frame.id }</code> and store them in our array. This is the value that will be passed into our <code>figmaFrames</code> variable and that we return.</li></ul><p id="ab7c">If you hit refresh on <a href="http://localhost:3001/">http://localhost:3001/</a> you should see something like this:</p><figure id="6183"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*HJdRX4JVSGd8jNSgnyd1eg.png"><figcaption>Boom. Progress.</figcaption></figure><h2 id="c8d8">Getting images from Figma</h2><p id="1970">In Part 1 we discussed that Figma has several endpoints and so far we have only been interacting with one of them. We give it our API Key and a file id and it returns JSON describing the file. The endpoint we need to use next takes our API key, the file id <i>AND</i> the ids of the elements we want to display. It then returns the urls for the images of our frames.</p><p id="cf60">We have everything we need to ask Figma for the image urls, we just need to write some more code:</p>
<figure id="d80c">
<div>
<div>
<iframe class="gist-iframe" src="/gist/danhollick/873ca41a792c698551531fe506f4ce88.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div
Options
</figure><p id="1ac9">Lets run through this:</p><ol><li>Firstly, we need to have a list of all the ids we want. The Figma endpoint needs these ids to figure out which elements to show. So we create a variable called <code>ids</code> and use <code>.map()</code> to pull out all the ids in our <code>figmaFrames</code> array. Importantly we use <code>.join()</code> to take all the ids and add them together in a comma separated string ie: <code>123,132,321,231</code>. The ids will be included in our URL so we need them as a string.</li><li>You’ll recognise this <code>fetch()</code> call from earlier — it’s very similar to the one we use for the file endpoint. Just like before, it’s an async call meaning we expect the response to take a while. This time we are constructing a different url:
- Instead of <code>‘https://api.figma.com/v1/files/'</code> we need to use <code>‘https://api.figma.com/v1/images/'</code> as the base of our url. Just like previously we append our <code>fileId.</code>
- The next part of the url, <code>‘?scale=2&ids=’</code> is a bit confusing, but it includes the scale of the images we want to return. I have hardcoded 2x in there, but you could do your own thing if you want.
- Lastly, we add our comma separated list of ids.
The rest is the same as before. We use our API Key to construct a header to authenticate this request against Figma’s servers.
We also add a <code>catch()</code> here to print any errors to the console (we probably should have done this earlier too)</li><li>Here we are parsing the result into JSON, so we can easily work with the data. We are doing this asynchronously because we don’t know for certain that the result has resolved yet.</li><li>This is a bit of a strange line but the response we get from the Figma endpoint has a few other things attached to it. We are only interested in the <code>image</code> object inside of it. Here we make our variable equal to one of its own children objects, essentially throwing away the extra info.</li><li>At this point in time our <code>figmaImages</code> array contains objects with <code>ids</code> and <code>urls</code>. We don’t need those <code>ids</code> anymore, so before we return the data, we clean it up so that each object has a <code>name:</code> and <code>url:</code> instead. The frame names aren’t in our <code>figmaImages</code> so we are rather going to update the <code>figmaFrames</code> array we have from above. We simply need to swap out the ids with the urls that we have in <code>figmaImages</code> using <code>.map()</code>. Now our objects look like this: <code>{“name”:”Left Aligned”, ”url”:”https://reallylongurl"}</code>.</li></ol><p id="bc0c">And that’s sort of it. Every time a client goes to that path it will get a response from our server. That response will now be an array with names and urls.</p><p id="87f8">Technically we could end this tutorial here, as we achieved what we set out to do. But it feels like a bit of cop-out to end things here.
In <a href="https://readmedium.com/a-designers-guide-to-the-figma-api-326f51153fb7">Part 6</a> we are going to hook up a front-end client to talk to our server and display the image results we get back.</p><div id="8da9" class="link-block">
<a href="https://readmedium.com/a-designers-guide-to-the-figma-api-326f51153fb7">
<div>
<div>
<h2>A designer’s guide to the Figma API</h2>
<div><h3>Part 6 of 7 — Setting up a front-end.</h3></div>
<div><p>medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*2Vv6eEB8V8Ao9MiU3Eqn1Q.png)"></div>
</div>
</div>
</a>
</div><blockquote id="c9f6"><p>If you want more super long, boring content like this make sure to follow me here. If you want some short-form boring content, follow me on twitter.</p></blockquote>
<figure id="0bf0">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&key=a19fcc184b9711e1b4764040d3dc5c07&schema=twitter&url=https%3A//twitter.com/danhollick/status/1016943577983782913&image=https%3A//i.embed.ly/1/image%3Furl%3Dhttps%253A%252F%252Fpbs.twimg.com%252Fprofile_images%252F772716760420192256%252FzolPi_ki_400x400.jpg%26key%3Da19fcc184b9711e1b4764040d3dc5c07" allowfullscreen="" frameborder="0" height="281" width="500">
</div>
</div>
</figure></iframe></div></div></figure></article></body>