API to create a user without sending out activation email?


(Nilesh Trivedi) #1

I am using Discourse with my app using shared cookie authentication. My app does the registration and authentication and Discourse (using a custom CurrentUserProvider) takes the user_id from the session cookie.

How do I get my app to tell Discourse to create a new user with just username and email? This is not a SSO setup so the official API will not suit me apparently.

I don’t want to send out activation email because my app authenticates the users with social logins so email is already verified.


Disable confirmation email after account creation
Adding users through API without confirming email
(Blake Erickson) #2

I’m pretty sure that an email does not get sent when you pass in the

active=true

flag when creating a user.


(Nilesh Trivedi) #3

Thank you. This worked. I had to modify the CURL command a bit:

curl -X POST --data "name=dave&username=dave&email=dave@example.com&password=P@ssword&active=trueapi_key=test_d7fd0429940&api_username=test_user" http://localhost:3000/users

(Daniel Brief) #4

Hi @nileshtrivedi & @blake,
I’m trying to do something like Nilesh did here, but it’s only partially working. If I pass “active=true” when creating the user, it’s true that he doesn’t get an activation email, but he can’t login. When trying to login he gets a message

"You can't log in yet. We previously sent an activation email to you at joe@example.com. Please follow the instructions in that email to activate your account. Click here to send the activation email again." 

So the “active=true” seems to only postpone the need for the user to verify his email (and the message will confuse the user since Discourse did not previously send an email).

Looking in the source code, it looks like the problem is because the user is “active” but not “email_confirmed”. Did this happen for you too? Did you find a way to work around it? To confirm the email via API?

Any insight would be appreciated!


Creating Active Users via the API gem
(Joshua Frank) #5

I have a similar requirement. Did you ever resolve your question?


Sso and api sign up
(Mike Taber) #6

I addressed this in a different thread here: Creating Active Users via the API gem

The short answer is that you need to create the user and set the account to be active. Then deactivate the account and immediately reactivate the account. It’s messy, but it works.


Adding users through API without confirming email
(Joshua Frank) #7

Oh, that is yucky indeed. But I’ll give it a shot. Thanks.


(Cheng Zheng) #9

I got this working (2018-4-29)

Create account via API (skip email confirm)

Step 1: Create account

curl -X POST --data "name=john&username=john&email=101@qq.com&password=thistest12A&active=true" http://localhost:3000/users?api_key=d35d2df31baca42878108abe5748fc95c5ac2729624862e8104112039e68f27b&api_username=system

You need to change these to what you want, here is:

  • Username: john
  • Email: 101@qq.com
  • Password: thistest12A

don’t forget to change api_key, api_username
you can create API key here: http://localhost:3000/admin/api/keys

Step 1 would return:
{"success":true,"active":true,"message":"Your account is activated and ready to use.","user_id":5}

you can see “user_id”:5

Step 2: take that user_id, Deactive Account

curl -X PUT "http://localhost:3000/admin/users/5/deactivate.json?api_key=d35d2df31baca42878108abe5748fc95c5ac2729624862e8104112039e68f27b&api_username=system"

Step 3: Active Account

curl -X PUT "http://localhost:3000/admin/users/5/activate.json?api_key=d35d2df31baca42878108abe5748fc95c5ac2729624862e8104112039e68f27b&api_username=system"

it would work

now you can login

  • Username: john
  • Email: 101@qq.com
  • Password: thistest12A