Register custom REST routes while building a plugin for Craft 3
Your code is almost there, Craft allows registering routes for the Control Panel and the front-end of the site. You need to change EVENT_REGISTER_CP_URL_RULES to EVENT_REGISTER_SITE_URL_RULES.

You are listening for an event on EVENT_REGISTER_CP_URL_RULES which will only register routes inside the control panel. This is useful for plugins that want to add controllers for settings and custom logic.


  The event that is triggered when registering URL rules for the control panel.


https://docs.craftcms.com/api/v3/craft-web-urlmanager.html#events

However, in the scenario where access is needed outside of the control panel, Craft triggers the EVENT_REGISTER_SITE_URL_RULES. This allows access for users that do not have control panel access (e.g. site guests and regular users).


  The event that is triggered when registering URL rules for the front-end site.