API POST for New User

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

I’m stuck and don’t know if this is merely my own error, if I’m receiving a false positive, or something else.

When I do a POST request via Postman, I get a SUCCESS message:
https://site.co/users.json?Api-Key=12345abcde12345&Api-Username=system&email=name@email.co&group_names=Alumni&custom_message=Welcome321

Message:

{
    "success": true,
    "active": false,
    "message": "<p>You’re almost done! We sent an activation mail to <b>name@email.co</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, when I check my inbox, I have no notification. Nothing in my /logs, no user.

In an attempt to troubleshoot, I do a simple GET request:

https://site.co/admin/email.json?Api-Key=12345abcde12345&Api-Username=system

I get no success reply. The reply is <!DOCTYPE html> ...

So I manually go to https://site.co/admin/email.json and see:
{"delivery_method":"smtp","settings":[{"name":"address","value":"smtp.sendgrid.net"},{"name":"port","value":2525},{"name":"user_name","value":"apikey"},{"name":"authentication","value":"plain"},{"name":"enable_starttls_auto","value":true}]}

Any ideas what I can try? My outcome is being able to invite a new user, added to a Group, via Webhook/POST.

You’ll need to move the API key into the header.

A long time ago I solved what I think is your problem by creating, deactivating, and activating the user.

1 Like

Your script is Javascript (jQuery). Using type="application/json" is generally for JSON in the script not Javascript. Here is an example:

<script id="data" type="application/json">
{"org": 10, "items":["one","two"]}
</script>

My understanding is your script tags should look like this:

<script>
$.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>

For Javascript, you don’t need to specify type="javascript" anymore, as this was made obsolete a while back.