Available in🇮🇳 India🇸🇬 Singapore🇺🇸 United States
Webhooks (Web Callback, HTTP Push API or Reverse API) automatically notify your application when specific events occur. Instead of continuously polling APIs to check for updates, webhooks push notifications directly to your server when events happen.
Webhooks vs APIs
Here is how webhooks compare to traditional API polling:How Razorpay Webhooks Work
When you subscribe to webhook events, Razorpay sends an HTTP POST request with JSON payload to your configured endpoint URL whenever those events are triggered. Suppose you have subscribed to theorder.paid webhook event, you will receive a notification every time a user pays you for an order, in the configured endpoint URL.
Use Cases
There can be multiple uses for webhook events. Two of these are listed below.Notifications on Failed Payments
Notifications on Failed Payments
When a payment attempted by your customer fails, we receive the failed payment status from the bank. This payment gets recorded in our system as Failed.Suppose you have enabled the
payment.failed webhook, you will receive a notification from us about the failed payment. You can then further analyse this payment and notify your customer about the failure.Setup and Configuration
- You can set up webhooks from your Dashboard and configure separate URLs for Live mode and Test mode. Know more about setting up Payment webhooks.
- A Test mode webhook receives events for your test transactions. Know more about testing webhooks.
- Webhook URLs must use ports 80 or 443 only.
- Ensure Razorpay webhook IPs are whitelisted on your server. Even if your server accepts all incoming requests, webhooks may still be blocked by cloud security groups or network configurations. Refer to Razorpay IPs and Certificates for the complete list of webhook IP addresses.
Idempotency
There could be scenarios where your endpoint might receive the same webhook event multiple times. This is an expected behaviour based on the webhook design. To handle duplicate webhook events:- You can identify the duplicate webhooks using the
x-razorpay-event-idheader. The value for this header is unique per event. - Check the value of
x-razorpay-event-idin the webhook request header. - Verify if an event with the same header is processed at your end.
Deactivation
All webhook responses must return a status code in the range2XX within a window of 5 seconds. If we receive response codes other than this or the request times out, it is considered a failure.
On failure, a webhook is re-tried at progressive intervals of time, defined in the exponential back-off policy, for 24 hours. If the failures continue for 24 hours, the webhook is disabled. You need to enable the webhook from the Dashboard after fixing the errors at your end. Know more about enabling Webhooks.
Handy TipsWhen a webhook gets disabled, you receive an email notification on the email id you configured while setting up the webhooks.
Setup Webhooks
To set up webhooks:- Log in to the Dashboard and navigate to Accounts & Settings.
- Click Webhooks under Website and app settings.
- Click the + Add New Webhook button.
-
In the Webhook Setup pop-up page:
- Enter the URL where you want to receive the webhook payload when an event is triggered. We recommend using an HTTPS URL.
Handy Tips
- You can set up to 30 URLs to receive Webhook notifications. Webhooks can only be delivered to public URLs.
- If your URL contains
razorpayas a domain, you will not be able to add the URL and will receive an error. - If you attempt to save a localhost endpoint as part of a webhook setup, you will notice an error. Know more about testing Webhooks on an application running on localhost.
- Enter a Secret for the webhook endpoint. The secret is used to validate that the webhook is from Razorpay. Do not expose the secret publicly. Know more about how to validate webhooks.
Handy Tips
- When setting up the webhook, specify a secret. Use this secret to validate that the webhook is from Razorpay. Entering the secret is optional but recommended. The secret should never be exposed publicly.
- The webhook secret does not need to be the Razorpay API key secret.
- In the Alert Email field, enter the email address to which the notifications should be sent in case of webhook failure. You will receive webhook related notifications like failures, deactivation and so on.
- Select the required events from the list of Active Events.
- Click Create Webhook. After you set up a webhook, it appears on the list of webhooks.
- You can select the webhook and click Edit to make more changes.
Validation
When your webhooksecret is set, Razorpay uses it to create a hash signature with each payload. This hash signature is passed with each request under the X-Razorpay-Signature header that you need to validate at your end. We provide support for validating the signature in all of our language SDKs.
If you have changed your webhook secret, remember to use the old secret for webhook signature validation while retrying older requests. Using the new secret will lead to a signature mismatch.
X-Razorpay-Signature
: The hash signature is calculated using HMAC with SHA256 algorithm; with your webhook secret set as the key and the webhook request body as the message.
You can also validate the webhook signature yourself using a HMAC as shown below: