# Cloudflare D1 Integrations

Connect queues, forms and scheduled workflows to a Cloudflare D1 database. Run SQL with bound values, route work using lookup tables and archive database exports.

## Setup notes

- **Connect a Cloudflare account** Enter your account ID and a D1 API token. Cloudflare grants D1 permissions at account level. Use a separate account when you need credentials isolated from production databases.
- **Choose the database for each action** Select a database and bind incoming values to SQL placeholders. A unique event ID lets a queue consumer receive the same business event again safely.
- **Query analytics and recover data** Analytics actions require the relevant Analytics Read permission. Time Travel availability depends on the recovery window for your database. Test restoration on a dedicated database.

## Workflow templates

- [Index incoming email in Cloudflare D1](https://www.fasthook.io/templates/mailhook-delivery-index-to-d1.md): Store the ID, subject, and date of each email received by the private Mailhook.
- [Record origin certificate details in D1](https://www.fasthook.io/templates/origin-ca-certificate-inventory-to-d1.md): Look up a certificate by serial number and refresh its hostname and expiry record in D1.
- [Log RevenueCat subscription cancellations and expirations in D1](https://www.fasthook.io/templates/revenuecat-subscription-cancellations-to-d1.md): Record subscription cancellation and expiration events in a Cloudflare D1 table.
- [Log mail DNS changes in D1 and notify Slack](https://www.fasthook.io/templates/cloudflare-dns-mail-changes-to-d1-and-slack.md): Watch a domain’s MX, SPF and DMARC records.
- [Route Tally submissions to Trello by product](https://www.fasthook.io/templates/tally-d1-routing-to-trello.md): Look up a product in D1 and create a card in its assigned Trello list.
- [Record order events from a queue once](https://www.fasthook.io/templates/cloudflare-queues-order-events-to-d1.md): Keep one D1 journal entry per order event, even when the producer sends the event twice.
- [Send overdue paid orders to Slack](https://www.fasthook.io/templates/cloudflare-d1-overdue-orders-to-slack.md): Post the IDs of paid orders still awaiting shipment after two business days.
- [Archive a daily D1 SQL export in R2](https://www.fasthook.io/templates/cloudflare-d1-backups-to-r2.md): Export database schema and data as SQL and save a dated copy in the selected R2 bucket.

## Actions

- **Analyze database activity** — Compare database queries, rows read and rows written over the selected period.
- **Analyze SQL query performance** — Group query measurements by time, database and the dimensions you select.
- **Change query insights settings** — Update the account settings that control how D1 collects query insights.
- **Change selected database settings** — Apply just the settings supplied in this request to the selected database.
- **Compare database performance** — Rank databases by the selected performance measure over a chosen time range.
- **Create a database** — Name a new database and choose its location and read replication settings.
- **Delete a database** — Remove the selected database from the connected Cloudflare account.
- **Explore database analytics columns** — Look up dataset columns and types before writing a query against the analytics service.
- **Explore database request logs** — Query recorded database requests for the selected period using SQL.
- **Explore SQL query logs** — Query D1 statement activity over a selected time range.
- **Export a database as a SQL file** — Wait for the database export and return its SQL file for a storage step. Use the bookmark action for larger exports.
- **Find a database recovery point** — Get the bookmark for the database at a given time so you can review the point before restoring.
- **Find costly SQL queries** — Review recorded queries for the selected database and time range, ordered by the measure you choose.
- **Find database tuning recommendations** — Read suggestions for improving queries on the selected database and follow the cursor for additional results.
- **Find databases in an account** — Browse a page of databases or filter the list by name.
- **Read database details** — Look up the name, size and configuration of a selected database.
- **Read query insights settings** — Check how query insights are configured for the connected account.
- **Recover a deleted database** — Restore a recently deleted database by its ID. Choose a new name if the original name is taken.
- **Replace database settings** — Supply the full configuration for the selected database.
- **Restore a database to a recovery point** — Rewind the selected database to a bookmark or timestamp within its recovery window.
- **Run SQL and return named columns** — Run a statement with bound values, or send a batch. Each row in the results uses the column names from your query.
- **Run SQL and return rows as arrays** — Run one statement or a batch and retrieve each row as an ordered array of column values.
- **Start or check a SQL export** — Export a database or selected tables. Use the returned bookmark to check progress and obtain the download link.
- **Start or check a SQL import** — Request an upload URL, submit the uploaded SQL file for import, or check an import using its bookmark.
- **Track database storage** — See how storage usage for your databases changes over time.
