-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add webhook flask example (#61)
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Plus Five Five, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Resend Webhook with Flask | ||
|
||
This example shows how to use Resend with [Flask](https://flask.palletsprojects.com/en/2.3.x/). | ||
|
||
## Prerequisites | ||
|
||
To get the most out of this guide, you’ll need to: | ||
|
||
* [Create an API key](https://resend.com/api-keys) | ||
* [Verify your domain](https://resend.com/domains) | ||
* Install [Ngrok](https://ngrok.com/). | ||
* Install `virtualenv` by running `pip install virtualenv` | ||
|
||
## Instructions | ||
|
||
1. Create and activate a new virtual env with: | ||
|
||
```sh | ||
virtualenv venv | ||
source venv/bin/activate | ||
``` | ||
|
||
2. Install dependencies: | ||
|
||
```sh | ||
pip install -r requirements.txt | ||
``` | ||
|
||
3. Run your Flask API with | ||
|
||
```sh | ||
python app.py | ||
``` | ||
|
||
4. Expose your local Flask API through Ngrok by running. | ||
|
||
```sh | ||
ngrok http 5000 | ||
``` | ||
|
||
This will get you a public url similar to: `https://b009-2804-1b0-f385-8a68-98ee-361f-7cd1-3195.ngrok-free.app` | ||
|
||
5. Add your webhook url on Resend [Webhook Dashboard](https://resend.com/webhooks). | ||
|
||
`https://b009-2804-1b0-f385-8a68-98ee-361f-7cd1-3195.ngrok-free.app/webhook` | ||
|
||
Note the trailing `/webhook` url path. | ||
|
||
6. Send some emails and see the requests coming through from Resend. | ||
|
||
## License | ||
|
||
MIT License |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os | ||
import json | ||
from flask import Flask, jsonify, request | ||
from svix.webhooks import Webhook, WebhookVerificationError | ||
|
||
app = Flask(__name__) | ||
|
||
if not os.environ["WEBHOOK_SECRET"]: | ||
raise EnvironmentError("WEBHOOK_SECRET is missing") | ||
|
||
|
||
@app.route('/webhook', methods = ['POST']) | ||
def index(): | ||
headers = request.headers | ||
payload = request.get_data() | ||
|
||
try: | ||
wh = Webhook(os.environ["WEBHOOK_SECRET"]) | ||
msg = wh.verify(payload, headers) | ||
except WebhookVerificationError as e: | ||
return ('', 400) | ||
|
||
print(msg) | ||
return jsonify(success=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
flask | ||
svix |