### Install Cloudflare SDK Source: https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/reference/platform-examples Install the Cloudflare SDK for Node.js using npm. This is a prerequisite for using the TypeScript SDK examples. ```bash npm install cloudflare ``` -------------------------------- ### CNAME Target Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/domain-support/hostname-validation/realtime-validation Example of a CNAME record required for real-time validation when using a CNAME target setup. ```text mystore.com CNAME customers.saasprovider.com ``` -------------------------------- ### Fallback Origin DNS Record Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/start/advanced-settings/apex-proxying/setup This is an example of an 'A' record configuration for a fallback origin. Ensure this record is proxied. ```text Type| Name| IPv4 address| Proxy status ---|---|---|--- `A`| `proxy-fallback`| `192.0.2.1`| Proxied ``` -------------------------------- ### Customer CNAME Record Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/start/getting-started This is an example of a CNAME record a customer would create at their DNS provider to point to your SaaS CNAME target. ```text mystore.example.com CNAME customers.saasprovider.com ``` -------------------------------- ### Usage Example for Deploying Worker with Assets Source: https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/reference/platform-examples/index.md This is a usage example demonstrating how to call the deployWorkerWithAssets function with specific account ID, environment, script name, and asset details. It includes sample HTML and CSS content encoded in base64. ```javascript await deployWorkerWithAssets("your-account-id", "production", "customer-site", [ { path: "/index.html", content: btoa("Hello World"), size: 37, }, { path: "/styles.css", content: btoa("body { font-family: sans-serif; }"), size: 33, }, ]); ``` -------------------------------- ### Custom Hostname Details Response Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/security/certificate-management/enforce-mtls/index.md This is an example JSON response when querying for custom hostname details. It includes SSL settings, validation records, and other relevant information for the hostname. ```json { "success": true, "result": { "id": "", "ssl": { "id": "", "bundle_method": "ubiquitous", "certificate_authority": "", "custom_certificate": "", "custom_csr_id": "", "custom_key": "", "expires_on": "", "hosts": [ "app.example.com", "*.app.example.com" ], "issuer": "", "method": "http", "settings": {}, "signature": "SHA256WithRSA", "type": "dv", "uploaded_on": "2020-02-06T18:11:23.531995Z", "validation_errors": [ { "message": "SERVFAIL looking up CAA for app.example.com" } ], "validation_records": [ { "emails": [ "administrator@example.com", "webmaster@example.com" ], "http_body": "ca3-574923932a82475cb8592200f1a2a23d", "http_url": "http://app.example.com/.well-known/pki-validation/ca3-da12a1c25e7b48cf80408c6c1763b8a2.txt", "txt_name": "_acme-challenge.app.example.com", "txt_value": "810b7d5f01154524b961ba0cd578acc2" } ], "wildcard": false }, } } ``` -------------------------------- ### Apex Proxying Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/domain-support/hostname-validation/realtime-validation Example of an A record required for real-time validation when using apex proxying. The record points to the IP prefix allocated to the SaaS provider's account. ```text example.com. 60 IN A 192.0.2.1 ``` -------------------------------- ### Example Certificate Content Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/security/certificate-management/custom-certificates/uploading-certificates This is an example of a PEM-encoded X.509 certificate. ```text -----BEGIN CERTIFICATE----- MIIFJDCCBAygAwIBAgIQD0ifmj/Yi5NP/2gdUySbfzANBgkqhkiG9w0BAQsFADBN MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMScwJQYDVQQDEx5E ... SzSHfXp5lnu/3V08I72q1QNzOCgY1XeL4GKVcj4or6cT6tX6oJH7ePPmfrBfqI/O OeH8gMJ+FuwtXYEPa4hBf38M5eU5xWG7 -----END CERTIFICATE----- ``` -------------------------------- ### HubSpot O2O CNAME Record Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt Example of a CNAME DNS record configuration for enabling O2O with HubSpot. This record should be created in your Cloudflare zone. ```text Type Name Target Proxy status CNAME .sites-proxy.hscoscdn<##>.net Proxied ``` -------------------------------- ### Example Pricing Calculation Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt Illustrates the monthly cost calculation for a Workers for Platforms project based on requests, CPU time, and script usage. Includes included requests and CPU time allowances. ```text Monthly Costs | Formula | | ---------------- | ------- | | **Subscription** | $25.00 | | **Requests** | $24.00 | (100,000,000 requests - 20,000,000 included requests) / 1,000,000 * $0.30 | | **CPU time** | $18.80 | ((10 ms of CPU time per request * 100,000,000 requests) - 60,000,000 included CPU ms) / 1,000,000 * $0.02 | | **Scripts** | $4.00 | (1200 scripts - 1000 included scripts) * $0.02 | | **Total** | $71.80 | ``` -------------------------------- ### Serve Static Assets from Binding Source: https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets Example Worker code demonstrating how to serve requests using the ASSETS binding, which is configured in Wrangler. This allows dynamic serving of bundled static files. ```javascript export default { async fetch(request, env, ctx) { return env.ASSETS.fetch(request); }, }; ``` -------------------------------- ### Shopify DNS CAA Record Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt This JSON-LD snippet represents a BreadcrumbList for navigating Cloudflare for Platforms documentation, specifically related to Shopify. ```json {"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/cloudflare-for-platforms/","name":"Cloudflare for Platforms"}},{"@type":"ListItem","position":3,"item":{"@id":"/cloudflare-for-platforms/cloudflare-for-saas/","name":"Cloudflare for SaaS"}},{"@type":"ListItem","position":4,"item":{"@id":"/cloudflare-for-platforms/cloudflare-for-saas/saas-customers/","name":"SaaS customers"}},{"@type":"ListItem","position":5,"item":{"@id":"/cloudflare-for-platforms/cloudflare-for-saas/saas-customers/provider-guides/","name":"Provider guides"}},{"@type":"ListItem","position":6,"item":{"@id":"/cloudflare-for-platforms/cloudflare-for-saas/saas-customers/provider-guides/shopify/","name":"Shopify"}}]} ``` -------------------------------- ### O2O DNS Record Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt This table shows a CNAME record configuration for a customer-owned zone (example.com) that enables O2O. The record points to the SaaS provider's domain and is proxied. ```text Type | Name | Target | Proxy status CNAME | mystore | customers.saasprovider.com | Proxied ``` -------------------------------- ### Custom Hostname API Response Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt This is an example JSON response from the Cloudflare API when querying custom hostname details. It includes SSL settings, validation records, and other relevant information. ```json { "success": true, "result": { "id": "", "ssl": { "id": "", "bundle_method": "ubiquitous", "certificate_authority": "", "custom_certificate": "", "custom_csr_id": "", "custom_key": "", "expires_on": "", "hosts": [ "app.example.com", "*.app.example.com" ], "issuer": "", "method": "http", "settings": {}, "signature": "SHA256WithRSA", "type": "dv", "uploaded_on": "2020-02-06T18:11:23.531995Z", "validation_errors": [ { "message": "SERVFAIL looking up CAA for app.example.com" } ], "validation_records": [ { "emails": [ "administrator@example.com", "webmaster@example.com" ], "http_body": "ca3-574923932a82475cb8592200f1a2a23d", "http_url": "http://app.example.com/.well-known/pki-validation/ca3-da12a1c25e7b48cf80408c6c1763b8a2.txt", "txt_name": "_acme-challenge.app.example.com", "txt_value": "810b7d5f01154524b961ba0cd578acc2" } ], "wildcard": false } } } ``` -------------------------------- ### Example Manifest for Static Assets Source: https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets A JSON object representing the manifest of files to be uploaded, including their paths, hashes, and sizes. This is used to initiate the asset upload process. ```json { "/index.html": { "hash": "08f1dfda4574284ab3c21666d1ee8c7d4", "size": 1234 }, "/styles.css": { "hash": "36b8be012ee77df5f269b11b975611d3", "size": 5678 } } ``` -------------------------------- ### Create User Worker Script Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt This example shows how to create a basic Worker script file named 'worker.mjs' using a shell command. The script exports a default object with an async fetch function that returns a simple response. ```bash # First, create the worker script file cat > worker.mjs << 'EOF' export default { async fetch(request, env, ctx) { return new Response("Hello from user Worker!"); }, }; EOF ``` -------------------------------- ### Create Custom Hostname API Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/start/advanced-settings/apex-proxying/setup Use the Create Custom Hostname endpoint to programmatically create a custom hostname. Leaving 'certificate_authority' empty defaults to 'default CA'. ```bash curl -X POST https://api.cloudflare.com/client/v4/zones/:zone_identifier/custom_hostnames \ -H "Authorization: Bearer $CF_API_TOKEN" \ -H "Content-Type: application/json" \ --data '{ "hostname": "app.customer.com", "ssl": { "method": "txt", "type": "dv", "wildcard_all_subdomains": true } }' ``` -------------------------------- ### List Dispatch Namespaces Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt Fetch a list of all Workers for Platforms dispatch namespaces within an account. This example uses cURL to make the API request. ```http curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" ``` -------------------------------- ### Get Custom Hostname Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt Retrieves details of a specific custom hostname within a zone. ```APIDOC ## GET /v4/zones/:zone_id/custom_hostnames/:custom_hostname_id ### Description Retrieves details of a specific custom hostname. ### Method GET ### Endpoint /v4/zones/:zone_id/custom_hostnames/:custom_hostname_id ### Parameters #### Path Parameters - **zone_id** (string) - Required - The ID of the zone. - **custom_hostname_id** (string) - Required - The ID of the custom hostname to retrieve. ``` -------------------------------- ### Breadcrumb Navigation Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt This JSON-LD snippet represents a breadcrumb trail for navigation, commonly used for SEO and user experience. ```json {"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/ssl/","name":"SSL/TLS"}},{"@type":"ListItem","position":3,"item":{"@id":"/ssl/reference/","name":"Reference"}},{"@type":"ListItem","position":4,"item":{"@id":"/ssl/reference/certificate-statuses/","name":"Certificate statuses"}}]} ``` -------------------------------- ### Update Custom Hostname SSL Settings with Cipher Suites Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt This example demonstrates how to update the SSL settings for a custom hostname, including specifying authorized cipher suites and the minimum TLS version. This is typically done after retrieving existing settings and modifying the desired parameters. ```bash curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/custom_hostnames/$CUSTOM_HOSTNAME_ID" \ --request PATCH \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "ssl": { "method": "http", "type": "dv", "custom_certificate": "", "custom_key": "", "settings": { "ciphers": [ "ECDHE-ECDSA-AES128-GCM-SHA256", "ECDHE-RSA-AES128-GCM-SHA256" ], "min_tls_version": "1.2" } } }' ``` -------------------------------- ### API Call to Set Rate Limiting Rule Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt This example demonstrates how to use the Cloudflare API to create a rate limiting rule that blocks requests if the 'security_level' tag is 'low' and the URI contains 'login'. This rule replaces any existing rate limiting rules for the zone. ```bash curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_ratelimit/entrypoint" \ --request PUT \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ \ "rules": [ \ { \ "action": "block", \ "ratelimit": { \ "characteristics": [ \ "cf.colo.id", \ "ip.src" \ ], \ "period": 10, \ "requests_per_period": 2, \ "mitigation_timeout": 60 \ }, \ "expression": "lookup_json_string(cf.hostname.metadata, \"security_level\") eq \"low\" and http.request.uri contains \"login\"" \ } \ ] \ }' ``` -------------------------------- ### Deploy Platform Starter Kit Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt One-click deployment button for the Platform Starter Kit, which sets up a complete Workers for Platforms environment. ```html Deploy to Cloudflare ``` -------------------------------- ### Enable O2O with CNAME Record for Kinsta Source: https://developers.cloudflare.com/cloudflare-for-platforms/llms-full.txt To enable O2O for a specific hostname within a Cloudflare zone, create a Proxied CNAME DNS record with your Kinsta site name as the target. Kinsta's domain addition setup will guide you through other validation steps. ```text Type | Name | Target | Proxy status | | ----- | ---------------- | ----------------------------- | ------------ | | CNAME | | sitename.hosting.kinsta.cloud | Proxied | ``` -------------------------------- ### Create Custom Hostname with Early Hints Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/performance/early-hints-for-saas/index.md Use this API call to create a new custom hostname and enable Early Hints by specifying "early_hints": "on" within the ssl settings. ```curl curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/custom_hostnames" \ --request POST \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ \ "hostname": "", \ "ssl": { \ "method": "http", \ "type": "dv", \ "settings": { \ "http2": "on", \ "min_tls_version": "1.2", \ "tls_1_3": "on", \ "early_hints": "on" \ }, \ "bundle_method": "ubiquitous", \ "wildcard": false \ } \ }' ``` -------------------------------- ### Example Originless DNS Record Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/start/advanced-settings/worker-as-origin/index.md An example of an originless DNS record for your fallback origin. This record type is used when the origin is not a specific IP address but rather a hostname that Cloudflare will resolve. ```dns service.example.com AAAA 100:: ``` -------------------------------- ### Terraform Custom Metadata Example Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/domain-support/custom-metadata/index.md When using Terraform, custom metadata for hostnames is limited to string keys and values due to Terraform's map type constraints. This example shows a valid structure. ```json {"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/cloudflare-for-platforms/","name":"Cloudflare for Platforms"}},{"@type":"ListItem","position":3,"item":{"@id":"/cloudflare-for-platforms/cloudflare-for-saas/","name":"Cloudflare for SaaS"}},{"@type":"ListItem","position":4,"item":{"@id":"/cloudflare-for-platforms/cloudflare-for-saas/domain-support/","name":"Custom hostnames"}},{"@type":"ListItem","position":5,"item":{"@id":"/cloudflare-for-platforms/cloudflare-for-saas/domain-support/custom-metadata/","name":"Custom metadata"}}]} ``` -------------------------------- ### Create User Worker Script Source: https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/reference/platform-examples/index.md Create a simple 'Hello World' Worker script file named 'worker.mjs'. This script will be deployed to the dispatch namespace. ```bash # First, create the worker script file cat > worker.mjs << 'EOF' export default { async fetch(request, env, ctx) { return new Response("Hello from user Worker!"); }, }; EOF ``` -------------------------------- ### Outbound Worker Example: Logging and JWT Creation Source: https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/configuration/outbound-workers/index.md An example Outbound Worker that logs incoming requests and conditionally creates a JWT for API calls. Access parameters passed from the dispatch Worker via the 'env' object. ```javascript export default { // this event is fired when the dispatched Workers make a subrequest async fetch(request, env, ctx) { // env contains the values we set in `dispatcher.get()` const customer_name = env.customer_name; const original_url = env.url; // log the request ctx.waitUntil( fetch("https://logs.example.com", { method: "POST", body: JSON.stringify({ customer_name, original_url, }), }), ); const url = new URL(original_url); if (url.host === "api.example.com") { // pre-auth requests to our API const jwt = make_jwt_for_customer(customer_name); let headers = new Headers(request.headers); headers.set("Authorization", `Bearer ${jwt}`); // clone the request to set new headers using existing body let new_request = new Request(request, { headers }); return fetch(new_request); } return fetch(request); }, }; ``` -------------------------------- ### Get fallback origin Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/start/common-api-calls Retrieves the fallback origin for a custom hostname. ```APIDOC ## Get fallback origin ### Description Retrieves the fallback origin for a custom hostname. ### Method GET ### Endpoint /accounts/{account_identifier}/zones/{zone_identifier}/custom_hostnames/{custom_hostname_identifier}/fallback_origin ``` -------------------------------- ### Create Custom Hostname with Early Hints Enabled Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/performance/early-hints-for-saas This API call creates a new custom hostname and enables Early Hints by setting `"early_hints": "on"` within the SSL settings. ```APIDOC ## POST /zones/:zone_id/custom_hostnames ### Description Creates a new custom hostname with Early Hints enabled. ### Method POST ### Endpoint https://api.cloudflare.com/client/v4/zones/$ZONE_ID/custom_hostnames ### Parameters #### Path Parameters - **ZONE_ID** (string) - Required - Your zone ID. #### Request Body - **hostname** (string) - Required - The custom hostname to create. - **ssl** (object) - Required - SSL configuration for the custom hostname. - **method** (string) - Required - SSL method, typically "http". - **type** (string) - Required - SSL type, typically "dv". - **settings** (object) - Required - SSL settings. - **http2** (string) - Required - Enable HTTP/2, "on" or "off". - **min_tls_version** (string) - Required - Minimum TLS version, e.g., "1.2". - **tls_1_3** (string) - Required - Enable TLS 1.3, "on" or "off". - **early_hints** (string) - Required - Enable Early Hints, "on" or "off". - **bundle_method** (string) - Required - SSL bundle method, e.g., "ubiquitous". - **wildcard** (boolean) - Required - Whether the hostname is a wildcard. ### Request Example ```json { "hostname": "", "ssl": { "method": "http", "type": "dv", "settings": { "http2": "on", "min_tls_version": "1.2", "tls_1_3": "on", "early_hints": "on" }, "bundle_method": "ubiquitous", "wildcard": false } } ``` ### Response #### Success Response (200) Returns the created custom hostname object, including its ID and SSL settings. #### Response Example (Response structure not provided in source, example omitted) ### Required API token permissions - SSL and Certificates Write ``` -------------------------------- ### Create Custom Hostname with Early Hints Enabled Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/performance/early-hints-for-saas Use this API call to create a new custom hostname and enable Early Hints by specifying 'early_hints': 'on' within the SSL settings. ```curl curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/custom_hostnames" \ --request POST \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ \ "hostname": "", \ "ssl": { \ "method": "http", \ "type": "dv", \ "settings": { \ "http2": "on", \ "min_tls_version": "1.2", \ "tls_1_3": "on", \ "early_hints": "on" \ }, \ "bundle_method": "ubiquitous", \ "wildcard": false \ } \ }' ``` -------------------------------- ### Get Custom CSRs Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/reference/status-codes/custom-csrs Retrieves a list of all Custom CSRs for a given zone. ```APIDOC ## GET /api/v4/zones/:zone_id/custom_csrs ### Description Retrieves a list of all Custom CSRs for a given zone. ### Method GET ### Endpoint /api/v4/zones/:zone_id/custom_csrs ### Parameters #### Path Parameters - **zone_id** (string) - Required - The ID of the zone. ### Response #### Success Response (200 OK) - **custom_csrs** (array) - A list of Custom CSR objects. - **custom_csr_id** (string) - The ID of the Custom CSR. - **created_at** (string) - The timestamp when the Custom CSR was created. #### Response Example { "custom_csrs": [ { "custom_csr_id": "a1b2c3d4e5f6", "created_at": "2023-10-27T10:00:00Z" } ] } ``` -------------------------------- ### Deploy Worker with Bindings and Tags via TypeScript SDK Source: https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/reference/platform-examples Deploy a Worker with KV namespace bindings and tags using the TypeScript SDK. This example demonstrates how to configure bindings and tags programmatically. ```typescript import Cloudflare from "cloudflare"; const client = new Cloudflare({ apiToken: process.env.API_TOKEN, }); async function deployWorkerWithBindingsAndTags( accountId: string, namespace: string, scriptName: string, scriptContent: string, kvNamespaceId: string, tags: string[], ) { const scriptFile = new File([scriptContent], `${scriptName}.mjs`, { type: "application/javascript+module", }); const result = await client.workersForPlatforms.dispatch.namespaces.scripts.update( namespace, scriptName, { account_id: accountId, metadata: { main_module: `${scriptName}.mjs`, compatibility_date: "2024-01-01", bindings: [ { type: "kv_namespace", name: "MY_KV", namespace_id: kvNamespaceId, }, ], tags: tags, // e.g., ["customer-123", "production", "pro-plan"] }, files: [scriptFile], }, ); return result; } // Usage const scriptContent = `export default { async fetch(request, env, ctx) { const value = await env.MY_KV.get("key") || "default"; return new Response(value); }, }; `; await deployWorkerWithBindingsAndTags( "your-account-id", "production", "customer-123-app", scriptContent, "kv-namespace-id", ["customer-123", "production", "pro-plan"], ); ```