How to logout user by api

AI-generated summary

In a Support topic tagged rest-api, adopilot initially struggled to log out users via the Discourse API, despite successfully using the same API key and username headers for other operations like fetching users by external ID. They referenced a Meta Discourse thread and questioned whether the API key and username needed to be sent in the request body as well. Their users were logged in via SSO, and while admin logout worked, the API call returned a 200 OK but no visible effect. In post 2, adopilot resolved the issue: their development environment was pointing to an HTTP endpoint, but the logout API endpoint requires HTTPS. After switching to HTTPS, the logout worked. They also shared a working C# code snippet for future readers, showing how to construct the request with the correct headers and endpoint URL: https://[your-discourse]/admin/users/{user_id}/log_out with api_key and api_username in the form data. The key takeaway: some API endpoints may work over HTTP, but logout specifically requires HTTPS.

For while I am trying to logout user by api, but without success,

I did read SSO Logout via Master API Key isn't working - #2 by simon

My httpclient has set username and api key inside headers, and it works for other stuff e.g. post get user by external id or other.

Do I need to send api-key and api-username in body also.

My users are logged in by sso, when I logout user from admin page on discource it works.

Here is part of code how do I trying to call it

var formData = new FormUrlEncodedContent(new
{
new KeyValuePair<string, string>(“username_or_email”, user.user.username)
});
var response = await _httpClient.PostAsync($“/admin/users/{user.user.id}/log_out”, formData);
var content = await response.Content.ReadAsStringAsync();

Response returns 200 code but content are web page not {Succes: “OK” }

My apologies for opening topic.

Problem was that my development setup point to http not https instance of discource.

When I switched to https endpoint it start working.

I get confused because some of api endpoints woking over http but this on does not.

For future readers here how i logout users by C#

             var url = $"/admin/users/{user.user.id}/log_out";
            using var formData = new MultipartFormDataContent{
{ new StringContent(_forumConfigModel.ApiKey), “api_key” },
{ new StringContent(_forumConfigModel.User), “api_username” }
};

var response = await _httpClient.PostAsync(url, formData);