[[["容易理解","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-04 (世界標準時間)。"],[],[],null,["# Hosting webhooks targets\n\nThis guide shows how to host a webhook target in a Cloud Run service.\n\nCloud Run provides good solutions for\nhosting your webhook targets. Cloud Run\nprovides more flexibility and is able to handle larger volumes with concurrency.\n\nHosting webhook targets in a Cloud Run service is ideal for the\nfollowing scenarios:\n\n- You want longer request timeouts (up to 15 minutes)\n- You're expecting large volume and need concurrency (80 concurrent requests per instance)\n\nCreate a webhook target in Cloud Run\n------------------------------------\n\nUsing Cloud Run, 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 this example, the index page of the URL is configured to accept only\n`POST` requests and expects data to be delivered through a JSON payload.\n\nIntegrate 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 [preceding example webhook target](#webhook-target), 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\nAuthorize 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](/run/docs/configuring/services/secrets), or 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\nor 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| **Note:** If you're creating webhooks to send data between multiple Cloud Run instances, use the [built in authentication](/run/docs/authenticating/service-to-service) to protect your HTTP Target. When prompted to allow [unauthenticated requests](/run/docs/authenticating/public), respond no.\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 Cloud Run and the webhooks provider have timeouts. The shorter\nof the two will apply to your application. If your data processing exceeds the\ntime allotted by either Cloud Run or the webhooks provider, you'll need to use\na product that allows you to complete your processing asynchronously, such as\n[Pub/Sub](/pubsub/docs) or\n[Cloud Tasks](/tasks/docs). These products allow\nyou to quickly hand off the data, immediately return a success response to the\nwebhooks provider, and continue the processing without the timeout concern. These 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](/run/docs/function-triggers)\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"]]