0
0
Rest APIprogramming~30 mins

Webhook registration endpoint in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Webhook Registration Endpoint
📖 Scenario: You are building a simple web service that allows users to register webhooks. A webhook is a URL where your service can send notifications when certain events happen.Users will send a POST request with their webhook URL and event type to register their webhook.
🎯 Goal: Create a webhook registration endpoint that accepts JSON data with a url and event, stores the webhook in a list, and returns a confirmation message.
📋 What You'll Learn
Create a list called webhooks to store registered webhooks.
Create a variable called allowed_events containing the strings 'order_created' and 'order_shipped'.
Create a POST endpoint /register-webhook that accepts JSON with url and event fields.
Validate that event is in allowed_events before storing.
Return a JSON response confirming registration with the webhook url and event.
💡 Why This Matters
🌍 Real World
Webhook registration endpoints are used in many web services to allow external systems to receive real-time notifications about events.
💼 Career
Understanding how to create and validate REST API endpoints is essential for backend developers and anyone working with web services.
Progress0 / 4 steps
1
Create the initial data structure
Create a list called webhooks to store webhook registrations.
Rest API
Need a hint?

Use square brackets [] to create an empty list named webhooks.

2
Add allowed events configuration
Create a list called allowed_events containing the strings 'order_created' and 'order_shipped'.
Rest API
Need a hint?

Use a list with the two event strings exactly as shown.

3
Create the webhook registration endpoint
Create a POST endpoint /register-webhook using Flask that accepts JSON data with url and event. Validate that event is in allowed_events. If valid, append a dictionary with keys 'url' and 'event' to webhooks. Return a JSON response with keys 'message', 'url', and 'event' confirming registration.
Rest API
Need a hint?

Use Flask decorators and request.get_json() to get JSON data. Check if event is allowed before adding to webhooks.

4
Run the app and test output
Add the code to run the Flask app if this file is the main program. Then, print webhooks after registration to verify the webhook was stored.
Rest API
Need a hint?

Use the standard Flask app run block. Printing webhooks can be done in the endpoint or after running tests.