The Discourse API can be used to automate sending invitation emails from your forum when an action on an is performed on an external application. For many applications, you can use Zapier to set this up without having to write any code.
A common use case for this is to invite users to your forum when they purchase a product
or signup on an external service. For the example in this topic I will use the Zapier WordPress integration to trigger sending an invite from Discourse when a new user signs up on a WordPress site.
Setup a trigger step on Zapier
Go to your Zapier dashboard and search for the application that you would like to trigger the invite email. In the screenshots below, I have selected WordPress as the trigger.
The only configuration that is required for this is to connect the WordPress Zap to a WordPress account, and to select the New User event to trigger the Zap.
Setup a action step to send the invite
Click the Add a Step button and then click Action/Search from the dropdown menu.
Enter ‘webhook’ into the search menu and then select Webhooks by Zapier from the actions menu. The Webhook action step will be added to your Zap’s sidebar, and a form will open that lets you choose what type of request you would like the webhook to perform.
Select POST from the form and then click Continue.
The form that opens lets you configure the POST
request that is made to Discourse. In the URL field, enter your forum’s base URL, followed by /invites
. For example, my forum’s base URL is https://demo.scossar.com
, so the full link to the invites route is https://demo.scossar.com/invites
You now need to configure the form’s Payload Type, Data, and Headers sections. All other sections of the form can be left with their default values.
The Payload Type tells Zapier what format to send the payload in. It should be set to Json.
The Data section is used to tell Discourse which email address to send the invite to, and to set an optional list of group names and a custom message. To invite a user without adding them to groups or including a custom message, enter the text email
as a data Key, and then click the ‘Insert a Field’ icon to open the field’s dropdown menu. Scroll down the list until you see the value that has been passed from your trigger app that contains the user’s email. When using WordPress as the trigger app, the completed Data section should look similar to this:
To automatically add users to groups when they accept the invite, click the Data section’s icon to add a new field. Set the Key of the field to group_names
. Set the Value of the field to a comma separated list of group names that you would like users to be added to. To include a custom message in the invite email, create another field with its Key set to custom_message
and its value set to the message you would like to send.
Note: by adding a Code step after your Zap’s trigger step it should be possible to dynamically set group names and a custom message for specific users.
The Headers section is used to authenticate your request. To configure the Headers field, you will need to generate an API key for a staff user on your site or an All Users API Key. To do that, go to your Discourse site’s Admin / API page and click the “New API Key” button.
Now, in the Headers section of the Zap, enter the following key/value pairs. Replace any values that I’ve included in brackets with values appropriate for your site. Capitalization of the key names is important.
Api-Username: <the staff username you want to send the invites>
Api-Key: <the API Key you generated>
Content-Type: application/json
The form should now look similar to the screenshot below, but with your Api-Username and Api-Key:
Click the Continue button.
You will now be able to test the integration by clicking the ‘Send Test To Webhooks by Zapier’ button. The test will be performed with data that you setup in your trigger step. You’ll see a success message if everything is correctly configured, and the user you setup in your trigger step does not yet exist on your Discourse forum, an invite email will be sent and you will see a Success message on Zapier. Once the test step is passing, click the Finish button and turn your Zap on.
For details about how to make other types of requests to the Discourse API with Zapier, see Make requests to the Discourse API with Zapier.
Last edited by @JammyDodger 2024-05-28T18:23:17Z
Check document
Perform check on document: