Sync With Webhooks
Learn how to use webhooks to automatically sync data between Lemon Squeezy and your app.
Webhooks enable Lemon Squeezy to automatically send data to your application in response to specific store events, ensuring your users remain informed and maintain access to their products.
For instance, when a subscription renewal is processed, information about the payment can be sent directly to an endpoint in your application. You can then utilize and process this data to update your application, ensuring that your users are informed of the renewal and, importantly, maintain access to your product.
Find more information about webhooks in the webhooks documentation.
Webhook events
There are many different events for which webhooks are sent, such as:
order_created
order_refunded
subscription_created
subscription_updated
subscription_cancelled
subscription_resumed
subscription_expired
subscription_paused
subscription_unpaused
subscription_payment_failed
subscription_payment_success
subscription_payment_recovered
license_key_created
license_key_updated
To find out more about webhook events, see the Webhook Events documentation.
Example webhook data
Webhook events are sent as JSON via POST requests to your endpoint.
The data
that is available in each webhook request is either a Subscription, Subscription invoice, Order or License key object. Each of our webhooks follows the same structure as below.
An example subscription_created
event’s request looks like this:
You can see in the example above that the data
object is a Subscription object.
If you added custom data to the checkout, it will be available in meta.custom_data
:
When you receive webhook events at your endpoint, it’s important to update your application’s local data, such as the order ID, renewal date, status, or payment method URL.
In addition to the primary data, each webhook includes relationships to related objects within Lemon Squeezy. You can use the provided URLs to perform sequential GET requests to access these related objects. For example, you can retrieve a product variant object by using the URL found in relationships.variant.links.related
.
Creating webhooks
Setting up webhooks is quick and easy. You can create and manage webhooks in two ways:
- From your Lemon Squeezy dashboard
- Using the Lemon Squeezy Webhooks API
From your Lemon Squeezy dashboard
- Go to Settings » Webhooks in Lemon Squeezy and click the plus icon.
- Specify the endpoint URL you want the data to be sent to in your app, then add a signing secret, which will help validate any requests to your endpoint (read on for information about signing and validating webhooks).
- The last step is to select the events you want to be sent to your app. It’s recommended to only select the events you need for your app, to limit the amount of requests hitting your service.
Using the Lemon Squeezy Webhooks API
Creating a new webhook using the API requires a simple POST
request, specifying the webhook endpoint you want data to be sent to, the events you want to subscribe to and a signing secret to keep webhook requests safe and secure.
Find out more about the webhooks API, including viewing, modifying and deleting webhooks, in the Webhooks API docs.
Looking for SDKs?
Lemon Squeezy offers official SDKs for several programming languages to make API integration even easier. Check out our available SDKs to find one that suits your development needs.
Adding an endpoint in your app
You will need an endpoint in your app that serves as the receiver for all webhook requests. This should accept POST
requests and be able to process and/or save each webhook as it is sent.
To tell Lemon Squeezy that a webhook request was recieved OK, return a HTTP 200
response. Any other response code will tell Lemon Squeezy that the webhook failed and the event will be sent again up to three more times. If your app has not returned a HTTP 200
response by the fourth attempt, the event will not be tried again automatically, though you can re-send recent webhooks from your webhooks settings.
We recommend storing webhook events locally, either in your database or a cache, even if only temporarily. This approach allows you to quickly return an HTTP 200 response and process the data outside of the webhook request. Additionally, if your app encounters an issue while processing a webhook, you will have the necessary data on hand to troubleshoot and fix the problem without waiting for the event to be resent.
Signing and validating webhook requests
To check that data sent to your specified webhook endpoint is actually from Lemon Squeezy, you should verify the signature that is sent along with the event data.
The signature is sent in the X-Signature
header and is a hash made up of the signing secret you specified when creating the webhook in your Lemon Squeezy account plus the request body.
To verify the webhook is from Lemon Squeezy you should attempt to create a matching hash and check that it matches the signature in the header.
For more information on signing webhook requests, see the Signing Requests documentation.
Simulate subscription webhooks in “Test mode”
We know building an API integration can be difficult, especially when you need to test subscription webhook events that happen in the future and can’t be easily tested via the checkout.
This is why we built a webhook simulation feature. When in “Test mode”, you can manually trigger certain subscription events from your Lemon Squeezy account.
Once you have some subscriptions in your test mode account (you can create these by purchasing products through the test mode checkout) you will see a “Simulate event” option in each subscription’s side panel. Simply select the event you want to trigger and a matching event will be sent to your endpoint.
Subscription renewal events
To test subscription renewal events (subscription_payment_*
), you need at least one renewal to have occurred on a subscription. We generate simulation webhooks based on this renewal data. For quicker testing:
- Create test subscription products with
daily
billing intervals - Purchase a subscription to one of these products
- Wait for the first renewal (which will occur the next day)
- You’ll then be able to simulate
subscription_payment_*
events for this subscription
And that’s it! You’re now equipped to set up, receive, and process webhooks from Lemon Squeezy. With this real-time data flow, your app will always be in sync with your store events. Happy coding!
For more information about webhooks, please see the Webhooks documentation and the Webhooks API documentation.