Can't add user to channel via API

Hey there,

I’ve recently started looking into Mattermost as an alternative for one of our ideas (embed a chat inside an app). So far it’s working just as we wanted, so we started testing it’s API in order to automate some processes.

As of now, I’m trying to add a user to a channel and it’s returning me an error that doesn’t actually make a lot of sense… The issue I’m having is exactly the same pointed out here: Add User to Channel with APIv4 endpoint however in my case it is a private channel, also created via API.

Any ideas on what could be going on?

Hi @mohr, what error are you seeing?

Hey Amy,

I was getting the same error I linked to, with “Response: No team member found for that user ID and team ID.”. However I’m trying to reproduce it in order to paste the error here and now I’m getting a different error… I can query channels and users by name, create users and teams, but the endpoint for “add user to channel” returns me a “session expired error”.

I’m testing every request using the same environment with the same personal access token.

Request: localhost:8065/api/v4/channels/557ehinz77rnjp5z1kzyac58ty/members
Body: { "user_id": "jkai5jbbc3djmkdott83ib8o8c" }

“id”: “api.context.session_expired.app_error”,
“message”: “Inválido ou sessão expirada, por favor faça login novamente.”, --> translates to Invalid or expired session , please login again.
“detailed_error”: “”,
“request_id”: “jaeoobca8pn35qfh4xxf3irq1w”,
“status_code”: 401


Hi @mohr, were you trying with the approach posted here: Add User to Channel with APIv4 endpoint?

If you mean the fact that he was trying to add the user to a group and not an open/private channel, yes. But in my case, it is a private channel, so I’m not sure what could be causing this.

More importantly, I’ve tried resetting everything, since the error actually changed to a 401 Unauthorized as I mentioned before, but nothing changed, I’m still getting a 401 error when trying to add to a channel, while other endpoints no longer work.

Another info is the fact that I’ve tried creating a new channel, and it returns the same 401 error.

So, summarizing, “Add user to a channel” and “Create a new channel” endpoints return me a 401 error, while creating and listing users and teams work just fine…

Hi @mohr,

  1. What is your Mattermost server version?
  2. Are you attempting to add the new user as part of the private channel?

Hi @amy.blais,

Server version is 5.11.0.
The user I’m trying to add already exists in mattermost, but yeah, I’m trying to give him access to this private channel.

Hi @mohr, are you (or the person who is attempting to add the other user to a private channel) part of that private channel?

I created the channel a few seconds before adding the user to it, using the same rest api token (the server admin token).