Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?


In this guide we’ll cover how to secure your Flask application by validating incoming requests to your Twilio webhooks are, in fact, from Twilio.

With a few lines of code we’ll write a custom decorator for our Flask app that uses the Twilio Python SDK’s validator utility. We can then use that decorator on our Flask views which accept Twilio webhooks to confirm that incoming requests genuinely originated from Twilio.

Let’s get started!

Create a custom decorator

The Twilio Python SDK includes a RequestValidator class we can use to validate incoming requests.

Flaskビューにリクエスト検証コードを含めることができますが、これはPythonデコレーターを記述する絶好の機会です。 こうすれば、Twilioからの受信リクエストを受け付ける全ルートで検証ロジックを再利用できます。




        To validate an incoming request genuinely originated from Twilio, we first need to create an instance of the RequestValidator class using our Twilio auth token. After that we call its validate method, passing in the request’s URL, payload, and the value of the request's X-TWILIO-SIGNATURE header.

        That method will return True if the request is valid or False if it isn’t. Our decorator then either continues processing the view or returns a 403 HTTP response for inauthentic requests.

        Use the Decorator with our Twilio Webhooks


              Apply a custom Twilio request validation decorator to a Flask view used for Twilio webhooks.

              Apply the request validation decorator to a Flask view

              Apply a custom Twilio request validation decorator to a Flask view used for Twilio webhooks.

              To use the decorator with an existing view, just put @validate_twilio_request above the view’s definition. In this sample application we use our decorator with two views: one that handles incoming phone calls and another that handles incoming text messages.

              メモ: Ngrokを使用したり、本番環境でお使いのスタックでSSL接続がアプリケーションより上流で終端している場合、TwilioのWebhook URLがhttp://ではなくhttps://から始まる場合、リクエストばりデーターがローカルで失敗する可能性があります。 これはFlaskアプリケーションで確認できるリクエストURLが、Twilioがアプリケーションに到達するために使用するURLと一致していないことが原因です。

              To fix this for local development with Ngrok, use http:// for your webook instead of https://. To fix this in your production app, your decorator will need to reconstruct the request's original URL using request headers like X-Original-Host and X-Forwarded-Proto, if available.

              Disable Request Validation During Testing

              もしFlaskビュー用のテストを記述している場合、Twilioリクエスト検証デコレーターを使用しているビューでテストは失敗するでしょう。 これらビューに送信するテストスイートのいかなるリクエストも、ミドルウェアの検証チェックに失敗します。

              To fix this problem we recommend adding an extra check in your decorator, like so, telling it to only reject incoming requests if your app is running in production.




                    It's a great idea to run automated testing against your webhooks to ensure that their signatures are secure. The following Python code can test your unique endpoints against both valid and invalid signatures.

                    To make this test work for you, you'll need to:

                    1. Set your Auth Token as an environment variable
                    2. Set the URL to the endpoint you want to test
                    3. If testing BasicAuth, change HTTPDigestAuth to HTTPBasicAuth
                          This sample test will test the validity of your webhook signature with HTTP Basic or Digest authentication.

                          Test the validity of your webhook signature

                          This sample test will test the validity of your webhook signature with HTTP Basic or Digest authentication.


                          Validating requests to your Twilio webhooks is a great first step for securing your Twilio application. We recommend reading over our full security documentation for more advice on protecting your app, and the Anti-Fraud Developer’s Guide in particular.

                          To learn more about securing your Flask application in general, check out the security considerations page in the official Flask docs.

                          Andrew Baker Jose Oliveros Paul Kamp Kevin Whinnery David Prothero Kat King
                          Rate this page:


                          誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。