I am a newbie to Strapi v4 and I know how to create a data entry within the Strapi Admin dashboard for a collection type. What I would like to do is to fetch the data from an external API and use the fetched data to populate the data entry. Is it possible? If yes, would you be kind enough to show me how step-by-step?
So far the only example I can found in the Strapi v4 document is dealing with the docker file.
For example, I have a Java API that provides all the necessary data, and I want to do a GET/ from it. The returned data will be used to create the data entry for a collection type.
What you should probably do is to develop a custom route with a controller / service logic which you can read about in the Strapi docs in the Development / Back-end customization segment (Back-end customization - Strapi Developer Docs).
You should probably use fetch API to GET your data into the Strapi server first (the best way is to either do it in a controller within custom API so that you can start it on a request with use of a webhook as needed OR… to have a CRON scheduled for you to do that on a time-interval basis) and then populate the database with it by using the Entity Service API create method (CRUD operations with Entity Service API - Strapi Developer Docs) in a loop for all the data (should probably come as an array of objects) that will rather need to be mapped in a proper way before you do that so that it matches the strapi collection structure.
Here’s the code snippet for the Entity service method mentioned:
Also if you decide to use a webhook to trigger the fetch request and then creating the entries in your database you will also might want to use a service to separate that logic so that your code stays DRY if you’ll need to reimplement it.
Hope that helps and if anything is not clear enough at this point and you would like me to elaborate on how you can achieve it, please feel free to get back to me and I will try to give you a more comprehensive answer to this.
I believe I understand the concept, but it is the “how to” that gets me because the lack of domain knowledge in Strapi. My background is in .NET Core and I am relative new to CMS. If you can assist me with more details for the “how to” between the external API to Strapi that would be great. Once the data is in Strapi, I know what to do.
async getUser(ctx) {
// get parameter from URL
const { id } = ctx.params;
// external API
const url = `${EXTERNAL_API_URL}/${id}`;
// fetch data from external API
const { data } = await axios.get(url);
ctx.body = data;
// Use strapi.entityService to create a new data entry in Strapi
await strapi.entityService.create("api::end-user.end-user", {
data: data,
});
return ctx.body;
},
Hello Aleaff
Just wondering if you ever had any success with your endeavours? I would like to investigate a similar usecase of using Strapi as an interface from a frontend (Appsmith) to APIs available from TypeDB to create/crud graph databases within it.
Might you have any repos to show examples of the approaches that might have worked for you? Many thanks.