How to call default API endpoint in custom plugin?

When you use an API endpoint from within a plugin, you are probably using that code from within the Strapi Admin UI - and the user will then be ‘authenticated’. (The point of using the ‘get’ helper is that it adds the bearer authorization automatically.)

When you use an API endpoint from a separate application, you have two options:

  1. Add an API token (generated within the Admin UI) as authorization, or

  2. Change the API endpoint to not require authorization.

For a collection type you created by means of the Content Builder, the code for the respective API endpoint can be found in ~/src/api - one folder per content type. To change the auth type for a particular route, you would need to edit the generated file in the routes folder, and add auth: false to the corresponding route entry. Look in the Strapi docs for details of how to configure a core router - e.g. for a generated API endpoint. The sample code there will show you exactly how to override the default auth requirement - or any other route config parameters.

Also, you only need the ‘get’ helper inside the Admin UI … For a separate front-end app, simply use Axios or indeed just the Fetch API.

1 Like