๐ ๐ฒ Creating a Secure Webhook - MUST HAVE
โก 5,599 views ยท ๐ SecOps & Security Automation
Description
How it works
This workflow demonstrates a fundamental pattern for securing a webhook by requiring an API key. It acts as a gatekeeper, checking for a valid key in the request header before allowing the request to proceed.
- Incoming Request: The
Secured Webhooknode receives an incomingPOSTrequest. It expects an API key to be sent in thex-api-keyheader. - API Key Verification:
- The
Check API Keynode takes the key from the incoming requestโs header. - It then makes an internal HTTP request to a second webhook (
Get API Key) which acts as a mock database. - This second webhook retrieves a list of registered API keys (from the
Registered API Keysnode) and filters it to find a match for the key that was provided.
- The
- Conditional Response:
- If a match is found, the
API Key Identifiednode routes the execution to the โsuccessโ path, returning a200 OKresponse with the identified userโs ID. - If no match is found, it routes to the โunauthorizedโ path, returning a
401 Unauthorizederror.
- If a match is found, the
This pattern separates the public-facing endpoint from the data source, which is a good security practice.
Set up steps
Setup time: ~2 minutes
This workflow is designed to be a self-contained example.
- Set up Credentials: This workflow uses โHeader Authโ for its internal communication. Go to Credentials and create a new Header Auth credential. You can use any name and value (e.g., Name:
X-N8N-Auth, Value:my-secret-password). Select this credential in all four webhook/HTTP Request nodes. - Add Your API Keys: Open the
Registered API Keysnode. This is your mock database. Edit the array to include theuser_idandapi_keypairs you want to authorize. - Activate the workflow.
- Test it: Use the
Test Secure Webhooknode to send a request.- Try it with a valid key from your list to see the success response.
- Change the
x-api-keyheader to an invalid key to see the401 Unauthorizederror.
For Production: Replace the mock database part of this workflow (the Get API Key webhook and Registered API Keys node) with a real database node like Supabase, Postgres, or Baserow to look up keys.
๐ Nodes Used
HTTP Request, Webhook, Filter
๐ฅ Import
Download workflow.json and import into n8n:
Workflow menu โ Import from File