Example Keygen + Stripe integration
The following web app is written in Node.js and shows how to integrate Keygen and Stripe together using webhooks. Much more could be done to automate e.g. license revocation when a subscription is canceled, etc.
This example application is not 100% production-ready, but it should get you 90% of the way there. You may need to add additional logging, error handling, as well as listening for additional webhook events.
Running the app
First up, configure a few environment variables:
# Stripe publishable key export STRIPE_PUBLISHABLE_KEY="YOUR_STRIPE_PUBLISHABLE_KEY" # Stripe secret key (don't share this!) export STRIPE_SECRET_KEY="YOUR_STRIPE_SECRET_KEY" # The Stripe plan to subscribe new customers to export STRIPE_PLAN_ID="YOUR_STRIPE_PLAN_ID" # Keygen product token (don't share this!) export KEYGEN_PRODUCT_TOKEN="YOUR_KEYGEN_PRODUCT_TOKEN" # Your Keygen account ID export KEYGEN_ACCOUNT_ID="YOUR_KEYGEN_ACCOUNT_ID" # The Keygen policy to use when creating licenses for new users # after they successfully subscribe to a plan export KEYGEN_POLICY_ID="YOUR_KEYGEN_POLICY_ID"
You can either run each line above within your terminal session before
starting the app, or you can add the above contents to your
file and then run
source ~/.bashrc after saving the file.
Next, install dependencies with
Then start the app:
Configuring the webhooks
For local development, create an
ngrok tunnel to your
local development server:
ngrok http 8080
Next up, add the generated
ngrok URL to your Stripe and Keygen accounts to
listen for webhooks.
- Stripe: add
- Keygen: add
In a production environment, you would use your actual server info in place of the
Testing the integration
Visit the following url: http://localhost:8080 and fill out the purchase form.
Incorrect ENV variables
In case of errors, please double check all of your environment variables. If one of the variables are incorrect, it may cause API authentication issues.
Please note that this example requires that your Keygen account is set to unprotected, because this example handles user creation on the front-end. You can update this setting on your account's settings page. If you would prefer to keep your account protected, the logic for user creation would need to be moved to a server-side URL.
Here's a few things to double check when a problem arises:
- Make sure you're using the correct account ID (find yours here)
- Make sure you're using a product token or admin token (the token should start with
- Make sure you're using the correct policy ID (it should be a UUID)
- Make sure that your Stripe environment variables are correct
- Make sure all dependencies have been installed via
- Make sure you have correctly configured webhooks for both Keygen and Stripe
- Make sure that the webhook URL is accessible from the public internet via
Reach out at email@example.com if you have any questions or concerns!