Table of Contents

Class WorkersApi

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

Implementation of the Workers API client for managing Worker routes.

public class WorkersApi : ApiResource, IWorkersApi
Inheritance
WorkersApi
Implements
Inherited Members

Constructors

WorkersApi(HttpClient, ILoggerFactory)

Initializes a new instance of the WorkersApi class.

public WorkersApi(HttpClient httpClient, ILoggerFactory loggerFactory)

Parameters

httpClient HttpClient

The HttpClient for making requests.

loggerFactory ILoggerFactory

The factory to create loggers for this resource.

Methods

CreateRouteAsync(string, CreateWorkerRouteRequest, CancellationToken)

Creates a new Worker route.

public 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.

public 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.

public 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.

public 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.

public 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.