Get started
This guide will instruct you through:
- Creating your first database using D1, Cloudflare’s native serverless SQL database.
- Creating a schema and querying your database via the command-line.
- Connecting a Cloudflare Worker to your D1 database to query your D1 database programmatically.
To continue:
- Sign up for a Cloudflare account ↗ if you have not already.
- Install
npm↗. - Install
Node.js↗. Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler requires a Node version of16.17.0or later.
You will create a new Worker as the means to query your database.
Create a new project named d1-tutorial by running:
npm create cloudflare@latest -- d1-tutorialyarn create cloudflare@latest d1-tutorialpnpm create cloudflare@latest d1-tutorialFor setup, select the following options:
- For What would you like to start with?, choose
Hello World example. - For Which template would you like to use?, choose
Hello World Worker. - For Which language do you want to use?, choose
TypeScript. - For Do you want to use git for version control?, choose
Yes. - For Do you want to deploy your application?, choose
No(we will be making some changes before deploying).
This creates a new d1-tutorial directory. Your new d1-tutorial directory contains:
- A
"Hello World"Worker atsrc/index.ts. - A
wrangler.tomlconfiguration file.wrangler.tomlis how yourd1-tutorialWorker will access your D1 database.
A D1 database is conceptually similar to many other databases: a database may contain one or more tables, the ability to query those tables, and optional indexes. D1 uses the familiar SQL query language ↗ (as used by SQLite).
To create your first D1 database:
- Change into the directory you just created for your Workers project:
cd d1-tutorial- Run the following
wrangler d1command and give your database a name. In this tutorial, the database will be namedprod-d1-tutorial:
npx wrangler d1 create prod-d1-tutorial✅ Successfully created DB 'prod-d1-tutorial'
[[d1_databases]]binding = "DB" # available in your Worker on env.DBdatabase_name = "prod-d1-tutorial"database_id = "<unique-ID-for-your-database>"This creates a new D1 database, and outputs the binding configuration needed in the next step.
You must create a binding for your Worker to connect to your D1 database. Bindings allow your Workers to access resources, like D1, on the Cloudflare developer platform. You create bindings by updating your wrangler.toml file.
To bind your D1 database to your Worker:
- Copy the lines obtained from step 2 from your terminal.
- Add them to the end of your
wrangler.tomlfile.
[[d1_databases]]binding = "DB" # available in your Worker on env.DBdatabase_name = "prod-d1-tutorial"database_id = "<unique-ID-for-your-database>"Specifically:
- The value (string) you set for
bindingis the binding name, and will be used to reference this database in your Worker. In this tutorial, name your bindingDB. - The binding name must be a valid JavaScript variable name ↗. For example,
binding = "MY_DB"orbinding = "productionDB"would both be valid names for the binding. - Your binding is available in your Worker at
env.<BINDING_NAME>and the D1 client API is exposed on this binding.
You can also bind your D1 database to a Pages Function. For more information, refer to Functions Bindings for D1.
With wrangler.toml configured properly, set up your database. Use the example schema.sql file below to initialize your database.
- Copy the following code and save it as a
schema.sqlfile in thed1-tutorialWorker directory you created in step 1:
DROP TABLE IF EXISTS Customers;CREATE TABLE IF NOT EXISTS Customers (CustomerId INTEGER PRIMARY KEY, CompanyName TEXT, ContactName TEXT);INSERT INTO Customers (CustomerID, CompanyName, ContactName) VALUES (1, 'Alfreds Futterkiste', 'Maria Anders'), (4, 'Around the Horn', 'Thomas Hardy'), (11, 'Bs Beverages', 'Victoria Ashworth'), (13, 'Bs Beverages', 'Random Name');- Initialize your database to run and test locally first. Bootstrap your new D1 database by running:
npx wrangler d1 execute prod-d1-tutorial --local --file=./schema.sql- Validate your data is in your database by running:
npx wrangler d1 execute prod-d1-tutorial --local --command="SELECT * FROM Customers"🌀 Mapping SQL input into an array of statements🌀 Executing on local database production-db-backend (5f092302-3fbd-4247-a873-bf1afc5150b) from .wrangler/state/v3/d1:┌────────────┬─────────────────────┬───────────────────┐│ CustomerId │ CompanyName │ ContactName │├────────────┼─────────────────────┼───────────────────┤│ 1 │ Alfreds Futterkiste │ Maria Anders │├────────────┼─────────────────────┼───────────────────┤│ 4 │ Around the Horn │ Thomas Hardy │├────────────┼─────────────────────┼───────────────────┤│ 11 │ Bs Beverages │ Victoria Ashworth │├────────────┼─────────────────────┼───────────────────┤│ 13 │ Bs Beverages │ Random Name │└────────────┴─────────────────────┴───────────────────┘After you have set up your database, run an SQL query from within your Worker.
- Navigate to your
d1-tutorialWorker and open theindex.tsfile. Theindex.tsfile is where you configure your Worker’s interactions with D1. - Clear the content of
index.ts. - Paste the following code snippet into your
index.tsfile:
export interface Env { // If you set another name in wrangler.toml as the value for 'binding', // replace "DB" with the variable name you defined. DB: D1Database;}
export default { async fetch(request, env): Promise<Response> { const { pathname } = new URL(request.url);
if (pathname === "/api/beverages") { // If you did not use `DB` as your binding name, change it here const { results } = await env.DB.prepare( "SELECT * FROM Customers WHERE CompanyName = ?", ) .bind("Bs Beverages") .all(); return Response.json(results); }
return new Response( "Call /api/beverages to see everyone who works at Bs Beverages", ); },} satisfies ExportedHandler<Env>;In the code above, you:
- Define a binding to your D1 database in your TypeScript code. This binding matches the
bindingvalue you set inwrangler.tomlunder[[d1_databases]]. - Query your database using
env.DB.prepareto issue a prepared query with a placeholder (the?in the query). - Call
bind()to safely and securely bind a value to that placeholder. In a real application, you would allow a user to define theCompanyNamethey want to list results for. Usingbind()prevents users from executing arbitrary SQL (known as “SQL injection”) against your application and deleting or otherwise modifying your database. - Execute the query by calling
all()to return all rows (or none, if the query returns none). - Return your query results, if any, in JSON format with
Response.json(results).
After configuring your Worker, you can test your project locally before you deploy globally.
While in your project directory, test your database locally.
- Run
wrangler dev:
npx wrangler devWhen you run wrangler dev, Wrangler provides a URL (most likely localhost:8787) to review your Worker.
- Navigate to the URL.
The page displays Call /api/beverages to see everyone who works at Bs Beverages.
- Test your database is running successfully. Add
/api/beveragesto the provided Wrangler URL. For example,localhost:8787/api/beverages.
If successful, the browser displays your data.
To deploy your Worker to production, you must first repeat the database bootstrapping steps after replacing the --local flag with the --remote flag to give your Worker data to read. This creates the database tables and imports the data into the production version of your database, running on Cloudflare’s global network.
- Bootstrap your database with the
schema.sqlfile you created in step 4:
npx wrangler d1 execute prod-d1-tutorial --remote --file=./schema.sql- Validate the data is in production by running:
npx wrangler d1 execute prod-d1-tutorial --remote --command="SELECT * FROM Customers"- Deploy your Worker to make your project accessible on the Internet. Run:
npx wrangler deployOutputs: https://d1-tutorial.<YOUR_SUBDOMAIN>.workers.devYou can now visit the URL for your newly created project to query your live database.
For example, if the URL of your new Worker is d1-tutorial.<YOUR_SUBDOMAIN>.workers.dev, accessing https://d1-tutorial.<YOUR_SUBDOMAIN>.workers.dev/api/beverages sends a request to your Worker that queries your live database directly.
To delete your database, run:
npx wrangler d1 delete prod-d1-tutorialIf you want to delete your Worker, run:
npx wrangler delete d1-tutorialIn this tutorial, you have:
- Created a D1 database
- Created a Worker to access that database
- Deployed your project globally
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the Cloudflare Developers community on Discord ↗.
- See supported Wrangler commands for D1.
- Learn how to use the D1 client API within your Worker.
- Explore community projects built on D1.