How to add an endpoint to a plugin?

Hello there,

I found this article here https://strapi.io/blog/how-to-create-a-custom-api-endpoint-in-strapi, describing creating an endpoint for api. If I follow this, and do a yarn strapi generate, I can also make the endpoint for my plugin. The generator created two files (one controller, and one service). This is fine but what kind of implementations afterwards? Also for me absolutly not logical, he creates the controllers and service direct under /src/plugin/controller and /src/plugin/service. Why not directly under /src/plugin/server/ … My plugin also uses a router, controller and so on…

Anyway, my plugin ist finished and I want to use it for external api-calls also. But if I try to call /api/mypluginname/ and so on, nothing happens, only error messages I got and get. In the admin panel I do not found my plugin under settings / role.

So my question: How to realize, that I can get access to my plugin services with an external tool like JSONVisualizer, to get all infos as a json return?

In the docs is nothing described. Please help.

regards,
Sven

Hello there,

I found the solution. It is very confusing that the original file generated by generator looks like:

module.export = [
      {
        method: 'GET',
        path: '/',
        handler: 'myController.index',
        config: {
          policies: [],
        },
      },
...
]

Also confusing in my opinion is, that the generator, if you create an endpoint for a plugin does it under /src/plugin directly.

The solution is simple.

I found this post here https://forum.strapi.io/t/permissions-for-strapi-plugin/19438/9.

So I changed my router file (src/plugins/server/routers/index.js) in that way:

module.exports = {
  admin: {
    type: 'admin',
    routes: [
      {
        method: 'GET',
        path: '/',
        handler: 'myController.index',
        config: {
          policies: [],
        },
      },
   ...
  },
  'content-api': {
    type: 'content-api',
    routes: [
      {
        method: 'GET',
        path: '/do-job-with-description/:description',
        handler: 'myController.doJobWithDescription',
        config: {
          policies: [],
        },
      },
    ]
  }
};

And now I can use an external tool also. Please notice: My Strapi backend runs behind a firewall in a save virtual enviroment and no one outside can use it. So, if you want to serve data to outside, you have to set the policies.

regards,
Sven