[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-01 UTC。"],[],[],null,["# Hosting a webhook target\n\nThis guide shows how to host a webhook target in a Knative serving service.\n\nCloud Run functions vs Knative serving\n--------------------------------------\n\n[Cloud Run functions](/functions/docs) and Knative serving both provide good solutions for\nhosting your webhook targets. Generally, Cloud Run functions is quick to set up,\ngood for prototyping, and ideal for lower volume workflows. Knative serving\nprovides more flexibility and is able to handle larger volumes with concurrency.\n\nUse Knative serving if:\n\n- You're using languages or runtimes not supported in Cloud Run functions\n- You want longer request timeouts (up to 15 minutes)\n- You're expecting large volume and need concurrency (80 concurrent requests or more per container instance)\n\nCreating a webhook target in Knative serving\n--------------------------------------------\n\nUsing Knative serving, you can define a webhook target in any language you\nchoose. You only need to create an HTTP endpoint that can accept the data.\nTypically this is done with a `POST`, for example: \n\n @app.route('/', methods=['POST'])\n def index():\n data = request.get_json()\n\nIn the above example, the index page of the URL is configured to accept only\n`POST` requests and expects data to be delivered via a JSON payload.\n\nIntegrating with the webhook provider\n-------------------------------------\n\nMost services that provide HTTP callbacks require you to verify URL ownership.\nThis is usually done by sending some kind of token, message, or secret and\nexpecting a valid response. You'll need to obtain these requirements from the\nservice provider. Using the same example above, this could look like: \n\n def index():\n data = request.get_json()\n return data['challenge']\n\nAfter the provider verifies your ownership, you'll need to add authorization on\nyour end as well.\n\nAuthorizing requests\n--------------------\n\nA webhook target is an open and public URL. Most services provide a token or a\nsecret to ensure that the incoming requests are from authorized services.\nBecause the URL is public, you cannot prevent malicious attempts to send data to\nthe webhook target. However, using tokens or secrets ensures you only process\ndata from authorized sources.\n\nIn order to verify the request, you can either\n[configure secrets](/anthos/run/docs/configuring/using-secrets), or to store your copy of the secret\neither as an environment variable or using some kind of key management system.\n\nWhen storing your copy of the secret as an environment variable, each request should have a secret or token in the request headers or the JSON\npayload, and you must check it to ensure the source is valid. \n\n def index():\n request_secret = request.headers['Secret']\n if request_secret != os.environ['SECRET']:\n return ('Unauthorized', 401)\n\nIf the webhook provider does not support a secret or other authentication\nmechanism, anyone with the URL of your webhook target will be able to send\nmessages. In this case, your webhook implementation should be safe to expose to\nthe public internet.\n\nResponding to requests\n----------------------\n\nMost services require you to respond to a request within a set amount of time,\nas specified by the service. Some webhooks have built-in retry methods if\nthere is an error response, such as an HTTP status code of 4xx or 5xx, so you'll\nneed to return a successful status code (2xx) to let the service know the event\nwas processed properly. \n\n def index():\n data = request.get_json()\n return ('', 200)\n\n### Timeouts\n\nBoth Knative serving and the webhooks provider have timeouts. The\nshorter of the two will apply to your application. If your data processing\nexceeds the time allotted by either Knative serving or the webhooks\nprovider, you'll need to use a product that allows you to complete your\nprocessing asynchronously, such as\n[Pub/Sub](https://cloud.google.com/pubsub/docs/) or\n[Cloud Tasks](https://cloud.google.com/tasks/). These products allow you\nto quickly hand off the data, immediately return a success response to the\nwebhooks provider, and continue the processing without the timeout concern.\nThese are also good options for handling failures and retries.\n\nCommon webhooks patterns\n------------------------\n\nWhat's next\n-----------\n\n- Learn more about webhooks (HTTP Triggers) on [Cloud Run functions](/functions/docs/calling)\n- Set up webhooks notifications on [Google Cloud Observability](/monitoring/support/notification-options#webhooks)\n- Send Pub/Sub messages to a webhook using [push subscriptions](/pubsub/docs/push)\n- Fulfill actions on [Dialogflow](/dialogflow/docs/fulfillment-overview) with webhooks"]]