Use webhooks
Webhooks notify your service when videos successfully finish processing and are ready to stream or if your video enters an error state.
To subscribe to receive webhook notifications on your service or modify an existing subscription, you will need a Cloudflare API token ↗.
The webhook notification URL must include the protocol. Only http://
or https://
is supported.
When a video on your account finishes processing, you will receive a POST
request notification with information about the video.
Note the status
field indicates whether the video processing finished successfully.
When a video is done processing and all quality levels are encoded, the state
field returns a ready
state. The ready
state can be useful if picture quality is important to you, and you only want to enable video playback when the highest quality levels are available.
If higher quality renditions are still processing, videos may sometimes return the state
field as ready
and an additional pctComplete
state that is not 100
. When pctComplete
reaches 100
, all quality resolutions are available for the video.
When at least one quality level is encoded and ready to be streamed, the readyToStream
value returns true
.
If a video could not process successfully, the state
field returns error
, and the errReasonCode
returns one of the values listed below.
ERR_NON_VIDEO
– The upload is not a video.ERR_DURATION_EXCEED_CONSTRAINT
– The video duration exceeds the constraints defined in the direct creator upload.ERR_FETCH_ORIGIN_ERROR
– The video failed to download from the URL.ERR_MALFORMED_VIDEO
– The video is a valid file but contains corrupt data that cannot be recovered.ERR_DURATION_TOO_SHORT
– The video’s duration is shorter than 0.1 seconds.ERR_UNKNOWN
– If Stream cannot automatically determine why the video returned an error, theERR_UNKNOWN
code will be used.
In addition to the state
field, a video’s readyToStream
field must also be true
for a video to play.
Example: POST body for successful video encoding
Cloudflare Stream will sign the webhook requests sent to your notification URLs and include the signature of each request in the Webhook-Signature
HTTP header. This allows your application to verify the webhook requests are sent by Stream.
To verify a signature, you need to retrieve your webhook signing secret. This value is returned in the API response when you create or retrieve the webhook.
To verify the signature, get the value of the Webhook-Signature
header, which will look similar to the example below.
Webhook-Signature: time=1230811200,sig1=60493ec9388b44585a29543bcf0de62e377d4da393246a8b1c901d0e3e672404
Retrieve the Webhook-Signature
header from the webhook request and split the string using the ,
character.
Split each value again using the =
character.
The value for time
is the current UNIX time ↗ when the server sent the request. sig1
is the signature of the request body.
At this point, you should discard requests with timestamps that are too old for your application.
Prepare the signature source string and concatenate the following strings:
- Value of the
time
field e.g.1230811200
- Character
.
- Webhook request body (complete with newline characters, if applicable)
Every byte in the request body must remain unaltered for successful signature verification.
Compute an HMAC with the SHA256 function (HMAC-SHA256) using your webhook secret and the source string from step 2. This step depends on the programming language used by your application.
Cloudflare’s signature will be encoded to hex.
Compare the signature in the request header to the expected signature. Preferably, use a constant-time comparison function to compare the signatures.
If the signatures match, you can trust that Cloudflare sent the webhook.
- Webhooks will only be sent after video processing is complete, and the body will indicate whether the video processing succeeded or failed.
- Only one webhook subscription is allowed per-account.
Golang
Using crypto/hmac ↗:
Node.js
Ruby
In JavaScript (for example, to use in Cloudflare Workers)