# Create staged users via API?

**URL:** https://meta.discourse.org/t/create-staged-users-via-api/279052
**Category:** Development
**Tags:** rest-api
**Created:** [September 14, 2023, 7:23pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052 "2023-09-14T19:23:28Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mattdm](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattdm/32/216484_2.png) [@mattdm](https://meta.discourse.org/u/mattdm)
#### Post date: [September 14, 2023, 7:23pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/1 "2023-09-14T19:23:28Z")

</div>

I’m kicking around the idea of building a webhook+api bridge service to connect given categories on two different discourse forums. The rough idea would be:

1. A webhook on each server for Topic and Post events\[1\]
2. A listener which takes these and replicates the event on the opposite server via the API
  - Uh, something to prevent loops, obviously\[2\]
  - Check if a user with the same email address exists on the opposite server\[3\]
  - If no such user exists, create a staged user
  - Change the staged user’s notification settings so they don’t get email notifications\[4\]
  - Create or update the post under the appropriate real or staged user

3. Probably a timed routine to go through topics and make sure nothing was missed, and possibly reorder so both sides agree

From [Understanding user statuses, roles, and permissions](https://meta.discourse.org/t/understanding-user-statuses-roles-and-permissions/35171), I think that staged users would basically do what we want – if someone _did_ create an account with that email address later, they could “claim” it and interact with all their posts just as if they’d been there all along.

But, is there a way to create a staged user from the API? I don’t see that in [Discourse API Docs](https://docs.discourse.org/#tag/Users/operation/createUser).

* * *

1. and possibly also Like and Solved events, but not in the first cut 

2. I _definitely_ thought about this _before_ writing out this list 

3. using email addresses as key because account systems may not have same usernames 

4. I saw a post about this being possible somewhere around here…

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [September 14, 2023, 7:50pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/2 "2023-09-14T19:50:20Z")

</div>

> [@mattdm](#):
>
> But, is there a way to create a staged user from the API? I don’t see that in [Discourse API Docs](https://docs.discourse.org/#tag/Users/operation/createUser).
> 
> * * *
> 
> 1. and possibly also Like and Solved events, but not in the first cut [↩︎](#footnote-ref-1368457-1)
> 2. I _definitely_ thought about this _before_ writing out this list [↩︎](#footnote-ref-1368457-2)
> 3. using email addresses as key because account systems may not have same usernames [↩︎](#footnote-ref-1368457-3)
> 4. I saw a post about this being possible somewhere around here… [↩︎](#footnote-ref-1368457-4)

Not sure if this is what you want?

> [@Creating a staged user with an API call](https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667/2):
>
> Yes, just create a user via the api. They won’t be “staged” at this point since they actually exist. If they ever need to log in, they can just reset their password. This is also possible Here is a rough example of how to create a user, active them, and generate an api key for them. def create\_user user = { name: example1, email: "example1@example.com", password: "ZvAmmkcSWQfsPQLBksg7wK59", username: example1, active: "false", approv…

---

<div class="post-metadata">

### Author: ![mattdm](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattdm/32/216484_2.png) [@mattdm](https://meta.discourse.org/u/mattdm)
#### Post date: [September 14, 2023, 8:22pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/3 "2023-09-14T20:22:46Z")

</div>

Well, kind of. As the OP in that topic says, in reply to that…

> [@Creating a staged user with an API call](https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667/3):
>
> I’m trying to avoid creating a full-fledged user account and picking a username. Essentially I want to replicate whatever is happening in the email trigger (‘custom incoming email address’ in the group settings) where the sender’s email address will be ‘staged,’ such that if/when they do register, they’ll be able to claim any messages that were created on their behalf via the email trigger.

… and there, the answer is to basically inject an email and let the email handling code take care of it. But, that doesn’t work for this case, because I need to intervene and turn off email notifications for the user _before_ creating the post.

It happens that we have SSO-overrides-username turned on for _one_ side of the proposed bridge, so I think just creating something like ‘othersite-user’ could be a workaround… but that won’t necessarily work without that SSO setting.

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [September 14, 2023, 9:33pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/4 "2023-09-14T21:33:04Z")

</div>

If passing `staged: true` to the admin user create call doesn’t work, I suspect we’d be open to a PR to enable that.

(It’s somewhat of a niche feature outside of your use case I think?)

Perhaps you could use the handle\_mail route to post a fake email as the user? (Oh, as @blake [suggests](https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667/4))

That would create the staged user if necessary and the topic at the same time.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [September 14, 2023, 9:43pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/5 "2023-09-14T21:43:56Z")

</div>

> [@mattdm](#):
>
> the answer is to basically inject an email and let the email handling code take care of it.

With this approach it’s going to look as though users have created topics and replies via email. Staged users can only create posts via email. You’ll get an invalid access error if you attempt to create a non-email post for a staged user via the API.

Something about the staged user approach doesn’t feel quite right to me. It might be worth polling your users to see if they have any concerns. If you could get permission from your users, you could just create active non-staged users on the mirror site, then post their topics and comments via the API.

> [@supermathie](#):
>
> If passing `staged: true` to the admin user create call doesn’t work, I suspect we’d be open to a PR to enable that.

Passing `staged: true` creates a staged user. I’m assuming a user created in this way will be able to post via email. My local dev site isn’t configured to send emails to Discourse, so I can’t test that at the moment.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 14, 2023, 11:39pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/6 "2023-09-14T23:39:08Z")

</div>

I’ve been wanting to use discourse as a generic form processor to handle contact forms. I hate contact forms, but some users would rather use a contact form them than send an email. Even if I think those people make bad decisions, I’d like to make it easier for them to contact me so they can give me money.

It would be really handy to be able to create a message that creates a staged user without having to resort to some other system to process the form and send an email.

I assume I’ll need to create a plugin to do this, but that’s my plan.

This is one of the only things I have left to figure out to get rid of wordpress.

> [@simon](#):
>
> You’ll get an invalid access error if you attempt to create a non-email post for a staged user via the API.

So maybe what I’ll do in the plugin is create the post as if it were an email. That might not be too hard on a plugin. And that would likely make it possible via am api.

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [September 15, 2023, 12:17am UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/7 "2023-09-15T00:17:39Z")

</div>

> [@pfaffman](#):
>
> It would be really handy to be able to create a message that creates a staged user without having to resort to some other system to process the form and send an email.

I reckon that you could use a Wizard ([🧙 Custom Wizard Plugin](https://meta.discourse.org/t/custom-wizard-plugin/73345)) to achieve that without too much bother. You can do anon wizards now. Happy to give you a hand if needed!

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [September 15, 2023, 1:07am UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/8 "2023-09-15T01:07:43Z")

</div>

> [@pfaffman](#):
>
> So maybe what I’ll do in the plugin is create the post as if it were an email.

Yes, this works: [Creating a staged user with an API call - #8 by blake](https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667/8).

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 15, 2023, 2:36pm UTC](https://meta.discourse.org/t/create-staged-users-via-api/279052/9 "2023-09-15T14:36:26Z")

</div>

Oh! Of course. That’s how the mail-receiver delivers mail. I think I want an endpoint that will accept arbitrary fields from whatevver form, stick them into a topic, and deliver them to me. That would keep me from having to make public an API key (scoped to just deliver mail it doesn’t seem that horrible, to have javascript process the form and then deliver it to the existing endpoint).
