Create via API
Configure Custom Error Responses as rules belonging to the http_custom_errors
phase. Use the Rulesets API to create Custom Error Responses via API.
When creating a custom error response via API, make sure you:
- Set the rule action to
serve_error
. - Define the parameters in the
action_parameters
field according to response type. - Deploy the custom error response rule to the
http_custom_errors
phase at the zone level.
The first rule in the http_custom_errors
phase ruleset that matches will be applied. No other rules in the ruleset will be matched or applied.
Follow this workflow to create a custom error response rule for a given zone via API:
-
Use the List zone rulesets operation to check if there is already a ruleset for the
http_custom_errors
phase at the zone level. -
If the phase ruleset does not exist, create it using the Update a zone entry point ruleset operation, which allows you to create a ruleset if it does not exist and update all the rules in the ruleset. Create the ruleset in the
http_custom_errors
phase.If the phase ruleset already exists, use the Update a zone entry point ruleset operation to replace all the rules in the ruleset, or the Add rule to ruleset operation to add a rule to the existing rules in the ruleset.
The examples in this section use the following fields in their rule expressions:
-
http.response.code
: Represents the HTTP status code returned to the client, either set by a Cloudflare product or returned by the origin server. Use this field to customize the error response for error codes returned by the origin server or by a Cloudflare product such as a Worker. -
cf.response.1xxx_code
: Contains the specific error code for Cloudflare-generated errors. This field will only work for Cloudflare-generated errors such as 52x and 1xxx.
This example configures a custom JSON error response for all 5xx errors (500
-599
) in the zone with ID {zone_id}
. The HTTP status code of the custom error response will be set to 530
.
This PUT
request, corresponding to the Update a zone entry point ruleset operation, replaces any existing rules in the http_custom_errors
phase entry point ruleset.
This example configures a custom HTML error response for responses with a 500
HTTP status code, and redefines the response status code to 503
.
This PUT
request, corresponding to the Update a zone entry point ruleset operation, replaces any existing rules in the http_custom_errors
phase entry point ruleset.
This example configures a custom HTML error response for Cloudflare error 1020 (Access Denied).
This PUT
request, corresponding to the Update a zone entry point ruleset operation, replaces any existing rules in the http_custom_errors
phase entry point ruleset.
The API token used in API requests to manage Custom Error Responses must have at least the following permission:
- Custom Error Rules > Edit