Interface IKvApi
- Namespace
- Cloudflare.NET.Accounts.Kv
- Assembly
- Cloudflare.NET.dll
Provides access to Cloudflare Workers KV operations. Workers KV is a global, low-latency key-value data store.
public interface IKvApi
Remarks
All operations are account-scoped. The account ID is configured via AccountId.
API Limits:
- Key name max length: 512 bytes
- Value max size: 25 MiB
- Metadata max size: 1024 bytes (serialized JSON)
- Minimum TTL: 60 seconds
- Write rate limit: 1 per second per key
Methods
BulkDeleteAsync(string, IEnumerable<string>, CancellationToken)
Deletes multiple keys in a single request.
Task<KvBulkDeleteResult> BulkDeleteAsync(string namespaceId, IEnumerable<string> keys, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keysIEnumerable<string>Keys to delete (max 10,000 keys).
cancellationTokenCancellationTokenCancellation 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.
Task<IReadOnlyDictionary<string, string?>> BulkGetAsync(string namespaceId, IEnumerable<string> keys, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keysIEnumerable<string>Keys to retrieve (max 100 keys).
cancellationTokenCancellationTokenCancellation 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.
Task<IReadOnlyDictionary<string, KvBulkGetItemWithMetadata?>> BulkGetWithMetadataAsync(string namespaceId, IEnumerable<string> keys, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keysIEnumerable<string>Keys to retrieve (max 100 keys).
cancellationTokenCancellationTokenCancellation 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.
Task<KvBulkWriteResult> BulkWriteAsync(string namespaceId, IEnumerable<KvBulkWriteItem> items, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
itemsIEnumerable<KvBulkWriteItem>Key-value pairs to write (max 10,000 items, 100MB total).
cancellationTokenCancellationTokenCancellation 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.
Task<KvNamespace> CreateAsync(string title, CancellationToken cancellationToken = default)
Parameters
titlestringThe title for the new namespace. Must be unique per account.
cancellationTokenCancellationTokenCancellation 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.
Task DeleteAsync(string namespaceId, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID to delete.
cancellationTokenCancellationTokenCancellation token.
Returns
- See Also
DeleteValueAsync(string, string, CancellationToken)
Deletes a key-value pair.
Task DeleteValueAsync(string namespaceId, string key, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name to delete.
cancellationTokenCancellationTokenCancellation token.
Returns
- See Also
GetAsync(string, CancellationToken)
Gets a specific KV namespace by ID.
Task<KvNamespace> GetAsync(string namespaceId, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<KvNamespace>
The namespace details.
- See Also
GetMetadataAsync(string, string, CancellationToken)
Reads only the metadata for a key (without retrieving the value).
Task<JsonElement?> GetMetadataAsync(string namespaceId, string key, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name.
cancellationTokenCancellationTokenCancellation 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.
Task<string?> GetValueAsync(string namespaceId, string key, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name.
cancellationTokenCancellationTokenCancellation token.
Returns
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.
Task<byte[]?> GetValueBytesAsync(string namespaceId, string key, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Use GetValueBytesWithExpirationAsync(string, string, CancellationToken) if you need the expiration timestamp.
- See Also
GetValueBytesWithExpirationAsync(string, string, CancellationToken)
Reads a value by key as bytes, including expiration info.
Task<KvValueResult?> GetValueBytesWithExpirationAsync(string namespaceId, string key, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name.
cancellationTokenCancellationTokenCancellation 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.
Task<KvStringValueResult?> GetValueWithExpirationAsync(string namespaceId, string key, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name.
cancellationTokenCancellationTokenCancellation 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.
IAsyncEnumerable<KvNamespace> ListAllAsync(ListKvNamespacesFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
filtersListKvNamespacesFiltersOptional filters for ordering.
cancellationTokenCancellationTokenCancellation token.
Returns
- IAsyncEnumerable<KvNamespace>
An async enumerable of all namespaces.
ListAllKeysAsync(string, string?, CancellationToken)
Lists all keys in a namespace, automatically handling pagination.
IAsyncEnumerable<KvKey> ListAllKeysAsync(string namespaceId, string? prefix = null, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
prefixstringOptional prefix filter.
cancellationTokenCancellationTokenCancellation token.
Returns
- IAsyncEnumerable<KvKey>
An async enumerable of all keys.
ListAsync(ListKvNamespacesFilters?, CancellationToken)
Lists KV namespaces in the account.
Task<PagePaginatedResult<KvNamespace>> ListAsync(ListKvNamespacesFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
filtersListKvNamespacesFiltersOptional filters for pagination and ordering.
cancellationTokenCancellationTokenCancellation 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.
Task<CursorPaginatedResult<KvKey>> ListKeysAsync(string namespaceId, ListKvKeysFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
filtersListKvKeysFiltersOptional filters for prefix, limit, and cursor.
cancellationTokenCancellationTokenCancellation 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.
Task<KvNamespace> RenameAsync(string namespaceId, string title, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID to rename.
titlestringThe new title for the namespace. Must be unique per account.
cancellationTokenCancellationTokenCancellation 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.
Task WriteValueAsync(string namespaceId, string key, byte[] value, KvWriteOptions? options = null, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name (max 512 bytes).
valuebyte[]The value to store (max 25 MiB).
optionsKvWriteOptionsOptional expiration and metadata settings.
cancellationTokenCancellationTokenCancellation token.
Returns
- See Also
WriteValueAsync(string, string, string, KvWriteOptions?, CancellationToken)
Writes a string value.
Task WriteValueAsync(string namespaceId, string key, string value, KvWriteOptions? options = null, CancellationToken cancellationToken = default)
Parameters
namespaceIdstringThe namespace ID.
keystringThe key name (max 512 bytes).
valuestringThe value to store (max 25 MiB).
optionsKvWriteOptionsOptional expiration and metadata settings.
cancellationTokenCancellationTokenCancellation token.
Returns
- See Also