# D1 ## Domain Types ### D1 - `D1` The details of the D1 database. - `created_at?: string` Specifies the timestamp the resource was created as an ISO8601 string. - `file_size?: number` The D1 database's size, in bytes. - `jurisdiction?: "eu" | "fedramp" | null` Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `name?: string` D1 database name. - `num_tables?: number` - `read_replication?: ReadReplication` Configuration for D1 read replication. - `mode: "auto" | "disabled"` The read replication mode for the database. Use 'auto' to create replicas and allow D1 automatically place them around the world, or 'disabled' to not use any database replicas (it can take a few hours for all replicas to be deleted). - `"auto"` - `"disabled"` - `uuid?: string` D1 database identifier (UUID). - `version?: string` # Database ## List D1 Databases `client.d1.database.list(DatabaseListParamsparams, RequestOptionsoptions?): V4PagePaginationArray` **get** `/accounts/{account_id}/d1/database` Returns a list of D1 databases. ### Parameters - `params: DatabaseListParams` - `account_id: string` Path param: Account identifier tag. - `name?: string` Query param: a database name to search for. - `page?: number` Query param: Page number of paginated results. - `per_page?: number` Query param: Number of items per page. ### Returns - `DatabaseListResponse` - `created_at?: string` Specifies the timestamp the resource was created as an ISO8601 string. - `jurisdiction?: "eu" | "fedramp" | null` Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `name?: string` D1 database name. - `uuid?: string` D1 database identifier (UUID). - `version?: string` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); // Automatically fetches more pages as needed. for await (const databaseListResponse of client.d1.database.list({ account_id: '023e105f4ecef8ad9ca31a8372d0c353', })) { console.log(databaseListResponse.uuid); } ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": [ { "created_at": "2022-11-15T18:25:44.442097Z", "jurisdiction": "eu", "name": "my-database", "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "version": "production" } ], "success": true, "result_info": { "count": 1, "page": 1, "per_page": 20, "total_count": 2000 } } ``` ## Get D1 Database `client.d1.database.get(stringdatabaseId, DatabaseGetParamsparams, RequestOptionsoptions?): D1` **get** `/accounts/{account_id}/d1/database/{database_id}` Returns the specified D1 database. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `params: DatabaseGetParams` - `account_id: string` Account identifier tag. ### Returns - `D1` The details of the D1 database. - `created_at?: string` Specifies the timestamp the resource was created as an ISO8601 string. - `file_size?: number` The D1 database's size, in bytes. - `jurisdiction?: "eu" | "fedramp" | null` Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `name?: string` D1 database name. - `num_tables?: number` - `read_replication?: ReadReplication` Configuration for D1 read replication. - `mode: "auto" | "disabled"` The read replication mode for the database. Use 'auto' to create replicas and allow D1 automatically place them around the world, or 'disabled' to not use any database replicas (it can take a few hours for all replicas to be deleted). - `"auto"` - `"disabled"` - `uuid?: string` D1 database identifier (UUID). - `version?: string` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const d1 = await client.d1.database.get('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', }); console.log(d1.uuid); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "created_at": "2022-11-15T18:25:44.442097Z", "file_size": 12, "jurisdiction": "eu", "name": "my-database", "num_tables": 12, "read_replication": { "mode": "auto" }, "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "version": "production" }, "success": true } ``` ## Create D1 Database `client.d1.database.create(DatabaseCreateParamsparams, RequestOptionsoptions?): D1` **post** `/accounts/{account_id}/d1/database` Returns the created D1 database. ### Parameters - `params: DatabaseCreateParams` - `account_id: string` Path param: Account identifier tag. - `name: string` Body param: D1 database name. - `jurisdiction?: "eu" | "fedramp"` Body param: Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `primary_location_hint?: "wnam" | "enam" | "weur" | 3 more` Body param: Specify the region to create the D1 primary, if available. If this option is omitted, the D1 will be created as close as possible to the current user. - `"wnam"` - `"enam"` - `"weur"` - `"eeur"` - `"apac"` - `"oc"` ### Returns - `D1` The details of the D1 database. - `created_at?: string` Specifies the timestamp the resource was created as an ISO8601 string. - `file_size?: number` The D1 database's size, in bytes. - `jurisdiction?: "eu" | "fedramp" | null` Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `name?: string` D1 database name. - `num_tables?: number` - `read_replication?: ReadReplication` Configuration for D1 read replication. - `mode: "auto" | "disabled"` The read replication mode for the database. Use 'auto' to create replicas and allow D1 automatically place them around the world, or 'disabled' to not use any database replicas (it can take a few hours for all replicas to be deleted). - `"auto"` - `"disabled"` - `uuid?: string` D1 database identifier (UUID). - `version?: string` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const d1 = await client.d1.database.create({ account_id: '023e105f4ecef8ad9ca31a8372d0c353', name: 'my-database', }); console.log(d1.uuid); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "created_at": "2022-11-15T18:25:44.442097Z", "file_size": 12, "jurisdiction": "eu", "name": "my-database", "num_tables": 12, "read_replication": { "mode": "auto" }, "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "version": "production" }, "success": true } ``` ## Update D1 Database `client.d1.database.update(stringdatabaseId, DatabaseUpdateParamsparams, RequestOptionsoptions?): D1` **put** `/accounts/{account_id}/d1/database/{database_id}` Updates the specified D1 database. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `params: DatabaseUpdateParams` - `account_id: string` Path param: Account identifier tag. - `read_replication: ReadReplication` Body param: Configuration for D1 read replication. - `mode: "auto" | "disabled"` The read replication mode for the database. Use 'auto' to create replicas and allow D1 automatically place them around the world, or 'disabled' to not use any database replicas (it can take a few hours for all replicas to be deleted). - `"auto"` - `"disabled"` ### Returns - `D1` The details of the D1 database. - `created_at?: string` Specifies the timestamp the resource was created as an ISO8601 string. - `file_size?: number` The D1 database's size, in bytes. - `jurisdiction?: "eu" | "fedramp" | null` Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `name?: string` D1 database name. - `num_tables?: number` - `read_replication?: ReadReplication` Configuration for D1 read replication. - `mode: "auto" | "disabled"` The read replication mode for the database. Use 'auto' to create replicas and allow D1 automatically place them around the world, or 'disabled' to not use any database replicas (it can take a few hours for all replicas to be deleted). - `"auto"` - `"disabled"` - `uuid?: string` D1 database identifier (UUID). - `version?: string` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const d1 = await client.d1.database.update('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', read_replication: { mode: 'auto' }, }); console.log(d1.uuid); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "created_at": "2022-11-15T18:25:44.442097Z", "file_size": 12, "jurisdiction": "eu", "name": "my-database", "num_tables": 12, "read_replication": { "mode": "auto" }, "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "version": "production" }, "success": true } ``` ## Update D1 Database partially `client.d1.database.edit(stringdatabaseId, DatabaseEditParamsparams, RequestOptionsoptions?): D1` **patch** `/accounts/{account_id}/d1/database/{database_id}` Updates partially the specified D1 database. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `params: DatabaseEditParams` - `account_id: string` Path param: Account identifier tag. - `read_replication?: ReadReplication` Body param: Configuration for D1 read replication. - `mode: "auto" | "disabled"` The read replication mode for the database. Use 'auto' to create replicas and allow D1 automatically place them around the world, or 'disabled' to not use any database replicas (it can take a few hours for all replicas to be deleted). - `"auto"` - `"disabled"` ### Returns - `D1` The details of the D1 database. - `created_at?: string` Specifies the timestamp the resource was created as an ISO8601 string. - `file_size?: number` The D1 database's size, in bytes. - `jurisdiction?: "eu" | "fedramp" | null` Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `name?: string` D1 database name. - `num_tables?: number` - `read_replication?: ReadReplication` Configuration for D1 read replication. - `mode: "auto" | "disabled"` The read replication mode for the database. Use 'auto' to create replicas and allow D1 automatically place them around the world, or 'disabled' to not use any database replicas (it can take a few hours for all replicas to be deleted). - `"auto"` - `"disabled"` - `uuid?: string` D1 database identifier (UUID). - `version?: string` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const d1 = await client.d1.database.edit('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', }); console.log(d1.uuid); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "created_at": "2022-11-15T18:25:44.442097Z", "file_size": 12, "jurisdiction": "eu", "name": "my-database", "num_tables": 12, "read_replication": { "mode": "auto" }, "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "version": "production" }, "success": true } ``` ## Delete D1 Database `client.d1.database.delete(stringdatabaseId, DatabaseDeleteParamsparams, RequestOptionsoptions?): DatabaseDeleteResponse | null` **delete** `/accounts/{account_id}/d1/database/{database_id}` Deletes the specified D1 database. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `params: DatabaseDeleteParams` - `account_id: string` Account identifier tag. ### Returns - `DatabaseDeleteResponse = unknown` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const database = await client.d1.database.delete('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', }); console.log(database); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": {}, "success": true } ``` ## Query D1 Database `client.d1.database.query(stringdatabaseId, DatabaseQueryParamsparams, RequestOptionsoptions?): SinglePage` **post** `/accounts/{account_id}/d1/database/{database_id}/query` Returns the query result as an object. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `DatabaseQueryParams = D1SingleQuery | MultipleQueries` - `DatabaseQueryParamsBase` - `D1SingleQuery extends DatabaseQueryParamsBase` - `MultipleQueries extends DatabaseQueryParamsBase` ### Returns - `QueryResult` - `meta?: Meta` - `changed_db?: boolean` Denotes if the database has been altered in some way, like deleting rows. - `changes?: number` Rough indication of how many rows were modified by the query, as provided by SQLite's `sqlite3_total_changes()`. - `duration?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `last_row_id?: number` The row ID of the last inserted row in a table with an `INTEGER PRIMARY KEY` as provided by SQLite. Tables created with `WITHOUT ROWID` do not populate this. - `rows_read?: number` Number of rows read during the SQL query execution, including indices (not all rows are necessarily returned). - `rows_written?: number` Number of rows written during the SQL query execution, including indices. - `served_by_colo?: string` The three letters airport code of the colo that handled the query. - `served_by_primary?: boolean` Denotes if the query has been handled by the database primary instance. - `served_by_region?: "WNAM" | "ENAM" | "WEUR" | 3 more` Region location hint of the database instance that handled the query. - `"WNAM"` - `"ENAM"` - `"WEUR"` - `"EEUR"` - `"APAC"` - `"OC"` - `size_after?: number` Size of the database after the query committed, in bytes. - `timings?: Timings` Various durations for the query. - `sql_duration_ms?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `results?: Array` - `success?: boolean` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); // Automatically fetches more pages as needed. for await (const queryResult of client.d1.database.query('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', sql: 'SELECT * FROM myTable WHERE field = ? OR field = ?;', })) { console.log(queryResult.meta); } ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": [ { "meta": { "changed_db": true, "changes": 0, "duration": 0, "last_row_id": 0, "rows_read": 0, "rows_written": 0, "served_by_colo": "LHR", "served_by_primary": true, "served_by_region": "EEUR", "size_after": 0, "timings": { "sql_duration_ms": 0 } }, "results": [ {} ], "success": true } ], "success": true } ``` ## Raw D1 Database query `client.d1.database.raw(stringdatabaseId, DatabaseRawParamsparams, RequestOptionsoptions?): SinglePage` **post** `/accounts/{account_id}/d1/database/{database_id}/raw` Returns the query result rows as arrays rather than objects. This is a performance-optimized version of the /query endpoint. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `DatabaseRawParams = D1SingleQuery | MultipleQueries` - `DatabaseRawParamsBase` - `D1SingleQuery extends DatabaseRawParamsBase` - `MultipleQueries extends DatabaseRawParamsBase` ### Returns - `DatabaseRawResponse` - `meta?: Meta` - `changed_db?: boolean` Denotes if the database has been altered in some way, like deleting rows. - `changes?: number` Rough indication of how many rows were modified by the query, as provided by SQLite's `sqlite3_total_changes()`. - `duration?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `last_row_id?: number` The row ID of the last inserted row in a table with an `INTEGER PRIMARY KEY` as provided by SQLite. Tables created with `WITHOUT ROWID` do not populate this. - `rows_read?: number` Number of rows read during the SQL query execution, including indices (not all rows are necessarily returned). - `rows_written?: number` Number of rows written during the SQL query execution, including indices. - `served_by_colo?: string` The three letters airport code of the colo that handled the query. - `served_by_primary?: boolean` Denotes if the query has been handled by the database primary instance. - `served_by_region?: "WNAM" | "ENAM" | "WEUR" | 3 more` Region location hint of the database instance that handled the query. - `"WNAM"` - `"ENAM"` - `"WEUR"` - `"EEUR"` - `"APAC"` - `"OC"` - `size_after?: number` Size of the database after the query committed, in bytes. - `timings?: Timings` Various durations for the query. - `sql_duration_ms?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `results?: Results` - `columns?: Array` - `rows?: Array>` - `number` - `string` - `unknown` - `success?: boolean` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); // Automatically fetches more pages as needed. for await (const databaseRawResponse of client.d1.database.raw( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', sql: 'SELECT * FROM myTable WHERE field = ? OR field = ?;', }, )) { console.log(databaseRawResponse.meta); } ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": [ { "meta": { "changed_db": true, "changes": 0, "duration": 0, "last_row_id": 0, "rows_read": 0, "rows_written": 0, "served_by_colo": "LHR", "served_by_primary": true, "served_by_region": "EEUR", "size_after": 0, "timings": { "sql_duration_ms": 0 } }, "results": { "columns": [ "string" ], "rows": [ [ 0 ] ] }, "success": true } ], "success": true } ``` ## Export D1 Database as SQL `client.d1.database.export(stringdatabaseId, DatabaseExportParamsparams, RequestOptionsoptions?): DatabaseExportResponse` **post** `/accounts/{account_id}/d1/database/{database_id}/export` Returns a URL where the SQL contents of your D1 can be downloaded. Note: this process may take some time for larger DBs, during which your D1 will be unavailable to serve queries. To avoid blocking your DB unnecessarily, an in-progress export must be continually polled or will automatically cancel. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `params: DatabaseExportParams` - `account_id: string` Path param: Account identifier tag. - `output_format: "polling"` Body param: Specifies that you will poll this endpoint until the export completes - `"polling"` - `current_bookmark?: string` Body param: To poll an in-progress export, provide the current bookmark (returned by your first polling response) - `dump_options?: DumpOptions` Body param - `no_data?: boolean` Export only the table definitions, not their contents - `no_schema?: boolean` Export only each table's contents, not its definition - `tables?: Array` Filter the export to just one or more tables. Passing an empty array is the same as not passing anything and means: export all tables. ### Returns - `DatabaseExportResponse` - `at_bookmark?: string` The current time-travel bookmark for your D1, used to poll for updates. Will not change for the duration of the export task. - `error?: string` Only present when status = 'error'. Contains the error message. - `messages?: Array` Logs since the last time you polled - `result?: Result` Only present when status = 'complete' - `filename?: string` The generated SQL filename. - `signed_url?: string` The URL to download the exported SQL. Available for one hour. - `status?: "complete" | "error"` - `"complete"` - `"error"` - `success?: boolean` - `type?: "export"` - `"export"` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const response = await client.d1.database.export('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', output_format: 'polling', }); console.log(response.at_bookmark); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "at_bookmark": "at_bookmark", "error": "error", "messages": [ "string" ], "result": { "filename": "filename", "signed_url": "signed_url" }, "status": "complete", "success": true, "type": "export" }, "success": true } ``` ## Import SQL into your D1 Database `client.d1.database.import(stringdatabaseId, DatabaseImportParamsparams, RequestOptionsoptions?): DatabaseImportResponse` **post** `/accounts/{account_id}/d1/database/{database_id}/import` Generates a temporary URL for uploading an SQL file to, then instructing the D1 to import it and polling it for status updates. Imports block the D1 for their duration. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `DatabaseImportParams = Init | Ingest | Poll` - `DatabaseImportParamsBase` - `Init extends DatabaseImportParamsBase` - `Ingest extends DatabaseImportParamsBase` - `Poll extends DatabaseImportParamsBase` ### Returns - `DatabaseImportResponse` - `at_bookmark?: string` The current time-travel bookmark for your D1, used to poll for updates. Will not change for the duration of the import. Only returned if an import process is currently running or recently finished. - `error?: string` Only present when status = 'error'. Contains the error message that prevented the import from succeeding. - `filename?: string` Derived from the database ID and etag, to use in avoiding repeated uploads. Only returned when for the 'init' action. - `messages?: Array` Logs since the last time you polled - `result?: Result` Only present when status = 'complete' - `final_bookmark?: string` The time-travel bookmark if you need restore your D1 to directly after the import succeeded. - `meta?: Meta` - `changed_db?: boolean` Denotes if the database has been altered in some way, like deleting rows. - `changes?: number` Rough indication of how many rows were modified by the query, as provided by SQLite's `sqlite3_total_changes()`. - `duration?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `last_row_id?: number` The row ID of the last inserted row in a table with an `INTEGER PRIMARY KEY` as provided by SQLite. Tables created with `WITHOUT ROWID` do not populate this. - `rows_read?: number` Number of rows read during the SQL query execution, including indices (not all rows are necessarily returned). - `rows_written?: number` Number of rows written during the SQL query execution, including indices. - `served_by_colo?: string` The three letters airport code of the colo that handled the query. - `served_by_primary?: boolean` Denotes if the query has been handled by the database primary instance. - `served_by_region?: "WNAM" | "ENAM" | "WEUR" | 3 more` Region location hint of the database instance that handled the query. - `"WNAM"` - `"ENAM"` - `"WEUR"` - `"EEUR"` - `"APAC"` - `"OC"` - `size_after?: number` Size of the database after the query committed, in bytes. - `timings?: Timings` Various durations for the query. - `sql_duration_ms?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `num_queries?: number` The total number of queries that were executed during the import. - `status?: "complete" | "error"` - `"complete"` - `"error"` - `success?: boolean` - `type?: "import"` - `"import"` - `upload_url?: string` The R2 presigned URL to use for uploading. Only returned when for the 'init' action. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const response = await client.d1.database.import('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353', action: 'init', etag: 'etag', }); console.log(response.at_bookmark); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "at_bookmark": "at_bookmark", "error": "error", "filename": "filename", "messages": [ "string" ], "result": { "final_bookmark": "final_bookmark", "meta": { "changed_db": true, "changes": 0, "duration": 0, "last_row_id": 0, "rows_read": 0, "rows_written": 0, "served_by_colo": "LHR", "served_by_primary": true, "served_by_region": "EEUR", "size_after": 0, "timings": { "sql_duration_ms": 0 } }, "num_queries": 0 }, "status": "complete", "success": true, "type": "import", "upload_url": "upload_url" }, "success": true } ``` ## Domain Types ### Query Result - `QueryResult` - `meta?: Meta` - `changed_db?: boolean` Denotes if the database has been altered in some way, like deleting rows. - `changes?: number` Rough indication of how many rows were modified by the query, as provided by SQLite's `sqlite3_total_changes()`. - `duration?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `last_row_id?: number` The row ID of the last inserted row in a table with an `INTEGER PRIMARY KEY` as provided by SQLite. Tables created with `WITHOUT ROWID` do not populate this. - `rows_read?: number` Number of rows read during the SQL query execution, including indices (not all rows are necessarily returned). - `rows_written?: number` Number of rows written during the SQL query execution, including indices. - `served_by_colo?: string` The three letters airport code of the colo that handled the query. - `served_by_primary?: boolean` Denotes if the query has been handled by the database primary instance. - `served_by_region?: "WNAM" | "ENAM" | "WEUR" | 3 more` Region location hint of the database instance that handled the query. - `"WNAM"` - `"ENAM"` - `"WEUR"` - `"EEUR"` - `"APAC"` - `"OC"` - `size_after?: number` Size of the database after the query committed, in bytes. - `timings?: Timings` Various durations for the query. - `sql_duration_ms?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `results?: Array` - `success?: boolean` ### Database List Response - `DatabaseListResponse` - `created_at?: string` Specifies the timestamp the resource was created as an ISO8601 string. - `jurisdiction?: "eu" | "fedramp" | null` Specify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. - `"eu"` - `"fedramp"` - `name?: string` D1 database name. - `uuid?: string` D1 database identifier (UUID). - `version?: string` ### Database Delete Response - `DatabaseDeleteResponse = unknown` ### Database Raw Response - `DatabaseRawResponse` - `meta?: Meta` - `changed_db?: boolean` Denotes if the database has been altered in some way, like deleting rows. - `changes?: number` Rough indication of how many rows were modified by the query, as provided by SQLite's `sqlite3_total_changes()`. - `duration?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `last_row_id?: number` The row ID of the last inserted row in a table with an `INTEGER PRIMARY KEY` as provided by SQLite. Tables created with `WITHOUT ROWID` do not populate this. - `rows_read?: number` Number of rows read during the SQL query execution, including indices (not all rows are necessarily returned). - `rows_written?: number` Number of rows written during the SQL query execution, including indices. - `served_by_colo?: string` The three letters airport code of the colo that handled the query. - `served_by_primary?: boolean` Denotes if the query has been handled by the database primary instance. - `served_by_region?: "WNAM" | "ENAM" | "WEUR" | 3 more` Region location hint of the database instance that handled the query. - `"WNAM"` - `"ENAM"` - `"WEUR"` - `"EEUR"` - `"APAC"` - `"OC"` - `size_after?: number` Size of the database after the query committed, in bytes. - `timings?: Timings` Various durations for the query. - `sql_duration_ms?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `results?: Results` - `columns?: Array` - `rows?: Array>` - `number` - `string` - `unknown` - `success?: boolean` ### Database Export Response - `DatabaseExportResponse` - `at_bookmark?: string` The current time-travel bookmark for your D1, used to poll for updates. Will not change for the duration of the export task. - `error?: string` Only present when status = 'error'. Contains the error message. - `messages?: Array` Logs since the last time you polled - `result?: Result` Only present when status = 'complete' - `filename?: string` The generated SQL filename. - `signed_url?: string` The URL to download the exported SQL. Available for one hour. - `status?: "complete" | "error"` - `"complete"` - `"error"` - `success?: boolean` - `type?: "export"` - `"export"` ### Database Import Response - `DatabaseImportResponse` - `at_bookmark?: string` The current time-travel bookmark for your D1, used to poll for updates. Will not change for the duration of the import. Only returned if an import process is currently running or recently finished. - `error?: string` Only present when status = 'error'. Contains the error message that prevented the import from succeeding. - `filename?: string` Derived from the database ID and etag, to use in avoiding repeated uploads. Only returned when for the 'init' action. - `messages?: Array` Logs since the last time you polled - `result?: Result` Only present when status = 'complete' - `final_bookmark?: string` The time-travel bookmark if you need restore your D1 to directly after the import succeeded. - `meta?: Meta` - `changed_db?: boolean` Denotes if the database has been altered in some way, like deleting rows. - `changes?: number` Rough indication of how many rows were modified by the query, as provided by SQLite's `sqlite3_total_changes()`. - `duration?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `last_row_id?: number` The row ID of the last inserted row in a table with an `INTEGER PRIMARY KEY` as provided by SQLite. Tables created with `WITHOUT ROWID` do not populate this. - `rows_read?: number` Number of rows read during the SQL query execution, including indices (not all rows are necessarily returned). - `rows_written?: number` Number of rows written during the SQL query execution, including indices. - `served_by_colo?: string` The three letters airport code of the colo that handled the query. - `served_by_primary?: boolean` Denotes if the query has been handled by the database primary instance. - `served_by_region?: "WNAM" | "ENAM" | "WEUR" | 3 more` Region location hint of the database instance that handled the query. - `"WNAM"` - `"ENAM"` - `"WEUR"` - `"EEUR"` - `"APAC"` - `"OC"` - `size_after?: number` Size of the database after the query committed, in bytes. - `timings?: Timings` Various durations for the query. - `sql_duration_ms?: number` The duration of the SQL query execution inside the database. Does not include any network communication. - `num_queries?: number` The total number of queries that were executed during the import. - `status?: "complete" | "error"` - `"complete"` - `"error"` - `success?: boolean` - `type?: "import"` - `"import"` - `upload_url?: string` The R2 presigned URL to use for uploading. Only returned when for the 'init' action. # Time Travel ## Get D1 database bookmark `client.d1.database.timeTravel.getBookmark(stringdatabaseId, TimeTravelGetBookmarkParamsparams, RequestOptionsoptions?): TimeTravelGetBookmarkResponse` **get** `/accounts/{account_id}/d1/database/{database_id}/time_travel/bookmark` Retrieves the current bookmark, or the nearest bookmark at or before a provided timestamp. Bookmarks can be used with the restore endpoint to revert the database to a previous point in time. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `params: TimeTravelGetBookmarkParams` - `account_id: string` Path param: Account identifier tag. - `timestamp?: string` Query param: An optional ISO 8601 timestamp. If provided, returns the nearest available bookmark at or before this timestamp. If omitted, returns the current bookmark. ### Returns - `TimeTravelGetBookmarkResponse` - `bookmark?: string` A bookmark representing a specific state of the database at a specific point in time. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const response = await client.d1.database.timeTravel.getBookmark( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }, ); console.log(response.bookmark); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "bookmark": "00000001-00000002-00004e2f-0a83ea2fceebc654de0640c422be4653" }, "success": true } ``` ## Restore D1 Database to a bookmark or point in time `client.d1.database.timeTravel.restore(stringdatabaseId, TimeTravelRestoreParamsparams, RequestOptionsoptions?): TimeTravelRestoreResponse` **post** `/accounts/{account_id}/d1/database/{database_id}/time_travel/restore` Restores a D1 database to a previous point in time either via a bookmark or a timestamp. ### Parameters - `databaseId: string` D1 database identifier (UUID). - `params: TimeTravelRestoreParams` - `account_id: string` Path param: Account identifier tag. - `bookmark?: string` Query param: A bookmark to restore the database to. Required if `timestamp` is not provided. - `timestamp?: string` Query param: An ISO 8601 timestamp to restore the database to. Required if `bookmark` is not provided. ### Returns - `TimeTravelRestoreResponse` Response from a time travel restore operation. - `bookmark?: string` The new bookmark representing the state of the database after the restore operation. - `message?: string` A message describing the result of the restore operation. - `previous_bookmark?: string` The bookmark representing the state of the database before the restore operation. Can be used to undo the restore if needed. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const response = await client.d1.database.timeTravel.restore( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }, ); console.log(response.bookmark); ``` #### Response ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": { "bookmark": "00000001-00000002-00004e2f-0a83ea2fceebc654de0640c422be4653", "message": "Database restored successfully", "previous_bookmark": "00000001-00000002-00004e2f-0a83ea2fceebc654de0640c422be4653" }, "success": true } ``` ## Domain Types ### Time Travel Get Bookmark Response - `TimeTravelGetBookmarkResponse` - `bookmark?: string` A bookmark representing a specific state of the database at a specific point in time. ### Time Travel Restore Response - `TimeTravelRestoreResponse` Response from a time travel restore operation. - `bookmark?: string` The new bookmark representing the state of the database after the restore operation. - `message?: string` A message describing the result of the restore operation. - `previous_bookmark?: string` The bookmark representing the state of the database before the restore operation. Can be used to undo the restore if needed.