Table of Contents

Class D1Api

Namespace
Cloudflare.NET.Accounts.D1
Assembly
Cloudflare.NET.dll

Implementation of Cloudflare D1 Database API operations.

public class D1Api : ApiResource, ID1Api
Inheritance
D1Api
Implements
Inherited Members

Remarks

D1 is Cloudflare's native serverless SQL database built on SQLite. All operations are account-scoped, using the account ID from configuration.

The REST API is subject to global Cloudflare API rate limits and is best suited for administrative operations. For high-throughput query access outside Workers, consider creating a proxy Worker with D1 Worker bindings.

Constructors

D1Api(HttpClient, IOptions<CloudflareApiOptions>, ILoggerFactory)

Initializes a new instance of the D1Api class.

public D1Api(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

CompleteImportAsync(string, string, string, CancellationToken)

Completes an import after uploading the SQL file.

public Task<D1ImportResponse> CompleteImportAsync(string databaseId, string etag, string filename, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

etag string

The MD5 hash/ETag of the uploaded file.

filename string

The filename returned from StartImportAsync(string, string, CancellationToken).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1ImportResponse>

Import response with bookmark for polling.

CreateAsync(string, R2LocationHint?, D1Jurisdiction?, CancellationToken)

Creates a new D1 database.

public Task<D1Database> CreateAsync(string name, R2LocationHint? primaryLocationHint = null, D1Jurisdiction? jurisdiction = null, CancellationToken cancellationToken = default)

Parameters

name string

The name for the new database.

primaryLocationHint R2LocationHint?

Optional region hint for database placement.

jurisdiction D1Jurisdiction?

Optional jurisdictional restriction (overrides location hint).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1Database>

The created database.

See Also

DeleteAsync(string, CancellationToken)

Deletes a D1 database.

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

Parameters

databaseId string

The database UUID to delete.

cancellationToken CancellationToken

Cancellation token.

Returns

Task

Remarks

This permanently deletes the database and all its data. The operation cannot be undone.

See Also

GetAsync(string, CancellationToken)

Gets a specific D1 database by ID.

public Task<D1Database> GetAsync(string databaseId, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1Database>

The database details.

See Also

ListAllAsync(ListD1DatabasesFilters?, CancellationToken)

Lists all D1 databases, automatically handling pagination.

public IAsyncEnumerable<D1Database> ListAllAsync(ListD1DatabasesFilters? filters = null, CancellationToken cancellationToken = default)

Parameters

filters ListD1DatabasesFilters

Optional filters for name search.

cancellationToken CancellationToken

Cancellation token.

Returns

IAsyncEnumerable<D1Database>

An async enumerable of all databases.

Remarks

The D1 API returns total_count=0 and total_pages=0 in the pagination info, which is inconsistent with the standard Cloudflare API schema. To work around this limitation, this implementation checks if the current page is full (item count equals PerPage) to determine if there are more pages.

ListAsync(ListD1DatabasesFilters?, CancellationToken)

Lists D1 databases in the account.

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

Parameters

filters ListD1DatabasesFilters

Optional filters for pagination and name search.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<PagePaginatedResult<D1Database>>

A page of databases with pagination info.

See Also

PollExportAsync(string, string, CancellationToken)

Polls the status of an ongoing export operation.

public Task<D1ExportResponse> PollExportAsync(string databaseId, string bookmark, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

bookmark string

The bookmark from the previous response.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1ExportResponse>

Export status. When complete, includes signed_url for download.

PollImportAsync(string, string, CancellationToken)

Polls the status of an ongoing import operation.

public Task<D1ImportResponse> PollImportAsync(string databaseId, string bookmark, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

bookmark string

The bookmark from the previous response.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1ImportResponse>

Import status with progress information.

QueryAsync(string, string, IReadOnlyList<object?>?, CancellationToken)

Executes a SQL query and returns results as objects.

public Task<IReadOnlyList<D1QueryResult>> QueryAsync(string databaseId, string sql, IReadOnlyList<object?>? @params = null, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

sql string

The SQL statement to execute.

params IReadOnlyList<object>

Optional parameter values for parameterized queries.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<IReadOnlyList<D1QueryResult>>

Query results with metadata.

Remarks

Use ? placeholders in SQL and provide values in params array. Multiple statements can be separated by semicolons for batch execution.

See Also

QueryAsync<TRow>(string, string, IReadOnlyList<object?>?, CancellationToken)

Executes a SQL query and returns strongly-typed results.

public Task<IReadOnlyList<D1QueryResult<TRow>>> QueryAsync<TRow>(string databaseId, string sql, IReadOnlyList<object?>? @params = null, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

sql string

The SQL statement to execute.

params IReadOnlyList<object>

Optional parameter values for parameterized queries.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<IReadOnlyList<D1QueryResult<TRow>>>

Typed query results with metadata.

Type Parameters

TRow

The type to deserialize each row into.

QueryRawAsync(string, string, IReadOnlyList<object?>?, CancellationToken)

Executes a SQL query and returns results as arrays (raw format).

public Task<IReadOnlyList<D1RawQueryResult>> QueryRawAsync(string databaseId, string sql, IReadOnlyList<object?>? @params = null, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

sql string

The SQL statement to execute.

params IReadOnlyList<object>

Optional parameter values for parameterized queries.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<IReadOnlyList<D1RawQueryResult>>

Raw query results with rows as arrays.

Remarks

The raw endpoint returns rows as arrays instead of objects, which is more efficient for large result sets. Column order matches the SELECT clause order.

See Also

StartExportAsync(string, D1ExportDumpOptions?, CancellationToken)

Initiates an export of the database to SQL format.

public Task<D1ExportResponse> StartExportAsync(string databaseId, D1ExportDumpOptions? dumpOptions = null, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

dumpOptions D1ExportDumpOptions

Optional filtering options (schema only, data only, specific tables).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1ExportResponse>

Initial export response with bookmark for polling.

Remarks

Export is an async operation that requires polling. Call PollExportAsync(string, string, CancellationToken) with the returned bookmark until status is "complete".

The database is unavailable for queries during export. Exports that aren't polled will auto-cancel.

See Also

StartImportAsync(string, string, CancellationToken)

Initiates an import operation by requesting an upload URL.

public Task<D1ImportResponse> StartImportAsync(string databaseId, string etag, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

etag string

MD5 hash of the SQL file content.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1ImportResponse>

Response containing upload_url and filename for SQL file upload.

Remarks

Import workflow:

  1. Compute MD5 hash of your SQL file
  2. Call StartImportAsync(string, string, CancellationToken) with the hash to get upload URL and filename
  3. PUT your SQL file to the upload_url (verify ETag matches)
  4. Call CompleteImportAsync(string, string, string, CancellationToken) with etag and filename to start ingestion
  5. Poll with PollImportAsync(string, string, CancellationToken) until status is "complete"

The database is blocked during import operations.

See Also

UpdateAsync(string, UpdateD1DatabaseOptions, CancellationToken)

Updates a D1 database's configuration.

public Task<D1Database> UpdateAsync(string databaseId, UpdateD1DatabaseOptions options, CancellationToken cancellationToken = default)

Parameters

databaseId string

The database UUID.

options UpdateD1DatabaseOptions

The update options (e.g., read replication settings).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<D1Database>

The updated database.

Remarks

Use this to enable/disable read replication by setting ReadReplication.Mode to "auto" or "disabled".

See Also