Create users using rest-api

https://docs.discourse.org/#tag/Users/operation/createUser

http://forums.my-domain.net/users.json returns a 404

Is the documentation outdated?

1 Like

Hello and welcome @NubeBuster :slight_smile:

I’ve just tried this on my test site and I have successfully created a user, so I believe it’s up-to-date and functional.

Are you using an API key and user with adequate permissions?

2 Likes

After changing the url to http://forums.my-domain.net/users instead of .json I get http “204 no content” back.

When creating the api key I don’t see the create route in granular mode. So I’ve set it to global. Perhaps the issue is indeed that I don’t have permission. Do I need to create the key as system user? Currently I am using the admin user with “Discourse Admin” group


const data = create = {
    name: "TestName",
    email: "test@mydomain.com",
    password: "TestTestTest",
    username: "TestUsername",
    active: true,
    approved: true,
    "user_fields[1]": true,
    external_ids: {
      uuid: "some uuid",
    },
};
//URLSearchParams is the same as FormData
const formData = new URLSearchParams();
for (const key in data) {
  formData.append(key, data[key]);
}

const url = BASE_URL + "users";
const response = await fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
     Accept: "application/json",
     "Api-Key": API_KEY,
     "Api-Username": API_USER,
  },
  body: formData,
 });

console.log("Response: " + response.status + ": " + response.statusText);

Perhaps you could show me the command/code you use to send the request? @JammyDodger

What problem are you solving by creating users via the API?

I want users to be bound to a minecraft account. Only visitors of my minecraft server can sign up. They will do so by performing an in-game command with their email address. Their username and display name will be their ingame name.

This is to prevent spam accounts, and to bind the username so it’s the same as their game tag.

2 Likes

I’ve solved the problem. The url had to be https not http.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.