Skip to content
Dashboard

Delivery

View as Markdown

Page rules

Route requests to another origin, redirect them, and change request and response headers with ordered rules.

123

Page rules change what happens to the requests of one domain. There are four families of rules, each a separate collection with the same six calls. The paths start with /v2/cdn/page_rules/{domain_public_id}/, where the id is the public id of the domain, taken from the domains list.

The examples on this page use these shell variables:

export DIAMWALL_API_KEY="your key"
export PAGE_RULES="https://api.diamwall.com/v2/cdn/page_rules/your-domain-public-id"

Every family has the same calls. Replace origin-rules with redirect-rules, request-transforms or response-transforms to work on another family.

You want to Call Header it needs
List the rules GET .../origin-rules/
Read one rule GET .../origin-rules/{rule_id}/
Create a rule POST .../origin-rules/ If-Match
Edit a rule PATCH .../origin-rules/{rule_id}/ If-Match
Delete a rule DELETE .../origin-rules/{rule_id}/ If-Match
Set the order of the rules PUT .../origin-rules/order/ If-Match

Every rule has the same common fields. Each family then adds its own, described in its section below.

Field On create What it holds
id Never sent Given by the CDN: r_ and 32 hexadecimal digits.
name Required One line, 1 to 128 bytes. Two rules of the same family cannot share a name.
description Optional One line, at most 1024 bytes. An empty string clears it.
enabled Required true applies the rule. false keeps it without applying it.
type Required all applies the rule to every request of the domain. filter applies it to the requests that expressions select.
expressions Required when type is filter The conditions, 1 to 64 of them.
priority Optional A whole number from 1 to 1000000. The lowest is evaluated first. Without it, the CDN gives the new rule one.

Booleans and numbers are sent as JSON booleans and numbers. The string "443" or "301" is refused. Nothing you send is trimmed or converted, and a field the API does not know is refused. A body holds at most 32768 bytes of JSON.

Each entry of expressions has exactly four keys:

Key Allowed values
type main for the first condition. main, and or or for the others.
field ip, country, continent, asn, ua, cookie, hostname, referer, reqmethod, ssl, uri, fulluri, querystring, httpversion
operator wildcard, equals, not_equals, contains, not_contains, starts_with, not_starts_with, ends_with, not_ends_with
value One line, 1 to 2048 bytes.
"expressions": [
{ "type": "main", "field": "uri", "operator": "starts_with", "value": "/api/" },
{ "type": "and", "field": "reqmethod", "operator": "equals", "value": "POST" }
]

When you send expressions, you send the whole list of the rule.

Every read of a family, the list or one rule, answers an ETag header. It is the revision of the whole collection of that family, not of one rule. Every create, edit, delete and reorder sends it back in If-Match, exactly as it came, with its quotes.

ETAG=$(curl -s -o /dev/null -D - "$PAGE_RULES/origin-rules/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
| tr -d '\r' | awk 'tolower($1) == "etag:" { print $2 }')
  • An empty collection has an ETag too, so the first create also starts with a read.
  • Each family has its own revision. An ETag read from redirect-rules/ is not valid for origin-rules/.
  • Every write answers a new ETag header. Use it on your next write to the same family.
  • Without If-Match the write answers 428. If the collection changed since your read, it answers 412 REVISION_MISMATCH, with the current revision in details.current_revision. Read again, check that your change still makes sense, then send it.
curl "$PAGE_RULES/origin-rules/?limit=50" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY"
{
"revision": "sha256:107b85532dbf...",
"consistency": "verified",
"publication": { "status": "verified", "rollout": "unknown" },
"data": {
"items": [
{
"id": "r_00000000000000000000000000000001",
"name": "API to its own origin",
"description": "Sends the API to the servers that hold it",
"enabled": true,
"priority": 10,
"type": "filter",
"expressions": [
{ "type": "main", "field": "uri", "operator": "starts_with", "value": "/api/" }
],
"overrides": { "destination": "192.0.2.20", "port": 8443, "ssl": true }
}
],
"page": { "limit": 50, "next_cursor": null, "total": 1 }
}
}
  • limit goes from 1 to 100. Without it a page holds 50 rules.
  • To read the next page, send data.page.next_cursor back as cursor, until it is null.
  • data.page.total is the number of rules in the whole collection.
  • cursor and limit are the only query parameters. Any other answers 400 INVALID_REQUEST.
  • If the collection changes between two pages, the next page answers 409 CURSOR_STALE. Start again from the first page.
  • An empty items list means the domain has no rule of that family. A list that could not be read is an error, never an empty list.
  • A rule of type all may come back with no expressions. A rule may also come back with no priority. Treat a missing field as missing, not as 0 or false.

To read one rule, add its id: GET .../origin-rules/{rule_id}/. The rule is under data. An id that no rule has answers 404 RESOURCE_ITEM_NOT_FOUND.

Read the list first to get the ETag, then send the rule:

curl -X POST "$PAGE_RULES/origin-rules/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{
"name": "API to its own origin",
"description": "Sends the API to the servers that hold it",
"enabled": true,
"priority": 10,
"type": "filter",
"expressions": [
{ "type": "main", "field": "uri", "operator": "starts_with", "value": "/api/" }
],
"overrides": { "destination": "192.0.2.20", "port": 8443, "ssl": true }
}'

The answer is 201 with the new rule under data, its id included, and a new ETag header. The rule is stored as you sent it: the API adds no default value.

A name that another rule of the family already has answers 409 RULE_NAME_CONFLICT. Choose another name.

Send only the fields that change. A field you leave out keeps its value.

curl -X PATCH "$PAGE_RULES/origin-rules/r_00000000000000000000000000000001/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{ "enabled": false }'

The answer is the rule as it is after the edit, under data. An empty body ({}) answers 400 NO_CHANGES.

curl -X DELETE "$PAGE_RULES/origin-rules/r_00000000000000000000000000000001/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "If-Match: $ETAG"

The request has no body. The answer has no rule: operation.id is the id that was deleted.

The body is exactly { "ids": [...] }: the ids of the rules of the family, in the order you want. It is always the whole order, never only the rules that moved. Read every page of the list first, so that you have every id.

curl -X PUT "$PAGE_RULES/origin-rules/order/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{
"ids": [
"r_00000000000000000000000000000002",
"r_00000000000000000000000000000001"
]
}'

The answer is the whole collection in its new order, under data.items, with no paging. A list that leaves a rule out, names a rule the family does not have, or repeats an id answers 400 INVALID_REQUEST. Read the collection again and send the full order.

Two answers mean that the write may or may not have been applied:

  • 502 FILE_SERVER_OUTCOME_UNKNOWN: the answer of the write was lost.
  • FILE_SERVER_V2_ERROR on a write: the CDN answered something the API does not recognise.

In both cases do not send the same request again. Read the collection, compare it with what you meant to do, and only then decide. After a create, the rule may already be in the list. After a delete, a 404 RESOURCE_ITEM_NOT_FOUND on a read of the rule says it is gone.

An origin rule changes where the matching requests are sent. Beside the common fields it has overrides, an object with at least one of these keys:

Key Value Plan
destination Where the requests are sent: an IPv4 address, an IPv6 address or a hostname. No port, no scheme, no path. Every plan
port The port they are sent to: a whole number from 1 to 65535. Every plan
ssl true to reach the origin over HTTPS, false over HTTP. Every plan
host_header The Host header sent to the origin: a hostname, an IPv4 address or a bracketed IPv6 address, optionally followed by a port. Enterprise
sni The server name sent to the origin in the TLS handshake: an IPv4 address, an IPv6 address or a hostname. Enterprise

A read answers only the overrides the rule has. A key that is absent is an override the rule does not make. It is not false, 0 or a default.

{
"name": "API to its own origin",
"enabled": true,
"type": "filter",
"expressions": [
{ "type": "main", "field": "uri", "operator": "starts_with", "value": "/api/" }
],
"overrides": { "destination": "192.0.2.20", "port": 8443, "ssl": true }
}

To change one override, send the whole overrides object with every key the rule should keep:

curl -X PATCH "$PAGE_RULES/origin-rules/r_00000000000000000000000000000001/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{ "overrides": { "destination": "192.0.2.20", "port": 9443, "ssl": true } }'

A redirect rule answers the matching requests with a redirect. Beside the common fields it has three fields, all required on create:

Field Value
destination An absolute HTTP or HTTPS URL, or a path starting with /. One line, at most 4096 bytes. No credentials, whitespace, double quotes, backslashes, semicolons or braces.
status_code 301, 302, 303, 307 or 308, as a number.
preserve_query_string true keeps the query string of the request on the destination. false drops it.
curl -X POST "$PAGE_RULES/redirect-rules/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{
"name": "Welcome page",
"description": "Sends the old welcome path to the new one",
"enabled": true,
"type": "filter",
"expressions": [
{ "type": "main", "field": "uri", "operator": "equals", "value": "/welcome-old" }
],
"destination": "https://example.com/welcome",
"status_code": 301,
"preserve_query_string": true
}'

On an edit, each of the three fields is changed on its own:

curl -X PATCH "$PAGE_RULES/redirect-rules/r_00000000000000000000000000000001/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{ "status_code": 302 }'

A request transform changes the headers of the matching requests. Beside the common fields it has actions, a list of 1 to 64 actions applied in order. Each action has an operation, a header and, for every operation but remove, a value.

operation What it does value
set_static Sets the header The text itself
set_dynamic Sets the header The name of a variable
add_static Adds to the header The text itself
add_dynamic Adds to the header The name of a variable
remove Takes the header away Not allowed
  • header starts with a letter and holds only letters, digits and -, at most 127 characters. Underscores are not accepted.
  • A static value is one line of at most 8192 bytes and may be empty. It cannot hold double quotes, backslashes, $, ; or braces.
  • A dynamic value is one of ip, country, continent, asn, asn_org, hostname, uri, path, query_string, method, scheme, user_agent, ssl_protocol, ssl_cipher, http_version, or a name of the form http_ followed by lowercase letters, digits and underscores.
curl -X POST "$PAGE_RULES/request-transforms/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{
"name": "Client address for the API",
"enabled": true,
"type": "filter",
"expressions": [
{ "type": "main", "field": "uri", "operator": "starts_with", "value": "/api/" }
],
"actions": [
{ "operation": "set_dynamic", "header": "X-Client-IP", "value": "ip" },
{ "operation": "add_static", "header": "X-Example", "value": "demo" },
{ "operation": "remove", "header": "X-Debug" }
]
}'

On an edit, actions is the whole list. To add one action, send the existing ones with it.

A response transform changes the headers of the responses to the matching requests. Its actions have the same shape, operations and limits as those of a request transform.

curl -X POST "$PAGE_RULES/response-transforms/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY" \
-H "Content-Type: application/json" \
-H "If-Match: $ETAG" \
-d '{
"name": "Headers of the public pages",
"enabled": true,
"type": "filter",
"expressions": [
{ "type": "main", "field": "uri", "operator": "starts_with", "value": "/public/" }
],
"actions": [
{ "operation": "remove", "header": "X-Powered-By" },
{ "operation": "set_static", "header": "X-Example", "value": "demo" },
{ "operation": "add_dynamic", "header": "X-Served-For", "value": "country" }
]
}'

GET .../settings/rules/ is read-only and answers no field yet. The answer holds only revision, consistency and publication, with no data and no ETag. Do not conclude anything about the rules of the domain from it. Read the rules through the four collections above.

curl "$PAGE_RULES/settings/rules/" \
-H "Authorization: Api-Key $DIAMWALL_API_KEY"

A state of unknown with a revision of null means the settings could not be read. Treat it as unavailable.

  • Credits. Each domain has a budget of 3000 credits a minute, shared by everyone who calls the API for that domain. A read of a list or of one rule costs 6 credits. A create, edit, delete, reorder or a read of the rules settings costs 8. An edit of an origin rule can cost 14, when the rule is read before it is changed.
  • Refused before the CDN. A request the API refuses by itself, such as a 400 for a wrong field or a 428, spends none of the domain’s credits.
  • Out of credits. The call answers 429 TOO_MANY_REQUESTS, with the wait in details.retry_after_seconds.
  • Rules per family. A family holds at most 500 rules. This ceiling does not depend on the plan. A create beyond it answers 400 INVALID_REQUEST.
  • Plan. host_header and sni of an origin rule need the Enterprise plan. Everything else on this page is on every plan.
  • Permissions. Reads need pages.view on the domain. A create needs pages.create, a delete needs pages.delete, an edit or a reorder needs pages.edit. Holding pages.create or pages.delete also lets you edit and reorder, and any of the three write permissions lets you read.
Status Code What happened
400 INVALID_REQUEST A field, the rule_id, cursor, limit or ids is wrong. The answer names it in fields when it can. With a request_id, the CDN refused a value the API let through, or the family is at 500 rules.
400 NO_CHANGES An edit was sent with an empty body.
400 INVALID_PRECONDITION If-Match is not the ETag as it came: a weak one, *, a list or a value without quotes.
403 FEATURE_NOT_AVAILABLE The origin rule carries host_header or sni and the plan of the domain does not have them.
404 RESOURCE_ITEM_NOT_FOUND No rule of the family has that id. It may have been deleted.
404 RULE_NOT_FOUND The rule was gone when the write reached it. Nothing was changed. Read the collection again.
409 RULE_NAME_CONFLICT Another rule of the family has that name.
409 CURSOR_STALE The collection changed while you were paging. Start from the first page.
409 LEGACY_COLLECTION_DRIFT The rules of this family cannot be read or written for this domain. Nothing in the request can fix it. Contact support with the request_id.
409 DOMAIN_REPLACEMENT_PENDING The domain is being renamed. Nothing was written. Try again later.
409 DOMAIN_WITHOUT_KEY The domain is not active on the network yet.
412 REVISION_MISMATCH The collection changed since your read. Read again and retry.
415 UNSUPPORTED_MEDIA_TYPE The body was not sent as application/json.
428 PRECONDITION_REQUIRED The If-Match header is missing.
429 TOO_MANY_REQUESTS The domain is out of credits for now.
502 FILE_SERVER_OUTCOME_UNKNOWN The answer of a write was lost. Read the collection before you try again.
503 COLLECTION_STATE_CHANGING The collection is busy with an earlier change. Wait, read it again, then decide.
503 INVALID_ORIGIN_ROUTING_PLAN, ORIGIN_ROUTING_ROOT_MISMATCH Origin rules only. The origin routing of the domain could not be published. Do not repeat the request. Read the collection again and contact support with the request_id.

The errors that come from the key itself, such as API_KEY_IP_NOT_ALLOWED, are on the Errors page.

Each row opens the endpoint in the API reference, with every parameter, rule and response.

Loading the endpoints...