Hi everyone, there’s a method to fetch data from an API endpoint and transform it in a local version inside jekyll?
My ideas is to fetch the data from the endpoint, generate the json file inside the _data folder and then use {{site.data.blabla}} to show the collection data inside the template. It’s possibile?
Not in Jekyll core there isn’t.
You’ll need a custom plugin to do it… something like this for example:
Jenkins can read JSON, YAML and CSV data from _data
directory and you reference as
---
---
{{ site.data.my_data | jsonify
Or use a for loop or whatever
Jenkins doesn’t care whether the file is known to git or you made it my hand or with a command.
If you use Netlify or GH Actions you can add your flow whenever the site builds.
download.sh > _data/my_data.json
jekyll build
And then ignore the file from git.
You don’t need a Jekyll plugin if you have a python or shell script that writes the data
If you need realtime data such as from a stock exchange API or your own API (which might wrap a database of say movie data).
You can use Netlify Functions as serverless API endpoints you can hit from your frontend. The Function requests the externally API internally, processes the data, then returns data to the frontend. In my case the external API disallowed cross-domain requests so I had to use a server-side layer (well, serverless but traditionally server-side PHP, Node, Python server)
Plus you can add caching so your Function for say 30 mn doesn’t have to fetch the external data on every single user request.
OR
You can use the JS frontend to request a REST or GraphQL API. At the risk of rate limiting such as in my case.
And putting more burden on the frontend code to know how the external service works, when you could move that logic to Node code in a Netlify Function.