# API POST for New User

**URL:** https://meta.discourse.org/t/api-post-for-new-user/148676
**Category:** Development
**Tags:** rest-api
**Created:** [April 20, 2020, 10:53pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676 "2020-04-20T22:53:30Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 20, 2020, 10:53pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/1 "2020-04-20T22:53:30Z")

</div>

I am attempting to make a POST request via API to create a new user.

Use case: Creating a new user once a credit card form is submitted on a Wordpress site. Ultimately, this will be fired from Google Tag Manager using Javascript.

To test this, I have written the following, saved it as a PHP file, and uploaded the file to my server. I get an error of `Parse error: syntax error, unexpected 'POST' (T_STRING) in /nas/content/live/username/newuser.php on line 2`

```
<?php 
curl -X POST "https://site.com/invites" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: 123456789012345678901234567890" \

{
"email": "test@gmail.com",
"group_names": "Paid_Members",
"custom_message": "Custom message here"
}

?>

```

What am I missing in this POST? Do I need to add `Accept: application/json` to this?

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [April 20, 2020, 11:13pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/2 "2020-04-20T23:13:17Z")

</div>

If you want to test with PHP, you will need to use ` curl_*` functions.

> **[PHP: cURL - Manual](https://www.php.net/manual/en/book.curl.php)**
>
> Client URL Library

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 20, 2020, 11:18pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/3 "2020-04-20T23:18:20Z")

</div>

Thanks. I’m realizing I don’t want to use PHP at all; I just want to use Javascript as that’s what I’ll use with GTM.

Perhaps this is getting me closer? Though I don’t know where to add the API key in this script:

```
<script>
$.ajax({
method: "POST",
url: "https://site.com/invites",
data: { email: "foo@gmail.com", group_names: "Paid_Members" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
</script>

```

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [April 20, 2020, 11:21pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/4 "2020-04-20T23:21:27Z")

</div>

API key/username in

```plaintext
headers: {}

```

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 20, 2020, 11:27pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/5 "2020-04-20T23:27:30Z")

</div>

I have the following, though GTM is flagging it for errors on line 5. Can you help me see what I’m missing?

```
 <script>
 $.ajax({
 method: "POST",
 url: "https://site.co/invites",
 headers: {Api-key: "1234567890987654321234578"},
 data: { email: "test@gmail.com", group_names: "Paid_Members" }
 })
 .done(function( msg ) {
 alert( "Data Saved: " + msg );
 });
 </script>

```

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [April 20, 2020, 11:28pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/6 "2020-04-20T23:28:21Z")

</div>

You miss `Api-Username` associated to the API key you created.  
About the errors, you need provide more information.

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 20, 2020, 11:32pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/7 "2020-04-20T23:32:52Z")

</div>

Trying:

`headers: { Api-Username: "System", Api-Key: "123123958712389478923478923" },`

“Error at line 5, character 15: Parse error. ‘}’ expected”

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [April 20, 2020, 11:36pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/8 "2020-04-20T23:36:43Z")

</div>

Missing `'` around keys. And unless you renamed, ìt should be `system`.

I would recommend you learn some JavaScript base first before doing tasks you can’t handle yet.

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 21, 2020, 12:13am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/9 "2020-04-21T00:13:06Z")

</div>

Apologies if I look like someone who knows what he’s doing! I appreciate your help, @Arkshine and will continue looking for how to solve this.

This is absolutely a task I am under-prepared for, yet I am trying. 🙂

I was able to get the request to save by putting `Api-Username` and `Api-Key` inside quotes. Thank you Arkshine. I then executed the script via GTM then check my Discourse logs without anything to show.

No new user is being generated.

Any thoughts?

Here’s my final script in GTM:

```
<script type="text/javascript">
$.ajax({
method: "POST",
url: "https://test.co/invites",
headers: { "Api-Username": "system", "Api-Key": "1234556" },
data: { email: "test@gmail.com", group_names: "Paid_Members" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
</script>

```

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [April 21, 2020, 12:37am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/10 "2020-04-21T00:37:46Z")

</div>

I think you misunderstood the API. This endpoint is not to create an user. It will generate an invite link and send it to the user’s email. Once clicked, the user will be able to register.

To create manually an user, It should be this endpoint: [Discourse API Docs](https://docs.discourse.org/#tag/Users/paths/~1users/post)

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 21, 2020, 1:11am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/11 "2020-04-21T01:11:11Z")

</div>

Well that was foolish of me. Thanks, Arkshine.

Feeling close, though I am not getting any changes in my Discourse logs.

I’ve tried `type: "POST",` as well without luck.

```
<script type="application/json">
$.ajax({
method: "POST",
url: "https://test.co/users",
headers: { "Api-Username": "system", "Api-Key": "12345" },
data: { 
"name": "Bob Dingo",
"email": "grow@test.co",
"password": "Success101",
"username": "BobDingo",
"active": true,
"approved": true,
"user_fields[1]": "string"
}

});
</script>

```

---

<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: [April 21, 2020, 1:24am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/12 "2020-04-21T01:24:35Z")

</div>

I recommend that you take a step or two back to describe the problem you’re trying to solve. It’s not clear that the path you’re taking is a good solution.

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 21, 2020, 1:31am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/13 "2020-04-21T01:31:34Z")

</div>

Good idea. I want to create a new user in Discourse, added to two Groups, when someone completes a payment form on my Wordpress site.

My plan is to pass the name/email variables from the form to the Thank You page, then send the buyer an email with their new login.

Google Tag Manager can pull the name/email variables from the URL, then add to the script as posted above.

Is there a better way to do this? I do not want to do SSO with [WP Discourse](https://github.com/discourse/wp-discourse).

---

<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: [April 21, 2020, 1:48am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/14 "2020-04-21T01:48:20Z")

</div>

> [@waffleslop](#):
>
> I want to create a new user in Discourse, added to two Groups, when someone completes a payment form on my Wordpress site

Why not use sso and one of several WordPress subscription managers that are documented here? How to update group membership from WordPress is well documented.

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 21, 2020, 2:12am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/15 "2020-04-21T02:12:08Z")

</div>

Alright, I’m struggling to find the right approach. I want to keep my forum on discourse for all the benefits AND I want to provide a simple course with 7 modules dripped over 14 weeks.

With your Feverbee Theme Component, the layout looks great.

Previously, I ran the course in cohorts and just had folks pay, then I would manually create their Discourse user. That was fine, but now I want a more “open enrollment.”

I looked into Paid Membership Pro but don’t love the idea of using a Wordpress site just to manage the membership.

I used Memberful for a few months, but their 4.9% transaction fee plus monthly fee is a bit extreme.

Discourse Subscriptions looks good, though I believe I’m not able to show the Plan pages to folks who are not logged in (which they wouldn’t be, if they were not already customers).

My idea to do an API POST to create a new member and add them to a Group is a fine tie-over fix while I drive more members. Once I have more members, I thought I could have some custom coding done.

Bonus issue: I have an iOS app I compiled from the repo and would prefer not renting a virtual machine again to re-compile with a new URL if I have to use SSO from a WP site. I might be wrong on having to change the app config file; I haven’t done this yet.

Wordpress SSO + Paid Membership Pro seems like the only option for actual member management.

---

<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: [April 21, 2020, 5:25pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/16 "2020-04-21T17:25:50Z")

</div>

I’m pretty stoked about the [Discourse Subscriptions plugin](https://meta.discourse.org/t/discourse-subscriptions-plugin/140818).

> [@waffleslop](#):
>
> I believe I’m not able to show the Plan pages to folks who are not logged in (which they wouldn’t be, if they were not already customers).

That seems like a solvable problem, perhaps by paying for the functionality or perhaps by having part of your site that’s accessible to people who haven’t yet paid.

---

<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: [April 21, 2020, 11:16pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/17 "2020-04-21T23:16:45Z")

</div>

I’ve played with the Subscriptions plugin a bit today. I think that what I’d recommend is that you have some free content on your site so that people have a reason to create an account before purchasing a course. I think this is a pretty decent solution to making the plan pages available to non-subscribers. It makes sense that they’d want to see a bit of the content before paying.

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 21, 2020, 11:31pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/18 "2020-04-21T23:31:55Z")

</div>

Good insight, but my buyer experience here is quite different. For this, it’s more of a direct sale, where the Discourse forum is part of the product.

I need a solution where I can send a lead to a checkout form they can complete without needing them to create a Discourse user first.

---

<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: [April 21, 2020, 11:33pm UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/19 "2020-04-21T23:33:35Z")

</div>

Yeah. You need the subscription thing to be tied in to the new user modal.

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [April 22, 2020, 12:14am UTC](https://meta.discourse.org/t/api-post-for-new-user/148676/20 "2020-04-22T00:14:13Z")

</div>

My thought is that just getting this API POST to work will give me a short-term solution while I on-board customers and can afford custom development.

My Google Tag Manager tag is firing, though nothing is showing inside Discourse. If I can get GTM to create a new user and set the correct Group, my solution is 70% long-term, and 100% short-term.

Any thoughts to what could be wrong with this?

```
<script type="application/json">
$.ajax({
method: "POST",
url: "https://site.co/invites",
headers: { "Api-Username": "system", "Api-Key": "12345" },
data: { 
"email": "grow@site.co",
"group_names": "Alumni",
"custom_message": "Welcome123"
}

});
</script>

```

I tried this URL in Postman:  
`https://site.co/users.json?Api-Key=12345&Api-Username=system&email=hello@domain.com&group_names=Alumni&custom_message=Welcome321`

Which gave me back this:

```
{
    "success": true,
    "active": false,
    "message": "<p>You’re almost done! We sent an activation mail to <b>hello@domain.com</b>. Please follow the instructions in the mail to activate your account.</p><p>If it doesn’t arrive, check your spam folder.</p>"
}

```

However I have received no email. I’ve checked my logs at `/logs` and nothing is listed. Checked Spam, too.

[Next page](https://meta.discourse.org/t/api-post-for-new-user/148676.md?page=2)
