Skip to main content
The D1 component lets you add a Cloudflare D1 database to your app.

Constructor

sst.config.ts
const database = new sst.cloudflare.D1("MyDatabase");

Properties

id

nodes

SDK

Access the D1 database in your worker:
src/worker.ts
import { Resource } from "sst";

export default {
  async fetch(request, env) {
    const result = await env.MyDatabase
      .prepare("SELECT * FROM users WHERE id = ?")
      .bind(1)
      .first();
    
    return Response.json(result);
  }
};

Examples

Create a D1 database

sst.config.ts
const database = new sst.cloudflare.D1("MyDatabase");
sst.config.ts
const database = new sst.cloudflare.D1("MyDatabase");

new sst.cloudflare.Worker("MyWorker", {
  handler: "src/worker.ts",
  link: [database]
});

Build docs developers (and LLMs) love