visit
In early 2018, I embarked on a side gig with a few partners - the idea was to make ridesharing socially engaging and fun. We made a ton of mistakes and never really got the product off the launchpad. In retrospect, the biggest mistakes we made were wasting precious time in writing code for authentication and authorization, and user notifications. We learned to focus on customer value the hard way.
Every startup should consider the trade-offs of buying vs. building non-differentiated features like authentication, notifications, payments, etc. Companies like Auth0 (authentication and authorization platform), Courier (one API to design and deliver notifications across multiple channels), and Stripe (payments infrastructure for the internet) have solved these problems so you, the entrepreneur or developer, can stay laser-focused on what your users truly want out of your product.
Whether working on a side gig like me or starting a company, shipping an MVP as quickly as possible helps you get feedback much earlier in the product development cycle. Outsourcing authentication, notifications, and payments tremendously helps in rolling your MVP out the door. The project I mentioned above took months to reach the MVP stage, and outsourcing the non-core features could have had a great impact on shortening that time frame.
This post will focus on a pretty common use case that suits almost every product you will ever build. A user signs up for your product, you save the user details and send them a welcome message. We will use Auth0’s post-user registration hook and Courier’s automation feature to build this use case quickly. Let’s dive right in!
With a single “send” API invocation, you can use Courier to reach your customers across one or more channels (email, text, push, or direct message). We learned through talking to our customers that they need something more powerful than just a single “send” action more often than not. They need workflows that do multiple actions with a single invocation, like making a single API call to send an email to their users, waiting for a desired period of time (inducing a delay), and sending a follow-up email thereafter. Another example of a workflow we've heard quite a lot is scheduled notifications, like sending product updates every week. The Courier Automations API allows you to build such workflows and quite literally “automate” your notifications. We will see it in action soon :)
Let's start by creating a free account on Courier, which lets you send up to 10,000 free notifications every month. For the use case we are tackling in this blog post, we will be leveraging Courier’s Automations API. With a single API call, you’ll be able to create a new user profile inside Courier followed by a welcome message to send to this newly created user. The Automations Guide has a detailed walkthrough of all the cool features unlocked via Courier’s Automations API. Today, we will be invoking automation that has two steps - update-profile and send.
This step essentially takes any profile JSON object with a unique ID (recipient ID) and saves it inside Courier. It stays as a permanent record inside Courier that you can further use to send notifications, add it to a list of users/profiles, customize preferences such as opt-in/opt-out, and so on. In our use case, this would originate from Auth0. Here’s how it would look:
{
"action": "update-profile",
"recipient_id": "<RECIPIENT_ID>",
"profile": {
// User properties from Auth0
// plus any more properties you want to attach to the user
},
"merge": "none"
}
<RECIPIENT_ID>
= A unique ID that you can use to identify your user.
This step sends a welcome email to the user. We will be sending users an email using Sendgrid as the downstream email provider. This Setup Email using SendGrid guide walks you through sending an email notification using Courier and SendGrid. Once you have created a notification template (with email as the channel using SendGrid), you can construct a send step that looks like this:
{
"action": "send",
"profile": {
"email": "[email protected]"
},
"recipient": "<RECIPIENT_ID>",
"template": "<TEMPLATE_ID>"
}
<RECIPIENT_ID>
= User ID you are sending the notification to (same as step 1)
<TEMPLATE_ID>
= ID of the notification template you just created for welcoming the new user.
Here’s how the payload to automations/invoke endpoint will look:
{
"automation":{
"steps":[
{
"action":"update-profile",
"recipient_id":"unique-foo",
"profile":{
"email":"[email protected]",
"first_name":"Foo",
"last_name":"Bar"
},
"merge":"none"
},
{
"action":"send",
"profile":{
"email":"[email protected]"
},
"template":"AAZRHC4TDAMEHKGJHDAA9199WMVH",
"recipient":"unique-foo"
}
]
}
}
This post assumes you have a basic understanding of Auth0 and have set up an application in Auth0 before, but if not, follow the Auth0 Getting Started guide.
Let's take a step back and imagine how things would look if there were no integration between Auth0 and Courier. Every time you would have a new user signed up with Auth0, you would have to explicitly invoke Courier to create their profile and send them a welcome message. This would require writing more code in your application, deploying it reliably, and maintaining it as your application scales. Having Auth0 and Courier integrated out of the box gives you great leverage in the form of speed and agility to move fast - without having to maintain a single line of code in your infrastructure.
We want Auth0 + Courier to work together automatically - like magic. Once a new user registers for the product, we want to trigger Courier’s Invoke Automation API - this is where Auth0 Post-User Registration comes into the picture. Here’s the code that lives behind Auth0 hook and uses the Courier Node.js SDK to invoke the automation dynamically based on the user obtained via Auth0 registration.
module.exports = async function (user, context, cb) {
const { CourierClient } = require("@trycourier/courier");
const courier = CourierClient({ authorizationToken:
context.webtask.secrets.COURIER_AUTH_TOKEN });
await courier.automations.invokeAdHocAutomation({
automation: {
steps: [
{
action: "update-profile",
recipient_id: user.id,
profile: {
email: user.email,
username: user.username,
phoneNumber: user.phoneNumber
},
merge: "none"
},
{
action: "send",
profile:{
email: user.email
},
template: "AAZRHC4TDAMEHKGJHDAA9199WMVH",
recipient: user.id
}
]
}
});
cb();
};
This saves the user profile to Courier followed by sending the welcome email you configured in the template.
The profiles can be viewed in Courier UI under Data Recipients tab:
Next, the welcome email is sent to the user - it looks like how you configured it to look in Courier. To give an example of how a notification could look:
We just saw how Auth0 + Courier could help you automagically build user onboarding flow without having to maintain any code in your own system. As we all know, the best code is no code that allows us to focus our energies where they are most needed.
Of course, Courier Integrates with more than just Auth0. Just like we sent an email using Sendgrid in this blog post, you can use any of these channels - email, SMS, push, direct messaging, etc. with providers like Mailgun, Twilio, and Slack with just a single API to power your notification workflows.
Sign up for a free Courier account. You build the next big thing, and let us handle the notifications for you. 🚀
Additional References -
Author: Tejas Kumthekar