405 PUT Method Not Allowed - Attempting to Add User to a Group Programatically

Hi there, I’m attempting to add a user to a group programatically via PUT request using the ‘keycloak-admin’ npm package on my express server but am running into a 405 status code error.

import KcAdminClient from 'keycloak-admin';

const _kc = new KcAdminClient({
  baseUrl: process.env.AUTH_KEYCLOAK_URL,
  realmName: process.env.AUTH_KEYCLOAK_REALM,
});

export class KeycloakService {
  init(req: any): void {
    try {
      let token = req.headers['authorization']
        ? req.headers['authorization'].toString()
        : '';
      token = token.replace('Bearer ', '');
      _kc.setAccessToken(token);
    } catch (e: any) {
      L.error(e);
      throw Error(e);
    }
  }

  async assignGroupToUser(user_id: string, group_id: string): Promise<string> {
    const res = await _kc.users.addToGroup({
      id: user_id,
      groupId: group_id,
    });
    return res;
  }
}

in my controller.ts file

async addRoleToGroup(req: Request, res: Response): Promise<void> {
    const {group_id, role_id, role_name} = req.params;
    try {
      KeycloakService.init(req);
      const roleToAdd = await KeycloakService.addRoleToGroup(group_id, role_id, role_name)
      res.json(roleToAdd);
    } catch (e: any) {
      const httpCode = e.response.status ? e.response.status : 500;
      res.status(httpCode).json({
        success: false,
        message: e.message,
      });
    }
  }

This is my keycloak.json file as well

{
  "realm": "x",
  "bearer-only": true,
  "auth-server-url": {auth URL specified elsewhere},
  "ssl-required": "none",
  "resource": {specified elsewhere},
  "confidential-port": 0
}

I’m making this call from the frontend of my application using axios and have been able to nail it down to the error occurring once the service is called and this part

const res = await _kc.users.addToGroup({
      id: user_id,
      groupId: group_id,
    });

has to be run. The service is called, and then cannot be completed when the keycloak client is “interacted with” or “requested to”.

Another method I tried was making a direct axios request from the frontend to my keycloak instance following the REST API docs for adding a user to a group and still no luck.

Any assistance is much appreciated