API POST for New User

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?

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

https://www.php.net/manual/en/book.curl.php

1 Like

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>

API key/username in

headers: {}
1 Like

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>

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

1 Like

Trying:

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

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

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.

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. :slight_smile:

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>
1 Like

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

2 Likes

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>

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.

2 Likes

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.

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.

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.

I’m pretty stoked about the Discourse Subscriptions plugin.

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.

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.

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.

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

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.

1 Like