## Import SQL into your D1 Database `client.D1.Database.Import(ctx, databaseID, params) (*DatabaseImportResponse, error)` **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). - `params DatabaseImportParams` - `AccountID param.Field[string]` Path param: Account identifier tag. - `Action param.Field[DatabaseImportParamsInitAction]` Body param: Indicates you have a new SQL file to upload. - `const DatabaseImportParamsInitActionInit DatabaseImportParamsInitAction = "init"` - `Etag param.Field[string]` Body param: Required when action is 'init' or 'ingest'. An md5 hash of the file you're uploading. Used to check if it already exists, and validate its contents before ingesting. ### Returns - `type DatabaseImportResponse struct{…}` - `AtBookmark 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 []string` Logs since the last time you polled - `Result DatabaseImportResponseResult` Only present when status = 'complete' - `FinalBookmark string` The time-travel bookmark if you need restore your D1 to directly after the import succeeded. - `Meta DatabaseImportResponseResultMeta` - `ChangedDB bool` Denotes if the database has been altered in some way, like deleting rows. - `Changes float64` Rough indication of how many rows were modified by the query, as provided by SQLite's `sqlite3_total_changes()`. - `Duration float64` The duration of the SQL query execution inside the database. Does not include any network communication. - `LastRowID float64` 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. - `RowsRead float64` Number of rows read during the SQL query execution, including indices (not all rows are necessarily returned). - `RowsWritten float64` Number of rows written during the SQL query execution, including indices. - `ServedByColo string` The three letters airport code of the colo that handled the query. - `ServedByPrimary bool` Denotes if the query has been handled by the database primary instance. - `ServedByRegion DatabaseImportResponseResultMetaServedByRegion` Region location hint of the database instance that handled the query. - `const DatabaseImportResponseResultMetaServedByRegionWnam DatabaseImportResponseResultMetaServedByRegion = "WNAM"` - `const DatabaseImportResponseResultMetaServedByRegionEnam DatabaseImportResponseResultMetaServedByRegion = "ENAM"` - `const DatabaseImportResponseResultMetaServedByRegionWeur DatabaseImportResponseResultMetaServedByRegion = "WEUR"` - `const DatabaseImportResponseResultMetaServedByRegionEeur DatabaseImportResponseResultMetaServedByRegion = "EEUR"` - `const DatabaseImportResponseResultMetaServedByRegionApac DatabaseImportResponseResultMetaServedByRegion = "APAC"` - `const DatabaseImportResponseResultMetaServedByRegionOc DatabaseImportResponseResultMetaServedByRegion = "OC"` - `SizeAfter float64` Size of the database after the query committed, in bytes. - `Timings DatabaseImportResponseResultMetaTimings` Various durations for the query. - `SqlDurationMs float64` The duration of the SQL query execution inside the database. Does not include any network communication. - `NumQueries float64` The total number of queries that were executed during the import. - `Status DatabaseImportResponseStatus` - `const DatabaseImportResponseStatusComplete DatabaseImportResponseStatus = "complete"` - `const DatabaseImportResponseStatusError DatabaseImportResponseStatus = "error"` - `Success bool` - `Type DatabaseImportResponseType` - `const DatabaseImportResponseTypeImport DatabaseImportResponseType = "import"` - `UploadURL string` The R2 presigned URL to use for uploading. Only returned when for the 'init' action. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/d1" "github.com/cloudflare/cloudflare-go/option" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) response, err := client.D1.Database.Import( context.TODO(), "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", d1.DatabaseImportParams{ AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"), Body: d1.DatabaseImportParamsBodyInit{ Action: cloudflare.F(d1.DatabaseImportParamsBodyInitActionInit), Etag: cloudflare.F("etag"), }, }, ) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", response.AtBookmark) } ``` #### 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 } ```