Bulk import to D1 using REST API
In this tutorial, you will learn how to import a database into D1 using the REST API.
- Sign up for a Cloudflare account ↗.
- Install
Node.js
↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0
or later.
To use REST APIs, you need to generate an API token to authenticate your API requests. You can do this through the Cloudflare dashboard.
- Log in to the Cloudflare dashboard ↗ and select your account.
- Select your user icon, then select My Profile.
- Go to API Tokens.
- Under API Tokens, select Create Token.
- Scroll to Custom token > Create custom token, then select Get started.
- Under Token name, enter a descriptive token name. For example,
Name-D1-Import-API-Token
. - Under Permissions:
- Select Account.
- Select D1.
- Select Edit.
- Select Continue to summary.
- Select Create token.
- Copy the API token and save it in a secure file.
- Refer to Create API token for more information on creating API tokens through the Cloudflare dashboard.
- Refer to Create tokens via API for more information on creating API tokens through API.
You must have an existing D1 table which matches the schema of the data you wish to import.
This tutorial uses the following:
- A database called
d1-import-tutorial
. - A table called
TargetD1Table
- Within
TargetD1Table
, three columns calledid
,text
, anddate_added
.
To create the table, follow these steps:
-
Go to Storage & Databases > D1.
-
Select Create.
-
Name your database. For this tutorial, name your D1 database
d1-import-tutorial
. -
(Optional) Provide a location hint. Location hint is an optional parameter you can provide to indicate your desired geographical location for your database. Refer to Provide a location hint for more information.
-
Select Create.
-
Go to Console, then paste the following SQL snippet. This creates a table named
TargetD1Table
.Alternatively, you can use the Wrangler CLI.
-
Create a new directory and initialize a new Node.js project.
-
In this repository, create a new file called
index.js
. This file will contain the code which uses REST API to import your data to your D1 database. -
In your
index.js
file, define the following variables:TARGET_TABLE
: The target table nameACCOUNT_ID
: The account ID (you can find this in the Cloudflare dashboard > Workers & Pages)DATABASE_ID
: The D1 database ID (you can find this in the Cloudflare dashboard > Storage & Databases > D1 SQL Database > your database)D1_API_KEY
: The D1 API token generated in step 1
In practice, you may already have the data you wish to import to a D1 database.
This tutorial generates example data to demonstrate the import process.
-
Install the
@faker-js/faker
module. -
Add the following code at the beginning of the
index.js
file. This code creates an array calleddata
with 2500 (uploadSize
) array elements, where each array element contains an object withid
,text
, anddate_added
. Each array element corresponds to a table row.
-
Create a function that will generate the SQL command to insert the data into the target table. This function uses the
data
array generated in the previous step.
The import process consists of four steps:
- Init upload: This step initializes the upload process. It sends the hash of the SQL command to the D1 API and receives an upload URL.
- Upload to R2: This step uploads the SQL command to the upload URL.
- Start ingestion: This step starts the ingestion process.
- Polling: This step polls the import process until it completes.
-
Create a function called
uploadToD1
which executes the four steps of the import process.In the above code:
- An
md5
hash of the SQL command is generated. initResponse
initializes the upload process and receives the upload URL.r2Response
uploads the SQL command to the upload URL.- Before starting ingestion, the ETag is verified.
ingestResponse
starts the ingestion process.pollImport
polls the import process until it completes.
- An
-
Add the
pollImport
function to theindex.js
file.The code above does the following:
- Sends a
poll
action to the D1 API. - Polls the import process until it completes.
- Sends a
-
Finally, add the
runImport
function to theindex.js
file to run the import process.
In the previous steps, you have created functions to execute various processes involved in importing data into D1. The final code executes those functions to import the example data into the target D1 table.
-
Copy the final code of your
index.js
file as shown below, with your variables defined at the top of the code.
-
Run your code.
You will now see your target D1 table populated with the example data.
By completing this tutorial, you have
- Created an API token.
- Created a target database and table.
- Generated example data.
- Created SQL command for the example data.
- Imported your example data into the D1 target table using REST API.