Introducing Autopilot, an AI coding assistant
gradient
Make Stripe webhooks more useful using AI

Make Stripe webhooks more useful using AI

Keanan Koppenhaver
Developer Advocate
Nov 15, 2023
8 min read

Everyone loves seeing a new customer notification come in from Stripe! But many times, it’s from a domain name you don’t recognize, meaning you have to actually go to your new customers website to figure out what they do and decide whether you should reach out to them directly about different ways they can use your product.

In this tutorial, we explore how you can use Airplane’s new webhooks feature to upgrade these default Stripe notifications and learn more about your customers without having to do any additional research on your own.

What are webhooks?

A webhook is an event-driven interface between two applications that allows a client-side action to be triggered by a server-side event or update. Instead of the client having to poll an API endpoint on the server to check for new data or an instance of a specific event, the server notifies the client when new data is available or a relevant event occurs. For example, instead of polling the Stripe API to check if you have any new customers, Stripe’s webhook system can notify your application when a new customer object is created. This helps make client applications more efficient and ensures that action is only taken when necessary as opposed to spending time and resources polling for updates that may not exist.

Webhooks generally have a payload, which contains all the relevant information about the event that’s being reported about. This payload is sent to an endpoint you specify inside your application, which is then responsible for taking any necessary action. Let’s take a look at a more practical example.

Let’s say you want to update the status of a user inside your application to limit their account if their subscription has expired in Stripe. One way to accomplish this would be to call the Stripe API every time that user logs in to check the status of their account. Or you could set up a webhook so that Stripe will notify your application whenever a user’s subscription has expired, allowing you to update their status inside your application accordingly. The latter is the approach we’re going to be taking in this example.

Using webhooks in Airplane

Airplane is a platform that helps developers transform scripts, queries, and APIs into custom UIs and workflows to fit the unique needs of their business. At the core of Airplane are Tasks, functions that anyone on your team can execute. They can be SQL queries, custom TypeScript or Python scripts, or even a wrapper around a REST API endpoint. And with the recent launch of webhooks, Airplane tasks can be triggered from an external source, which means we can trigger an Airplane task when a user is created in Stripe and use Airplane to do some additional customer research.

Before we get started, make sure you have registered for a free Airplane account and have downloaded and installed the Airplane CLI.

We’ll be writing this task in Python, so after running airplane init from the command line and selecting Task and Python as our configuration options, we can specify our need for a webhook right in the task definition.

python

Once you run airplane deploy and your deployment completes, accessing this task inside Airplane will provide you with the webhook URL. Click to copy this so we can configure it to receive events from Stripe.

Note that we’ve hidden the last portion of the webhook URL in the above screenshot. Adding a webhook to a task will create a publicly accessible URL, which means anyone with the webhook URL will be able to trigger the task, so treat the URL as a secret.

Configuring webhooks in Stripe

Once we have our webhook URL, we can either create or log in to our Stripe account and head over to the webhooks page of the Stripe dashboard. Because we’re just testing things out for now, make sure your Stripe dashboard is set to Test Mode by selecting the toggle in the upper right hand corner of the page.

Make sure Test mode is enabled in Stripe

Clicking on the Add Endpoint button will allow you to add an endpoint, a description as well as specify the events you’re interested in receiving notifications for. For our example, we only care about the customer.created event. Once you have set up your webhook, click Add endpoint again to save it.

And with that configuration, Stripe will now notify our Airplane task each time a new Customer is created!

Slack + Stripe

The second webhook connection we want to create is between Slack and Stripe. This will allow Stripe to post a notification in Slack whenever a new customer is created. There is an official Stripe Slack app that you can add to your Slack team. After going through the authorization process, Slack will give you an endpoint URL to add to Stripe in the same way that we just added an endpoint for our Airplane Task. This will make sure that Stripe can send notifications to Slack for new customers.

An example of a new customer notification in Slack

Integrating Langchain to do customer research

Now that we have Stripe connected to Airplane and Slack, we need to actually write the logic in Airplane to do additional research on new customers, based on the domain name of their email address. For this task, we’ll use Langchain, a powerful library that leverages AI to perform tasks in Python.

First, since we’ll be using OpenAI’s API to do the actual processing, we’ll need to register for an OpenAI API key and set that up as a config variable inside Airplane named OPENAI_API_KEY. Next, we can update our task definition to include this config variable, so that we can use it inside our task.

python

Before we get started writing our code, we need to install the necessary dependencies for this project.

shell

For the actual task itself, we want to do three things: get the new customer’s domain, use Langchain to summarize their homepage and then post a message to Slack to correspond to our new customer notification from Stripe.

Get the new customer’s domain name

Getting the domain name for the new customer is relatively straightforward. If there is a list of domain names (such as gmail.com or other non-corporate domains) that you want to exclude from processing, this would be the place to do that.

python

In this snippet, we make sure we are responding to the correct Stripe event (customer.created) and then select the second half of the customer’s email address to get the domain we want to research.

We are keeping this example short on purpose, but it’s a good idea to make sure that requests to your webhook endpoint are actually coming from Stripe and that you have all the proper security measures in place. For more information on how to do this, take a look at the Stripe demo on the webhooks documentation page.

Use Langchain to summarize the homepage

Now that we have the customer’s domain name, we can use WebBaseLoader and the summarize chain inside of Langchain to load their website and generate a paragraph summary of what their company does. It’s important to note here that the success of this portion of the task really depends on each company’s homepage. For example, the Apple.com homepage is largely images and doesn’t provide a lot of information about what the company does. This means our task will be less successful at providing context around a customer signing up with an Apple.com email address. However, for most companies, this technique works quite well.

python

This portion of the script loads the homepage of our customer’s domain into memory and runs it through the summarization chain inside of Langchain to generate our paragraph summary. For example, this is the summary it generates for Patreon.com:

Send a Slack notification with our customer research

The last step in the process is to send a Slack notification with our customer research so that anyone viewing the Stripe notification can also see some more information about that particular new customer. After connecting your Slack team to Airplane, we can accomplish this notification process in one block of code.

python

Replace #customer-research with the name of the Slack channel that your new customer notifications go into and feel free to customize the text that’s actually displayed as part of the notification. With this piece in, our complete script looks something like this:

python

When a new customer signs up for Stripe (or you manually create a new customer in Test mode), you should see both the Stripe notification and, after waiting a few seconds for the Airplane task to run, an Airplane notification with more information about that new customer’s company!

An example of a Stripe new customer notification with some additional contextual information

Building internal tools with more context using Airplane

With Airplane, workflow improvements like this are easier than ever to get up and running. With a flexible, code-first architecture and the ability to share the tools you’ve built with your teammates, Airplane is a powerful platform that helps you build quickly with the ability to scale.

Whether you’re using built-ins to integrate AI into your workflow, connecting to your existing databases or taking advantage of our growing library of templates, Airplane is the easiest way to build and maintain a library of internal tools.

Sign up for a free account today and get started building with Airplane in minutes.

Share this article:
Keanan Koppenhaver
Developer Advocate
Keanan is a Developer Advocate at Airplane. He's passionate about teaching other developers and helping them level up their skills.

Subscribe to new blog posts from Airplane.