Storyblok is the first headless CMS that works for developers & marketers alike.
The default field types in the Storyblok Block Library are great – they allow you to add and modify Storyblok-related data that is output in the API. However, sometimes you want to fetch data outside of Storyblok. The Field Type Plugin is an excellent feature that allows you to fetch and output external data.
With a simple Single File Component built in Vue, we can fetch artist data from Spotify and render it in the front end.
Since the Field Type Plugin is written in Vue, basic knowledge of Vue application programming is required.
A good tutorial on how to get the Spotify token can be found here.
How to use the Spotify API
To be able to use the Spotify API, you have to install an app. Installing an app on the Spotify developers page is easy and free. Head over to the Spotify dashboard, login with your Spotify account, and click on Create an app.
After adding a name and description, press Create {1}.
After submitting, proceed by copying the Client ID{1} and Client Secret {2}, we will need them later.
The next step is to whitelist the localhost domain. Click on Edit Settings. Add http://localhost to Redirect URIs and click Add and then Save at the bottom of the modal.
The next step is to obtain a refresh token which is needed to allow creating an access token that will allow us to fetch data from the Spotify API.
Replace the CLIENT_ID with your Spotify app client and open this link:
The following screen should be open. Click on Agree {1}.
The browser should redirect to an empty screen, which is expected to happen. What is important is to copy the URL parameter code, which is needed to fetch the refresh token.
Run the following command in your terminal but please make sure to replace the CLIENT_ID, CLIENT_SECRET, and CODE:
After calling the above command, the result should look like this:
Copy the refresh_tokenand then you are ready to build your Storyblok Field Plugin.
Creating the Field Type Plugin
In order to find artists, we need to give the content editors the ability to type an artist's name into a text input. When submitted, it should display a list of matching artists from Spotify. Let's start by adding the basics of the plugin.
The function initWith() sets a state value called model, which is output by the plugin. The watch observes the model that updates the output every time the model data is updated. These functions are related to the Storyblok Vue plugin.
To allow our content editor to search for artists, let's add a simple form including an input field and button.
As you can see, CSS classes prefixed with uk- are used. The Storyblok Plugin UI uses a style utility library called UIkit.
Let's continue by adding relevant data and looping the found artists.
The artists are looped through and displayed with an image and a name. In its current state, data is not displayed because the array is empty.
Let's change that by getting data.
We will need an access token provided by the Spotify token API to be able to fetch artist data.
Add the client ID, client secret, and refresh token to the constants on the top of your file. The fetchToken() function provides a new access token that allows retrieving data from Spotify.
After calling the fetchToken() function, we call the Spotify search endpoint by adding the access token as the authentication bearer. The retrieved artists are set to the artist data array, which was previously looped in the template.
If an empty query happens to be submitted, the artist data object is set to empty. Finally, the search function has been added to the template to allow us to call the search function every time the form is submitted.
Fetching and displaying on its own is not helpful for the ultimate use of the plugin. The model has to be extended to add the data to the plugin output, which eventually gets sent to the frontend of the application.
spotify_id and spotify_name have been added to the initWith() function. The setItem() function, which gets called on the click of an artist, sets the newly added model entries.
Let’s apply this to a real-life example!
Our plugin is not complete without adding the output to the frontend. Firstly, let's add the plugin to our block.
Add a field with the technical name spotify_artist{1}:
Select Plugin as the field type {1}:
Select spotify-search as the custom type {1}:
The plugin should now be visible and ready:
To see the final output of the plugin, add an artist, click on Save on the top right corner, click at the arrow right of Publish, and click on Draft JSON, which should open a new tab with a JSON object including the following information:
Of course, spotify_name and spotify_id could be different depending on what artist you added.
This data could now be used to display and render a component on the frontend. For example, how about a Spotify artist widget?
Conclusion
The Storyblok Field Type Plugin is an excellent choice for handling external data. We created a plugin using Spotify which is a great example. Any other type of data could be fetched and displayed similarly.