Skip to content
Start here

Import SQL into your D1 Database

client.d1.database.import(stringdatabaseId, DatabaseImportParamsparams, RequestOptionsoptions?): DatabaseImportResponse { at_bookmark, error, filename, 6 more }
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.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
ParametersExpand Collapse
databaseId: string

D1 database identifier (UUID).

DatabaseImportParams = Init | Ingest | Poll
DatabaseImportParamsBase
Init extends DatabaseImportParamsBase
Ingest extends DatabaseImportParamsBase
Poll extends DatabaseImportParamsBase
ReturnsExpand Collapse
DatabaseImportResponse { at_bookmark, error, filename, 6 more }
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<string>

Logs since the last time you polled

result?: Result { final_bookmark, meta, num_queries }

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, changes, duration, 8 more }
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.

One of the following:
"WNAM"
"ENAM"
"WEUR"
"EEUR"
"APAC"
"OC"
size_after?: number

Size of the database after the query committed, in bytes.

timings?: Timings { sql_duration_ms }

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"
One of the following:
"complete"
"error"
success?: boolean
type?: "import"
upload_url?: string

The R2 presigned URL to use for uploading. Only returned when for the 'init' action.

Import SQL into your D1 Database

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);
{
  "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
}
Returns Examples
{
  "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
}