# Creating a staged user with an API call

**URL:** https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667
**Category:** Development
**Tags:** rest-api
**Created:** [4월 5, 2018, 7:19오후 UTC](https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667 "2018-04-05T19:19:10Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [4월 5, 2018, 8:59오후 UTC](https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667/2 "2018-04-05T20:59:43Z")

</div>

> [@dmccann](#):
>
> 1. create staged user

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.

> [@dmccann](#):
>
> 1. generate API key for that user

This is also possible

* * *
Here is a rough example of how to create a user, active them, and generate an api key for them.

```plaintext
    def create_user
      user = {
        name: example1,
        email: "example1@example.com",
        password: "ZvAmmkcSWQfsPQLBksg7wK59",
        username: example1,
        active: "false",
        approved: "true",
        approved_by_id: 1,
        approved_at: DateTime.now
      }

      new_user = @client.create_user(user)
      id = new_user['user_id']
      @client.activate(id)
      uri = URI.parse(@config.full_discourse_url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      request = Net::HTTP::Post.new("/admin/users/#{id}/generate_api_key?api_key=#{@client.api_key}&api_username=#{@client.api_username}")
      response = http.request(request)
      result = JSON.parse(response.body)
    end

```

Another option instead of generating an api key for each user is you can just instantiate a new discourse client using the same admin api key and just specify the new username:

```plaintext
client = DiscourseApi::Client.new("http://127.0.0.1:3000")
client.api_key = "a71cb5058c6be27e42806ad788bc7b0008af9c15170d1be1827a24c8e8334107"
client.api_username = "system"

... create user here...

client2 = DiscourseApi::Client.new("http://127.0.0.1:3000")
client2.api_key = "a71cb5058c6be27e42806ad788bc7b0008af9c15170d1be1827a24c8e8334107"
client2.api_username = example1

... create post here ...

```

---

_[View the full topic](https://meta.discourse.org/t/creating-a-staged-user-with-an-api-call/84667)._
