Skip to content

Get started

This guide walks you through running your first authorized test. In this guide, you turn on Network Quality for your account, create an API token, set up your service to request authorization tokens, and run a test from a client.

A client is the software that runs tests for an end user or device, such as a website, a mobile app, or a router. The flow has three parts:

  1. Your client asks your service for a Network Quality authorization token.
  2. Your service decides whether the end user or device is authorized. If it is, your service requests a token from the Cloudflare API with an API token that only your service knows. Cloudflare issues and signs the token, and your service returns it to the client unchanged.
  3. Your client runs the test and sends the authorization token with every measurement request.
sequenceDiagram
accTitle: Network Quality authorization flow
accDescr: The client asks your service for an authorization token. Your service requests the token from the Cloudflare API and returns it to the client, which sends it with every request to the measurement endpoints.
participant Client
participant Server as Your service
participant API as Cloudflare API
participant Speed as Measurement endpoints
Client->>Server: Request an authorization token
Server->>API: POST /accounts/{account_id}/networkquality/token
API-->>Server: Authorization token
Server-->>Client: Authorization token
Client->>Speed: Measurement requests with the token

Prerequisites

  • A Cloudflare account with access to Network Quality. Network Quality is in private beta. To request access, contact your account team.
  • A service you operate that can decide which end users or devices are authorized, and make HTTPS requests to the Cloudflare API.

1. Turn on Network Quality

  1. In the Cloudflare dashboard, go to Networking > Network Quality.

    Go to Network Quality ↗
  2. Select Turn on.

To turn off Network Quality later, use the Network Quality API toggle at the top of the same page.

You need the Network Quality Edit permission to change this setting.

Send a PUT request to the Network Quality settings endpoint:

curl --request PUT \
  "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/networkquality/settings" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{ "enabled": true }'
{
	"success": true,
	"errors": [],
	"messages": [],
	"result": {
		"enabled": true,
		"updated_at": "2026-01-01T00:00:00.000Z"
	}
}

The API token must have the Account > Network Quality > Edit permission.

To check the current setting, send a GET request to the same endpoint. The Account > Network Quality > Read permission is enough for this request. If Network Quality has never been turned on for the account, the result object is { "enabled": false }, without updated_at.

2. Create an API token

Your service uses a Cloudflare API token to request authorization tokens.

  1. Create an API token with the following permission:

    • Account > Network Quality > Edit
  2. Scope the token to the account where you turned on Network Quality.

  3. Copy the token value. You store it as a secret in the next step.

3. Request authorization tokens from your service

Your service requests an authorization token from Cloudflare when a client is about to run a test and does not have a valid token. One token authorizes all the requests of one or more tests, until it expires or its byte budget runs out. You can use any language or platform. For each token request, your service should:

  1. Check that the end user or device is authorized to use Network Quality on your behalf. You define the policy. For examples, refer to Decide who can get a token.
  2. Request a token from the Cloudflare API with your API token, using the token attributes that fit your authorization model.
  3. Return the token to the client unchanged.

To request a token, send a POST request to the token endpoint:

curl --request POST \
  "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/networkquality/token" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "bytes": 2000000000,
    "eyeball_id": "3f8a1c9e2b7d4f60a5e1"
  }'
{
	"success": true,
	"errors": [],
	"messages": [],
	"result": {
		"token": "<TOKEN>"
	}
}

For each request field, including the optional source_ips restriction, refer to Authorization tokens.

Example: Request tokens from a Cloudflare Worker

The following Worker exposes a POST /speedtest-token endpoint to your client. It checks whether the end user or device is authorized, requests a token, and returns it unchanged. It requires Node.js ↗︎ and Wrangler.

  1. Create a new Worker project. my-token-server is the name of the project folder, and you can choose any name:

    npm create cloudflare@latest -- my-token-server

    For setup, select the following options:

    • For What would you like to start with?, choose Hello World example.
    • For Which template would you like to use?, choose Worker only.
    • For Which language do you want to use?, choose TypeScript.
    • For Do you want to use git for version control?, choose Yes.
    • For Do you want to add an AGENTS.md file to help AI coding tools understand Cloudflare APIs?, choose Yes.
    • For Do you want to deploy your application?, choose No (we will be making some changes before deploying).
  2. Add your account ID as a variable in your Wrangler configuration file:

    {
      "$schema": "./node_modules/wrangler/config-schema.json",
      "name": "my-token-server",
      "main": "src/index.ts",
      // Set this to today's date
      "compatibility_date": "2026-11-30",
      "vars": {
        "ACCOUNT_ID": "<ACCOUNT_ID>"
      }
    }
    name = "my-token-server"
    main = "src/index.ts"
    # Set this to today's date
    compatibility_date = "2026-11-30"
    
    [vars]
    ACCOUNT_ID = "<ACCOUNT_ID>"
  3. Store your API token as a secret:

    npx wrangler secret put NETWORK_QUALITY_API_TOKEN
  4. Replace the contents of src/index.ts with the following code:

    src/index.jsjs
    // Maximum data that tests can transfer with each token. Adjust it to your client.
    const TOKEN_BYTES = 2_000_000_000;
    
    // Replace with your own authorization policy, such as a session check or Turnstile verification.
    // Return a stable, pseudonymous ID for the end user or device, or null to reject the request.
    async function authorize(request) {
    	return null;
    }
    
    export default {
    	async fetch(request, env) {
    		const url = new URL(request.url);
    		if (request.method !== "POST" || url.pathname !== "/speedtest-token") {
    			return new Response("Not found", { status: 404 });
    		}
    
    		const eyeballId = await authorize(request);
    		if (!eyeballId) {
    			return new Response("Unauthorized", { status: 401 });
    		}
    
    		try {
    			const response = await fetch(
    				`https://api.cloudflare.com/client/v4/accounts/${env.ACCOUNT_ID}/networkquality/token`,
    				{
    					method: "POST",
    					signal: AbortSignal.timeout(10_000),
    					headers: {
    						Authorization: `Bearer ${env.NETWORK_QUALITY_API_TOKEN}`,
    						"Content-Type": "application/json",
    					},
    					body: JSON.stringify({
    						bytes: TOKEN_BYTES,
    						eyeball_id: eyeballId,
    						// source_ips: ["<CLIENT_IP>"],
    					}),
    				},
    			);
    
    			if (!response.ok) {
    				const headers = new Headers();
    				const retryAfter = response.headers.get("Retry-After");
    				if (retryAfter) {
    					headers.set("Retry-After", retryAfter);
    				}
    				return new Response("Unable to get token", {
    					status: response.status === 429 ? 429 : 502,
    					headers,
    				});
    			}
    
    			const { result } = await response.json();
    			return Response.json(
    				{ token: result.token },
    				{ headers: { "Cache-Control": "no-store" } },
    			);
    		} catch {
    			return new Response("Unable to get token", { status: 502 });
    		}
    	},
    };
    src/index.tsts
    interface Env {
    	ACCOUNT_ID: string;
    	NETWORK_QUALITY_API_TOKEN: string;
    }
    
    // Maximum data that tests can transfer with each token. Adjust it to your client.
    const TOKEN_BYTES = 2_000_000_000;
    
    // Replace with your own authorization policy, such as a session check or Turnstile verification.
    // Return a stable, pseudonymous ID for the end user or device, or null to reject the request.
    async function authorize(request: Request): Promise<string | null> {
    	return null;
    }
    
    export default {
    	async fetch(request, env): Promise<Response> {
    		const url = new URL(request.url);
    		if (request.method !== "POST" || url.pathname !== "/speedtest-token") {
    			return new Response("Not found", { status: 404 });
    		}
    
    		const eyeballId = await authorize(request);
    		if (!eyeballId) {
    			return new Response("Unauthorized", { status: 401 });
    		}
    
    		try {
    			const response = await fetch(
    				`https://api.cloudflare.com/client/v4/accounts/${env.ACCOUNT_ID}/networkquality/token`,
    				{
    					method: "POST",
    					signal: AbortSignal.timeout(10_000),
    					headers: {
    						Authorization: `Bearer ${env.NETWORK_QUALITY_API_TOKEN}`,
    						"Content-Type": "application/json",
    					},
    					body: JSON.stringify({
    						bytes: TOKEN_BYTES,
    						eyeball_id: eyeballId,
    						// source_ips: ["<CLIENT_IP>"],
    					}),
    				},
    			);
    
    			if (!response.ok) {
    				const headers = new Headers();
    				const retryAfter = response.headers.get("Retry-After");
    				if (retryAfter) {
    					headers.set("Retry-After", retryAfter);
    				}
    				return new Response("Unable to get token", {
    					status: response.status === 429 ? 429 : 502,
    					headers,
    				});
    			}
    
    			const { result } = await response.json<{ result: { token: string } }>();
    			return Response.json(
    				{ token: result.token },
    				{ headers: { "Cache-Control": "no-store" } },
    			);
    		} catch {
    			return new Response("Unable to get token", { status: 502 });
    		}
    	},
    } satisfies ExportedHandler<Env>;
  5. Deploy the Worker:

    npx wrangler deploy

The Worker must run on the same hostname as your website, for example with a route. Otherwise, add Cross-Origin Resource Sharing (CORS) headers to its responses, and answer OPTIONS preflight requests.

4. Send the token with measurement requests

Your client sends the authorization token in an Authorization header with every request to the measurement endpoints. Only send the token over HTTPS.

For example, to download 1 MB:

curl --output /dev/null \
  "https://speed.cloudflare.com/__down?bytes=1000000" \
  --header "Authorization: Bearer $NETWORK_QUALITY_TOKEN" \
  --write-out "%{speed_download} bytes/s\n"

Next steps

Was this helpful?