# How to ban / unban a member from a channel?

**URL:** <https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442>\
**Category:** General\
**Created:** [May 24, 2022, 2:35pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442 "2022-05-24T14:35:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [May 24, 2022, 2:35pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/1 "2022-05-24T14:35:18Z")

</div>

How should I proceed to ban and unban a member from a channel ?

I tried different endpoints found in the api documentation :

- api/v3/channels/{channelId}/users/ban  
=\> throws 401

- api/v2/channel/{channelId}/users/ban  
=\> throws 401

- sockets/channel.banUsers  
=\> throws 404

Obviously, I use them sending the proper body and headers.

I didn’t find any query in the sdk to achieve this.

Am I missing something here ?

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [May 25, 2022, 1:49pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/2 "2022-05-25T13:49:26Z")

</div>

Hello @jsharl error 401 is Unauthorized, means that you don’t have the permission to ban other users, you will have to be an admin. Another way for admin to ban users is via the console, please see the attached image below. If you have further questions, pls let us know 😊

 ![Screen Shot 2565-05-25 at 14.42.36](https://europe1.discourse-cdn.com/flex013/uploads/amity/original/1X/6108e7ed2d5001a06283d327382950465ff3eabb.png)

---

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [May 25, 2022, 2:00pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/3 "2022-05-25T14:00:50Z")

</div>

Means a channel moderator is not able to ban a member ?

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [May 25, 2022, 2:25pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/4 "2022-05-25T14:25:01Z")

</div>

Yes, correct, because moderator is a counted as a client role. In order to ban members, you must have administrator permission/role.

---

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [May 30, 2022, 8:58am UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/5 "2022-05-30T08:58:12Z")

</div>

We tried everything regarding ban/unban or even mute/unmute but it seems like we can’t manage to make it work.

Everything I get is a 401 or a 404 in some cases.

For each channel we create, we want to be able to promote one or more members as moderator(s).  
A moderator should be able to ban / unban (or mute / unmute ) basic members.

We want a former moderator for each room , and this moderator should be able to promote other members as moderator from the front-end of the app.

We don’t want the moderator to be global, but only for one room.

What would be the settings to achieve this?

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [May 30, 2022, 2:17pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/6 "2022-05-30T14:17:32Z")

</div>

Hi @jsharl since this action is not supported by our sdk as I mentioned above. Thus, I have double checked with the team for other possible ways, and yes, we do have a workaround to achieve this.

You can check for user role in your front-end first if they’re moderator or not. If yes, then you can call the ban user API using the hardcode admin access token. However, I would suggest you to build a ban API on your backend and inside that API, you can call Amity ban user API with the hardcode admin access token as it’s more secure and there’s no need for frontend update 🙏

---

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [May 30, 2022, 2:32pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/7 "2022-05-30T14:32:31Z")

</div>

Hi,  
thx for your response.

> You can check for user role in your front-end first if they’re moderator or not. If yes, then you can call the ban user API using the hardcode admin access token

Actually, this is what we are currently doing and talking about.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [May 30, 2022, 2:49pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/8 "2022-05-30T14:49:03Z")

</div>

Can you please share your code with us? the team will help check, thank you 🙏

---

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [May 30, 2022, 3:12pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/9 "2022-05-30T15:12:40Z")

</div>

Base request for Amity :

```auto
async function amityRequest(body, endpoint, method) {
  const apiUrl = "https://api.eu.amity.co";
  const token = await checkAmityToken() // see below
    .catch((error) => {
      throw new Error(error);
    });

  const headers = {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + token,
  };

  const response = await axios({
    headers,
    method,
    url: apiUrl + endpoint,
    body
  }).catch((error) => {
    throw new Error(error);
  });
  return response;
}

```

Function that gets the Amity token given a userId:  
(This request is triggered at app launch)

```auto
export const getAmityToken: any = createAsyncThunk(
    'officialChatRoom/getAmityToken',
    async (userId, thunk) => {
        const result = await amityTokenRequest(`/api/v3/authentication/token?userId=${userId}`, 'get');
        return result.data; 
// The result is stored in the local storage
// Then a function called getAmityToken retrieves it (see base request above)
    }
);

```

Request from RTK :

(I guess the issue is somewhere here in the process… am I getting the good token ?)

```auto
export const banMember: any = createAsyncThunk(
    'officialChatRoom/banMember',
    async (data, thunk) => {
        const { channelId, body } = data;
        const result = await amityRequest(body, `/api/v3/channel/${channelId}/users/ban`, 'put');
        return result;
    }
);

```

Action called from the component after interaction:

```auto
dispatch(banMember(
                {
                    channelId: Config.APPID, // Config.APPID returns env variable data
                    body: {
                        channelId: Config.APPID,
                        userIds: [memberData._id],
                    }
                }
            )

```

Thx a lot for your time and help. 🙏

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [May 31, 2022, 2:00pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/10 "2022-05-31T14:00:04Z")

</div>

Hi @jsharl just to double confirm if you get the correct Admin access token that we mentioned, we’re referring to Amity’s console admin access token which you can generate from going to [asc.amity.co](http://asc.amity.co/)

Log in → Settings → Admin Users → Click on setting icon in admin user  
 ![Screen Shot 2565-05-31 at 14.56.02](https://europe1.discourse-cdn.com/flex013/uploads/amity/original/1X/64d9b11db8a6346f76d38ea628bdb7887b1909a2.png)

and there will be a popup, then click on Generate access token. You can hardcode this admin access token and use it when calling ban user API

---

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [May 31, 2022, 2:23pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/11 "2022-05-31T14:23:18Z")

</div>

Indeed, I was not using this one. I was using the token related to the user from :

```auto
`/api/v3/authentication/token?userId=${userId}`

```

So I generated the access token from the console and hardcode it in the api request header, like so :

```auto
async function amityRequest(body, endpoint, method) {
  const apiUrl = "https://api.eu.amity.co";
  const token = '1d2u3m4m5y6t7o8k9e0n'; // access token from the console

  let headers = {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + token,
  };

  const response = await axios({
    headers,
    method,
    url: apiUrl + endpoint,
    body
  }).catch((error) => {
    console.log(error);
  });
  return response;
}

```

Issue is the same, I get a 404.

---

<div class="post-metadata">

**Author:** ![Andrew1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew1](https://forum.social.plus/u/Andrew1)\
**Post date:** [June 2, 2022, 8:09am UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/12 "2022-06-02T08:09:36Z")

</div>

Hi @jsharl can you please help try the new code snippet below and see if the issue still persists?

```auto
async function amityRequest() {
  const axios = require('axios').default;
  const apiUrl = "https://api.sg.amity.co";
  const token = '${adminToken}'; // access token from the console

  let headers = {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + token,
  };

  var data = JSON.stringify({
    "userId": "${userId}"
  });

  const response = await axios({
    headers: headers,
    method: 'post',
    url: apiUrl + '/api/v2/users/ban',
    data: data
  }).catch((error) => {
    console.log(error);
  });
  console.log(JSON.stringify(response));
  return response;
}

```

Thank you

---

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [June 3, 2022, 7:32am UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/13 "2022-06-03T07:32:57Z")

</div>

Hi Andrew,

Thx for your response.

I just tried your solution and I get a code 422.

Plus, what I’m trying to achieve is to ban a user for particular channel and not globally so I think I would need to send the channelId at some point.

ps: since I’m in europe , I used this url :  
`const apiUrl = "https://api.eu.amity.co";`

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [June 7, 2022, 12:45pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/14 "2022-06-07T12:45:17Z")

</div>

Hi @jsharl The 422 error is caused by a parameter error when calling an API, can you help share with us your request body?

---

<div class="post-metadata">

**Author:** ![jsharl](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jsharl](https://forum.social.plus/u/jsharl)\
**Post date:** [June 7, 2022, 12:55pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/15 "2022-06-07T12:55:13Z")

</div>

Hi @SocialPlus_Support ,

Here’s the body I sent the last time I tried :

Action triggered from the component :

```auto
 dispatch(banMember(
                {
                    body: JSON.stringify({
                        "userId": memberData._id,
                    })
                }
            )

```

Request from RTK :

```auto
export const banMember: any = createAsyncThunk(
    'officialChatRoom/banMember',
    async (data, thunk) => {
        const { body } = data;
        console.log(body);
        const result = await amityBanRequest(body, `/api/v2/users/ban`, 'post');

        return result;
    }
);

```

Just so you got the request parameters / headers :

```auto
async function amityBanRequest(body, endpoint, method) {
  const apiUrl = "https://api.eu.amity.co";
  const token = 'dummyadmintokenfromtheconsole';

  let headers = {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + token,
  };

  const response = await axios({
    headers,
    method,
    url: apiUrl + endpoint,
    body
  }).catch((error) => {
    console.log(error);

  });
  console.log(headers,
    method,
    apiUrl + endpoint,
    body);
  return response;
}

```

---

<div class="post-metadata">

**Author:** ![Andrew1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew1](https://forum.social.plus/u/Andrew1)\
**Post date:** [June 9, 2022, 12:31pm UTC](https://forum.social.plus/t/how-to-ban-unban-a-member-from-a-channel/442/16 "2022-06-09T12:31:58Z")

</div>

Hi @jsharl

Please change the “body” key inside axios({ }) to “data” key instead as shown below.

```auto
data: JSON.stringify({
    "userId": "${userId}"
  })

```
