avatarDaniel Hollick

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2402

Abstract

nous</i> function, hence the <code>async</code> in front of it. In oversimplified terms, this means we are expecting the result to take a while, so we are telling the code that depends on this function that the result will <i>eventually</i> resolve but might not be ready when it first asks. You can see this on line <code>8</code> and <code>15</code>, where we use the keyword <code>await</code> to indicate that this value might not be available immediately.</li><li>Our function takes one argument, which is just the unique file ID from our file. Passing this in as an argument means we can re-use this function with other files if we want, just with a different file ID.</li><li>Notice how we had to import <code>var fetch = require('isomorphic-fetch')</code>? This is a <i>poly-fill</i> that allows us to use the <code>fetch()</code> function. It’s a bit complicated but <code>fetch()</code> allows us to fetch things from a specified path. In this case we actually construct our url by appending our <code>projectId</code> to the endpoint url.</li><li>The second argument we pass to <code>fetch()</code> is an object that contains our authentication token as a header and the GET method. This is all stuff the Figma endpoint will need to process our request, but don’t stress too much about understanding that.</li></ul><p id="14f7">This type of function is called <i>Middleware</i> because it sort of sits between the two applications.</p><p id="0c85">Before we can do anything else we need to tell the server when it should run this function. We do this by adding the following code near the end of our <code>server.js</code>:</p> <figure id="afc7"> <div> <div>

            <iframe class="gist-iframe" src="/gist/danhollick/dbe50ae011d720f3bddcae70d90382ea.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="fdfa"><code>app.use</code> tells the server to mount the middleware we just wrote when we navigate to a specific url. This is how it works:</p><ul><li>The first argument it takes is the path. When our server gets a request on that path, it’ll fire off our middleware. In this case <code>/</code> means it must fire on the <i>route</i> url. Basically the home page.</li><li>The second argument is the middleware 

Options

we want to mount. In this case we are wrapping our <code>figmaFileFetch()</code> in another <code>async</code> function because we want to make sure it can resolve. After it has resolved we parse the response into a string(so we can read it) and send it off as a response.</li></ul><p id="ea91">Now if you go into your terminal, run <code>npm start</code> and go to <a href="http://localhost:3001/">http://localhost:3001/</a> in your browser, you should see this:</p><figure id="9596"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*dk3s0nzGaSoyg6G0TGWm0g.png"><figcaption>It’s like looking into the low budget Matrix.</figcaption></figure><p id="f945">Believe it or not, this is a success. That’s the structure of our file, described in JSON. This means that our server is successfully making a request to the Figma endpoint and returning a result.</p><p id="52c3">In <a href="https://readmedium.com/a-designers-guide-to-the-figma-api-3a23a3f93d2">Part 5</a>, we are going to process this response to pull out the parts we need and make another request to a different endpoint.</p><div id="ec4f" class="link-block"> <a href="https://readmedium.com/a-designers-guide-to-the-figma-api-3a23a3f93d2"> <div> <div> <h2>A designer’s guide to the Figma API</h2> <div><h3>Part 5 of 7 — Processing the response.</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> <figure id="d1db"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=twitter&amp;url=https%3A//twitter.com/danhollick/status/1009703143645597696&amp;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>

A designer’s guide to the Figma API

Part 4 of 7 — Making a request.

You can find the code for this tutorial here:

In Part 3 we setup our Figma file as well as some stuff we need for authentication against Figma’s servers. In this part we are going to start making requests to the API.

Step 4: Make a request.

The first step in this process is to make a request to the GET file endpoint. This endpoint will return our file structure as a JSON object. We can worry about what we do with that JSON object a bit later.

We need to write a function that the server can execute to fetch the data. That function needs to do a few things:

  • Make a request to the Figma endpoint with our API key for authentication.
  • Wait for that request to resolve and parse it into JSON, just to make sure it’s in the correct format.

figmaFileFetch() is the function we need. Go ahead and copy it into server.js

A few things to note about our function:

  • It’s an asynchronous function, hence the async in front of it. In oversimplified terms, this means we are expecting the result to take a while, so we are telling the code that depends on this function that the result will eventually resolve but might not be ready when it first asks. You can see this on line 8 and 15, where we use the keyword await to indicate that this value might not be available immediately.
  • Our function takes one argument, which is just the unique file ID from our file. Passing this in as an argument means we can re-use this function with other files if we want, just with a different file ID.
  • Notice how we had to import var fetch = require('isomorphic-fetch')? This is a poly-fill that allows us to use the fetch() function. It’s a bit complicated but fetch() allows us to fetch things from a specified path. In this case we actually construct our url by appending our projectId to the endpoint url.
  • The second argument we pass to fetch() is an object that contains our authentication token as a header and the GET method. This is all stuff the Figma endpoint will need to process our request, but don’t stress too much about understanding that.

This type of function is called Middleware because it sort of sits between the two applications.

Before we can do anything else we need to tell the server when it should run this function. We do this by adding the following code near the end of our server.js:

app.use tells the server to mount the middleware we just wrote when we navigate to a specific url. This is how it works:

  • The first argument it takes is the path. When our server gets a request on that path, it’ll fire off our middleware. In this case / means it must fire on the route url. Basically the home page.
  • The second argument is the middleware we want to mount. In this case we are wrapping our figmaFileFetch() in another async function because we want to make sure it can resolve. After it has resolved we parse the response into a string(so we can read it) and send it off as a response.

Now if you go into your terminal, run npm start and go to http://localhost:3001/ in your browser, you should see this:

It’s like looking into the low budget Matrix.

Believe it or not, this is a success. That’s the structure of our file, described in JSON. This means that our server is successfully making a request to the Figma endpoint and returning a result.

In Part 5, we are going to process this response to pull out the parts we need and make another request to a different endpoint.

JavaScript
Figma
UI
Recommended from ReadMedium