Custom api endpoint in strapi4 returning 404 not found error

[details=“System Information”]

  • Strapi Version: 4.4.0:
  • Operating System: MacOs Sonoma:
  • Database: PostgresSql:
  • Node Version: 16.20.2:
  • NPM version: 8.19.4:
  • Yarn version: 1.22.19:

I am building a strapi application with javascript where I have to fetch some data from an external api. For my use case, I have to create a custom strapi api endpoint, which responds with the fetched data when hit. I do not want to create the api against any content type. I just want an endpoint for my above use case. Here is the code for the files I have been working with:

//.src/api/lpv-banner-data/controllers/lpv-banner-data.js

'use strict';

module.exports = {
  async fetchData (ctx, next){
    try{
      const data = await strapi.service("api::lpv-banner-data.lpv-banner-data").fetchData();
      console.log(data);
      ctx.body("data", data);
    }
    catch(err){
      ctx.badRequest("Post report controller error", err);
    }
  } 
};
//.src/api/lpv-banner-data/routes/lpv-banner-data.js

module.exports = {
  routes: [
    {
      method: 'GET',
      path: '/',
      handler: 'lpv-banner-data.fetchData', 
      config: {
        policies: [],
        middlewares: [],
      },
    },
  ],
  prefix: 'lpv-banner-data',
  controller: 'lpv-banner-data',
};
//.src/api/lpv-banner-data/services/lpv-banner-data.js


'use strict';
const axios = require('axios');

module.exports = {
    fetchData: async ()=> {
        try{
            const response = await axios.get(`https://api-endpoint`);
            const data = response.data;
            const propertyTypesData = await getPropertyTypes.json();
            return propertyTypesData;
        }catch(err){
            return err;
        }
    }
};

The external api gives a response. I have tried defining the entire fetch logic in the controller. But the results were same. There were no middlewares written for the above usecase. I have given role permissions in the settings in the admin panel. Note that I cannot write this api against any already built content types or cannot create a new content type. Is there a way to achieve this?

Hello,

I can help you with that. Use the below logic to create custom API:

//.src/api/lpv-banner-data/routes/lpv-banner-data.js
'use strict';
const { createCoreRouter } = require('@strapi/strapi').factories;
const defaultRouter = createCoreRouter('api::lpv-banner-data.lpv-banner-data');

const customRouter = (innerRouter, extraRoutes = []) => {
  let routes;
  return {
    get prefix() {
      return innerRouter.prefix;
    },
    get routes() {
      if (!routes) routes = innerRouter.routes.concat(extraRoutes);
      return routes;
    },
  };
};

const myExtraRoutes = [
  {
    method: 'GET',
    path:'/api-route',
    handler:'api::lpv-banner-data.lpv-banner-data.fetchData'
  }
];

module.exports = customRouter(defaultRouter, myExtraRoutes);
//.src/api/lpv-banner-data/controllers/lpv-banner-data.js
'use strict';
const { createCoreController } = require('@strapi/strapi').factories;
const moment = require("moment");

module.exports = createCoreController("api::lpv-banner-data.lpv-banner-data", ({ strapi }) => ({
  async fetchData (ctx, next){
    try{
      const data = await strapi.service("api::lpv-banner-data.lpv-banner-data").fetchData();
      ctx.send(data)
    }
    catch(err){
      ctx.badRequest("Post report controller error", err);
    }
  } 
}))

Then execute your API endpoint in the frontend.

await axios.get(`https://sitename/api/api-route`);

Hope this helps you and get you started :wink:

1 Like