In order to simply the tutorial a bit, I've created a basic starter for the plugin we'll create. You can clone it and setup the project. After this step we'll have the basic setup for the app built and we can focus on building out the plugin.
Clone the starter project
After running this command you shouldn't see any red errors in your console! We can proceed from here.
Step 02. Run your plugin in Figma
The next step would be to test that your app works in Figma. Follow these steps:
In the Menu Bar Click "Plugins > Manage Plugins"
Hit the "+" button next to Create New Plugin
Click "Click to choose a manifest.json file"
In the project we just cloned select the manifest file in the dist folder ex. ~/Desktop/figma-plugin-news-api/dist/manifest.json
Now right click in any figma file Select "Plugins > Development > FigmaNewsApiPlugin"
If everything worked you should see...
Step 03. Grab your API Key From NY Times
Before we get started with building out the rest of the application we will need to get an access token which will allow us to make API requests to the NY Times. Follow these steps:
Confirm the email in your inbox / Sign into your account
Go to the new app page and fill in Name something like "Figma Plugin" Create NY Times App
Enable "Article Search API"
Click "Create"
Copy the API Key ex. 2jH4mOVGrKa8YrkBAJ7sF3u9JppLLIvl
Save this to your clipboard for safe keeping
Step 04. Create the UI for the App
Create the Form Component
Our Figma App will consist of two forms:
Auth: A Form to Authenticate the user and save the users api key
Search: A Form to Search the NY Times
We can create a simple form component to drive both parts of our app.
In src/components/Form.tsx add the following
Create the Utilities file with our API Requests
In order to get our auth form working we'll also need to create the API request to fetch articles from NY Times.
Create a new folder and file and add src/utils/index.ts add the following:
Create the Authorization Form
The first component we will create will be a form to save the API Key we created in Step 3. That component should look like this
In src/components/Auth.tsx add the following
Create the Search Form
Once our users can login they'll need a form to perform searches
In src/components/Search.tsx add the following
Render our Components in App
The step will want to take it to render our new Auth and Search components in our App.
In src/components/App.tsx update to:
Wrap Creating the UI
With the code above we should now have all of the UI we need to grab the API key from the user. As well as making API requests to the NY Times API!
Note if you want to try out the App as is an easy way to do it is to use serve on the dist folder
Additionally I realize there a bunch of steps above. If you are seeing any errors you can checkout the step-2 branch with all of the UI. In your terminal run:
Step 05. Start Interacting with Figma
Save the NYTimes API Key
Currently anytime a user opens the Figma plugin they will have to input the api token. We can bypass this step by storing the API Key in our plugin data. To enable this functionality we will make changes in three files messages.ts, plugin.ts and src/Auth.tsx
Inside of our messages file src/messages.ts let's add a new line for our SAVE_AUTH event and export it
In our src/utils/index.ts file let's add a function to send JSON messages from anywhere in our UI
Inside of src/components/Auth.tsx we will want to add a few things.
To save the response to our plugin we will want to add the following to our plugin.ts file
We will add a few lines into our src/components/App.tsx to change the UI once the user is logged in
Now our Figma app will store the users API Key so they don't have to enter it again. If you want to skip the steps above run the following in your project!
Step 06. Fill Data from NYTImes into Figma
Finally with our app setup and UI working we can fill data from the NYTImes into Figma. We will make changes in messages.tsplugin.ts and in src/components/Search.tsx
Start with adding our new FILL_RESULTS message in messages.ts
Then import the message and post it just like we did for auth in src/components/Search.tsx
Just like we did for Authorization, we need to recieve the search results in our plugin and update the UI in plugin.ts
Start recieving the event in our plugin right below the SAVE_AUTH Message add
Below our main function we can add our "fillArticles" function
If you want to skip the steps above run the following in your project!