Create custom endpoint for Users in strapi v4

How create custom endpoint for Users in strapi v4

3 Likes

Yeah! It necessary for me to. Same logic as v3 doesn’t work!


I attached few screens. What i doing wrong?

3 Likes

Hey andrew,

To extend a plugin’s interface using the ./src/extensions folder in v4 of strapi:

  1. Create the ./src/extensions folder at the root of the app, if the folder does not already exist.
  2. Create a subfolder with the same name as the plugin to be extended, in your case users-permissions.
  3. Depending on what needs to be extended:
  • create a strapi-server.js file to extend a plugin’s back end using the Server API
  • or create a strapi-admin.js file to extend the admin panel.

Since you are looking for controllers and routes, here is an example of a backend extension:

// path: ./src/extensions/users-permissions/strapi-server.js

module.exports = (plugin) => {
  plugin.controllers.user.find = (ctx) => {
console.log('test');
};

  return plugin;
};

This guide is from: Plugins extension - Strapi Developer Docs

4 Likes

Merci beaucoup j’ai tester sa marché

1 Like

Yeah! Thank you so much! That works like i wanted))
This screenshot for those who was like me )))

Can you answer one more question please, or give me a link. I dont understand about router types (admin or content-api). For what things those types?

5 Likes

Hey @Andrew_Andrew, I’m glad that helped!

Routes are related to your api.

You could have routes for your admin panel, or for the client side API (content-api)

For example, imagine you are developing a custom plugin (or extending one), and you need access to some data in the frontend part of your plugin but ONLY for the admin side. You will create a route with the type of admin-api and you can now make an API call to that custom route/controller and get data that you need.

As expected, the route you created (let’s say /plugin-name/templates), will be added to the settings and you could restrict it by admin roles:

However, you may also need to create controler and a route for the client side with the type content-api, which would allow you to access it like your other content type API’s (/api/restaurant etc…), and restrict it by user role types and as they show up in this section of the settings:

I hope that helps!

All the best

1 Like

Hi, I followed the above instructions, but when I make the strapi-server.js file, I get strapiServer is not a function error and the server crashes. Any Idea what I’m doing wrong?

You have to return something, like this:

// strapi-server.js
module.exports = (plugin) => {
  plugin.controllers.user.find = (ctx) => {
    console.log('test');
};

  return plugin;
};

Thank you so much. Very helpful. A lot of new changes going from Strapi 3 to 4. Glad we have this forum.

Thank you all. Great thread.
I created a custom endpoint for users but don’t know how to access “Entity Service API”.
How can I access strapi? and update the user like so:

const entry = await strapi.entityService.findOne('api::user.user', 1, {
  populate: { someRelation: true },
});
2 Likes

Just found out a way! Here is my solution for those curious.
Looks like strapi is global and can be access anywhere.

const entity = await strapi.entityService.update(
      "plugin::users-permissions.user",
      ctx.state.user.id,
      { data }
);
2 Likes

thx. save my day.

I’m on Strapi 4.12.4. Currently trying to extend the users-permissions plugin in a similar manner.

extensions/users-permissions/strapi-server.ts:

export default (plugin) => {
  plugin.controllers.user.testRoute = (ctx) => {
    ctx.body = {
      message: "Hello World!",
    };
  };

  plugin.routes["content-api"].routes.push({
    method: "GET",
    path: "/user/test-route",
    handler: "user.testRoute",
    config: {
      prefix: "",
    },
  });

  return plugin;
};

When I try to access http://localhost:1337/user/test-route, I get a 404 error. The exported function with the plugin argument is being called. I’ve checked that with a console.log. The plugin object is also present. Even when I enter a wrong handler for the newly-created route, I get a server error, preventing the start of the app. So to me this means that the handler is registered, along with the route. I’ve also tried doing it in JavaScript instead of TypeScript. Same outcome.

The question here is - why it gives me the 404 error. :thinking:

Can somebody help?

1 Like

35 minutes later:

Realized I was accessing http://localhost:1337/user/test-route

Which should’ve been: http://localhost:1337/api/user/test-route

Hope this is helpful for somebody.

Thanks for great information google

what version strapi did you use?