# Cloudflare D1

Canonical URL: https://developers.iagenify.com/sdk/ref/providers/d1/
Language: en

`api.storage.connect({ type: "database", provider: "d1", endpoint })`

A relational database of your own, reached over its HTTP query API. Three actions: tables, overview, and the statement you wrote.

Status: type: database · SQL · one statement per call

## Connecting {#connect}

```javascript
const db = await api.storage.connect({
  label: "Analytics",
  type: "database",
  provider: "d1",
  endpoint: {
    credentials: {
      accountId: "…",
      databaseId: "…",
      apiToken: "…",
    },
  },
});
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `credentials.accountId` | string | required | The account the database belongs to. |
| `credentials.databaseId` | string | required | The database itself. |
| `credentials.apiToken` | string | required | A token with permission to query it. |

> **Two older spellings are still read**
>
> bucketName is accepted in place of databaseId, and secretAccessKey in place of apiToken — the shape is shared with the object providers. Prefer the names above; the others are read for compatibility only.

All three are required at query time, not at connection: a set that is missing one raises `Identifiants Cloudflare D1 incomplets` on the first `run()`.

## The three actions {#actions}

| Call | Gives back |
| --- | --- |
| `{ action: "tables" }` | Every table with its columns, types and primary keys. |
| `{ action: "overview" }` | Up to 30 tables, their columns, and one example row each. |
| `{ action: "execute", sql, params }` | The rows, plus what the statement changed. |
| `{ sql, params }` | The rows alone — the shorter form, with no `action`. |

```javascript
await db.run({ action: "tables" });
// [ { name: "orders", columns: [ { name: "id", type: "INTEGER", primaryKey: true }, … ] } ]

await db.run({
  action: "execute",
  sql: "SELECT id, total FROM orders WHERE status = ? ORDER BY id DESC LIMIT 20",
  params: ["paid"],
});
```

## Always use params {#params}

> **A value pasted into SQL is an injection, wherever it came from**
>
> params is bound by the database, so a quote in a value stays a quote. This matters most when the statement is written by a model: the value and the statement must travel separately.

## No row counts in tables or overview {#no-count}

Neither action counts rows, deliberately: this provider charges for every row a statement reads, so a `COUNT(*)` across a wide table to tell you « how big is this » can cost more than the query you were about to write. One example row per table is enough to write the right statement.

## Internal tables are hidden {#internal}

Names beginning `sqlite_` or `_cf_` are filtered out of `tables` and `overview`.

## What is bounded {#limits}

| Thing | Bound |
| --- | --- |
| Statements per call | one |
| `overview` | 30 tables, 25 columns, 1 row each |
| Example row | fetched with `LIMIT 1`, and a table that refuses it is returned without one |

## Writing {#writes}

A write is `execute` with the statement that performs it. There is no separate insert or update action — SQL already says what it does.

```javascript
await db.run({
  action: "execute",
  sql: "UPDATE orders SET status = ? WHERE id = ?",
  params: ["shipped", 4182],
});
```

- [connect()](/sdk/ref/storage/connect): The call itself, and where the secret goes.
- [execute](/sdk/ref/connected-storage/run/execute): The action in full, with its guards.
- [IaGenify Cloud](/sdk/ref/providers/iagenify-cloud): A SQL database with nothing to connect.

```python
db = api.storage.connect(
    label="Analytics",
    type="database",
    provider="d1",
    endpoint={
        "credentials": {
            "accountId": "…",
            "databaseId": "…",
            "apiToken": "…",
        },
    },
)
```

```python
db.run(action="tables")
# [{"name": "orders", "columns": [{"name": "id", "type": "INTEGER", "primaryKey": True}, …]}]

db.run(
    action="execute",
    sql="SELECT id, total FROM orders WHERE status = ? ORDER BY id DESC LIMIT 20",
    params=["paid"],
)
```

```python
db.run(
    action="execute",
    sql="UPDATE orders SET status = ? WHERE id = ?",
    params=["shipped", 4182],
)
```
