This guide demonstrates how to receive a webhook from services such as Resend, Twilio, Shopify, and Stripe on your localhost development environment using the Hookdeck CLI. You’ll learn how to set up your environment, run the Hookdeck CLI, receive webhooks, and replay webhooks from the Hookdeck Console to test your code during development.
from datetime import datetimeimport jsonimport loggingfrom flask import Flask, request# Configure logging to output to the consolelogging.basicConfig( level=logging.INFO, handlers=[logging.StreamHandler()])app = Flask(__name__)app.logger.setLevel(logging.INFO)# Define a route to handle POST requests sent to /webhook@app.route("/webhook", methods=["POST"])def handle_hello_webhook(): # Log the received webhook with the current timestamp and JSON payload app.logger.info("webhook_received %s %s", datetime.now().isoformat(), json.dumps(request.json, indent=2)) # Respond with a status of ACCEPTED return { "status": "ACCEPTED" }if __name__ == "__main__": # Run the Flask app on port 3030 in debug mode app.run(debug=True, port=3030)
Step 3: Create a localtunnel with the Hookdeck CLI
In a new terminal window, run the following command to create a localtunnel:
Copy
hookdeck listen 3030 my-webhook
The output will look similar to the following:
Copy
Dashboard👤 Console URL: https://api.hookdeck.com/signin/guest?token={token}Sign up in the Console to make your webhook URL permanent.my-webhook Source🔌 Event URL: https://hkdk.events/{id}Connectionsmy-webhook -> cli forwarding to /> Ready! (^C to quit)
The cURL command output will be similar to the following:
Copy
{"status":"SUCCESS","message":"Request handled by Hookdeck. Check your dashboard to inspect the request: https://dashboard.hookdeck.com/requests/req_[id]","request_id":"req_[id]"}%
You will see the terminal running the Hookdeck CLI log the inbound webhook:
Copy
2024-07-09 19:06:46 [200] POST http://localhost:3030/webhook | https://console.hookdeck.com/?event_id={id}
You will also see the Python server log the inbound webhook:
Step 5: Trigger a test webhook from the Hookdeck Console
Open the Console URL from your terminal in your browser.Choose a Sample Webhook Provider from the list of Example Webhooks. For example, Stripe.Select a Sample Webhook Type from the list on the right. For example, invoice.created.Click Send.The Hookdeck Console will show the test webhook has been triggered. You can also inspect the webhook payload and the localhost web server response.You will see the terminal running the Hookdeck CLI log the inbound webhook:
Copy
2024-07-09 19:06:46 [200] POST http://localhost:3030/webhook | https://console.hookdeck.com/?event_id={id}
You will also see the Python server log the inbound webhook:
Step 7: Trigger and receive a webhook from an API platform
Copy the Event URL from the Hookdeck CLI output. The same URL can also be found in the Hookdeck Console.Go to the API provider platform, such as Resend, Twilio, Shopify, or Stripe, and register the Hookdeck URL as the webhook URL with the provider.Trigger a webhook from your chosen API provider, and you will see a log entry appear in the Hookdeck console.Additionally, you will see the webhook logged in the Hookdeck CLI:
Copy
2024-07-09 19:45:57 [200] POST http://localhost:3030/webhook | https://console.hookdeck.com/?event_id={id}
And by the Python server running in your local development environment:
You have successfully received a webhook on your localhost development environment using the Hookdeck CLI! You also inspected the webhook payload and server response and replayed a webhook using the Hookdeck console.This guide provided a basic example to get you started. You can now expand on this by creating a Hookdeck account, trying features such as transformations, and filtering, benefitting from functionality like configurable retries, and generally using Hookdeck as your reliable inbound webhook infrastructure.