Table of Contents

Class KvApi

Namespace
Cloudflare.NET.Accounts.Kv
Assembly
Cloudflare.NET.dll

Implementation of Workers KV API operations.

public class KvApi : ApiResource, IKvApi
Inheritance
KvApi
Implements
Inherited Members

Constructors

KvApi(HttpClient, IOptions<CloudflareApiOptions>, ILoggerFactory)

Initializes a new instance of the KvApi class.

public KvApi(HttpClient httpClient, IOptions<CloudflareApiOptions> options, ILoggerFactory loggerFactory)

Parameters

httpClient HttpClient

The HttpClient for making requests.

options IOptions<CloudflareApiOptions>

The Cloudflare API options containing the account ID.

loggerFactory ILoggerFactory

The factory to create loggers.

Methods

BulkDeleteAsync(string, IEnumerable<string>, CancellationToken)

Deletes multiple keys in a single request.

public Task<KvBulkDeleteResult> BulkDeleteAsync(string namespaceId, IEnumerable<string> keys, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

keys IEnumerable<string>

Keys to delete (max 10,000 keys).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<KvBulkDeleteResult>

Result indicating success/failure counts.

Remarks

The request body is a simple JSON array of key names: ["key1", "key2"].

See Also

BulkGetAsync(string, IEnumerable<string>, CancellationToken)

Retrieves multiple values in a single request.

public Task<IReadOnlyDictionary<string, string?>> BulkGetAsync(string namespaceId, IEnumerable<string> keys, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

keys IEnumerable<string>

Keys to retrieve (max 100 keys).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<IReadOnlyDictionary<string, string>>

Dictionary of key-value pairs (value is null for non-existent keys).

Remarks

The API returns values as a dictionary: { "key1": "value1", "key2": "value2" }. Use BulkGetWithMetadataAsync(string, IEnumerable<string>, CancellationToken) if you need metadata for each key.

See Also

BulkGetWithMetadataAsync(string, IEnumerable<string>, CancellationToken)

Retrieves multiple values with metadata in a single request.

public Task<IReadOnlyDictionary<string, KvBulkGetItemWithMetadata?>> BulkGetWithMetadataAsync(string namespaceId, IEnumerable<string> keys, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

keys IEnumerable<string>

Keys to retrieve (max 100 keys).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<IReadOnlyDictionary<string, KvBulkGetItemWithMetadata>>

Dictionary of key-value pairs with metadata.

See Also

BulkWriteAsync(string, IEnumerable<KvBulkWriteItem>, CancellationToken)

Writes multiple key-value pairs in a single request.

public Task<KvBulkWriteResult> BulkWriteAsync(string namespaceId, IEnumerable<KvBulkWriteItem> items, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

items IEnumerable<KvBulkWriteItem>

Key-value pairs to write (max 10,000 items, 100MB total).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<KvBulkWriteResult>

Result indicating success/failure counts.

Remarks

Existing values and expirations will be overwritten. Check UnsuccessfulKeys and retry if needed.

See Also

CreateAsync(string, CancellationToken)

Creates a new KV namespace.

public Task<KvNamespace> CreateAsync(string title, CancellationToken cancellationToken = default)

Parameters

title string

The title for the new namespace. Must be unique per account.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<KvNamespace>

The created namespace.

Exceptions

CloudflareApiException

Thrown if title already exists (code 10014).

See Also

DeleteAsync(string, CancellationToken)

Deletes a KV namespace and all its keys.

public Task DeleteAsync(string namespaceId, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID to delete.

cancellationToken CancellationToken

Cancellation token.

Returns

Task
See Also

DeleteValueAsync(string, string, CancellationToken)

Deletes a key-value pair.

public Task DeleteValueAsync(string namespaceId, string key, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name to delete.

cancellationToken CancellationToken

Cancellation token.

Returns

Task
See Also

GetAsync(string, CancellationToken)

Gets a specific KV namespace by ID.

public Task<KvNamespace> GetAsync(string namespaceId, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<KvNamespace>

The namespace details.

See Also

GetMetadataAsync(string, string, CancellationToken)

Reads only the metadata for a key (without retrieving the value).

public Task<JsonElement?> GetMetadataAsync(string namespaceId, string key, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<JsonElement?>

The metadata as JSON, or null if no metadata exists.

See Also

GetValueAsync(string, string, CancellationToken)

Reads a value by key as a string.

public Task<string?> GetValueAsync(string namespaceId, string key, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<string>

The value as a string, or null if the key doesn't exist.

Remarks

Use GetValueWithExpirationAsync(string, string, CancellationToken) if you need the expiration timestamp.

See Also

GetValueBytesAsync(string, string, CancellationToken)

Reads a value by key as bytes.

public Task<byte[]?> GetValueBytesAsync(string namespaceId, string key, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<byte[]>

The value as bytes, or null if the key doesn't exist.

Remarks

See Also

GetValueBytesWithExpirationAsync(string, string, CancellationToken)

Reads a value by key as bytes, including expiration info.

public Task<KvValueResult?> GetValueBytesWithExpirationAsync(string namespaceId, string key, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<KvValueResult>

The value and expiration, or null if the key doesn't exist.

See Also

GetValueWithExpirationAsync(string, string, CancellationToken)

Reads a value by key as a string, including expiration info.

public Task<KvStringValueResult?> GetValueWithExpirationAsync(string namespaceId, string key, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<KvStringValueResult>

The value and expiration, or null if the key doesn't exist.

See Also

ListAllAsync(ListKvNamespacesFilters?, CancellationToken)

Lists all KV namespaces, automatically handling pagination.

public IAsyncEnumerable<KvNamespace> ListAllAsync(ListKvNamespacesFilters? filters = null, CancellationToken cancellationToken = default)

Parameters

filters ListKvNamespacesFilters

Optional filters for ordering.

cancellationToken CancellationToken

Cancellation token.

Returns

IAsyncEnumerable<KvNamespace>

An async enumerable of all namespaces.

ListAllKeysAsync(string, string?, CancellationToken)

Lists all keys in a namespace, automatically handling pagination.

public IAsyncEnumerable<KvKey> ListAllKeysAsync(string namespaceId, string? prefix = null, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

prefix string

Optional prefix filter.

cancellationToken CancellationToken

Cancellation token.

Returns

IAsyncEnumerable<KvKey>

An async enumerable of all keys.

ListAsync(ListKvNamespacesFilters?, CancellationToken)

Lists KV namespaces in the account.

public Task<PagePaginatedResult<KvNamespace>> ListAsync(ListKvNamespacesFilters? filters = null, CancellationToken cancellationToken = default)

Parameters

filters ListKvNamespacesFilters

Optional filters for pagination and ordering.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<PagePaginatedResult<KvNamespace>>

A page of namespaces with pagination info.

See Also

ListKeysAsync(string, ListKvKeysFilters?, CancellationToken)

Lists keys in a namespace with cursor-based pagination.

public Task<CursorPaginatedResult<KvKey>> ListKeysAsync(string namespaceId, ListKvKeysFilters? filters = null, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

filters ListKvKeysFilters

Optional filters for prefix, limit, and cursor.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<CursorPaginatedResult<KvKey>>

Keys and pagination info (use CursorInfo.Cursor for next page).

See Also

RenameAsync(string, string, CancellationToken)

Renames a KV namespace.

public Task<KvNamespace> RenameAsync(string namespaceId, string title, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID to rename.

title string

The new title for the namespace. Must be unique per account.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<KvNamespace>

The updated namespace with id, title, and supports_url_encoding.

See Also

WriteValueAsync(string, string, byte[], KvWriteOptions?, CancellationToken)

Writes a binary value.

public Task WriteValueAsync(string namespaceId, string key, byte[] value, KvWriteOptions? options = null, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name (max 512 bytes).

value byte[]

The value to store (max 25 MiB).

options KvWriteOptions

Optional expiration and metadata settings.

cancellationToken CancellationToken

Cancellation token.

Returns

Task
See Also

WriteValueAsync(string, string, string, KvWriteOptions?, CancellationToken)

Writes a string value.

public Task WriteValueAsync(string namespaceId, string key, string value, KvWriteOptions? options = null, CancellationToken cancellationToken = default)

Parameters

namespaceId string

The namespace ID.

key string

The key name (max 512 bytes).

value string

The value to store (max 25 MiB).

options KvWriteOptions

Optional expiration and metadata settings.

cancellationToken CancellationToken

Cancellation token.

Returns

Task
See Also