Table of Contents

Interface IWorkersApi

Namespace
Cloudflare.NET.Workers
Assembly
Cloudflare.NET.dll

Provides access to Cloudflare Workers API operations.

This interface provides zone-scoped operations for managing Worker routes, which map URL patterns to Worker scripts. Future expansion may include Worker script management, bindings, and other Workers platform features.

public interface IWorkersApi

Remarks

Worker routes are zone-scoped resources that determine which requests are handled by which Workers based on URL pattern matching.

Methods

CreateRouteAsync(string, CreateWorkerRouteRequest, CancellationToken)

Creates a new Worker route.

Task<WorkerRoute> CreateRouteAsync(string zoneId, CreateWorkerRouteRequest request, CancellationToken cancellationToken = default)

Parameters

zoneId string

The zone identifier.

request CreateWorkerRouteRequest

The route creation parameters.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<WorkerRoute>

The created Worker route.

Examples

var route = await client.Workers.CreateRouteAsync(zoneId,
  new CreateWorkerRouteRequest(
    Pattern: "api.example.com/*",
    Script: "api-handler"));

Console.WriteLine($"Created route: {route.Id}");

Exceptions

ArgumentException

Thrown when zoneId is null or whitespace.

ArgumentNullException

Thrown when request is null.

DeleteRouteAsync(string, string, CancellationToken)

Deletes a Worker route.

Task DeleteRouteAsync(string zoneId, string routeId, CancellationToken cancellationToken = default)

Parameters

zoneId string

The zone identifier.

routeId string

The route identifier.

cancellationToken CancellationToken

A cancellation token.

Returns

Task

Examples

await client.Workers.DeleteRouteAsync(zoneId, routeId);
Console.WriteLine($"Deleted route: {routeId}");

Exceptions

ArgumentException

Thrown when zoneId or routeId is null or whitespace.

GetRouteAsync(string, string, CancellationToken)

Gets details for a specific Worker route.

Task<WorkerRoute> GetRouteAsync(string zoneId, string routeId, CancellationToken cancellationToken = default)

Parameters

zoneId string

The zone identifier.

routeId string

The route identifier.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<WorkerRoute>

The Worker route details.

Examples

var route = await client.Workers.GetRouteAsync(zoneId, routeId);
Console.WriteLine($"Pattern: {route.Pattern}, Script: {route.Script}");

Exceptions

ArgumentException

Thrown when zoneId or routeId is null or whitespace.

ListRoutesAsync(string, CancellationToken)

Lists all Worker routes for a zone.

Task<IReadOnlyList<WorkerRoute>> ListRoutesAsync(string zoneId, CancellationToken cancellationToken = default)

Parameters

zoneId string

The zone identifier.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<IReadOnlyList<WorkerRoute>>

All Worker routes in the zone.

Examples

var routes = await client.Workers.ListRoutesAsync(zoneId);

foreach (var route in routes)
{
  Console.WriteLine($"{route.Pattern} -> {route.Script ?? "(disabled)"}");
}

Exceptions

ArgumentException

Thrown when zoneId is null or whitespace.

UpdateRouteAsync(string, string, UpdateWorkerRouteRequest, CancellationToken)

Updates an existing Worker route.

Task<WorkerRoute> UpdateRouteAsync(string zoneId, string routeId, UpdateWorkerRouteRequest request, CancellationToken cancellationToken = default)

Parameters

zoneId string

The zone identifier.

routeId string

The route identifier.

request UpdateWorkerRouteRequest

The update parameters.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<WorkerRoute>

The updated Worker route.

Examples

var updated = await client.Workers.UpdateRouteAsync(zoneId, routeId,
  new UpdateWorkerRouteRequest(
    Pattern: "api.example.com/v2/*",
    Script: "api-handler-v2"));

Exceptions

ArgumentException

Thrown when zoneId or routeId is null or whitespace.

ArgumentNullException

Thrown when request is null.