Skip to content

Commit

Permalink
feat: Add webhook flask example (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish authored Sep 8, 2023
1 parent 69508f5 commit 56b2aba
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions with-webhooks-python-flask/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
21 changes: 21 additions & 0 deletions with-webhooks-python-flask/LICENSE
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.
53 changes: 53 additions & 0 deletions with-webhooks-python-flask/README.md
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
28 changes: 28 additions & 0 deletions with-webhooks-python-flask/app.py
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)
2 changes: 2 additions & 0 deletions with-webhooks-python-flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
svix

0 comments on commit 56b2aba

Please sign in to comment.