- Home
- Skills
- APIs & Backend
- Cloudflare Workers: Edge APIs with D1, KV, R2 & AI
Works with the AI Agents you already use
Cloudflare Workers: Edge APIs with D1, KV, R2 & AI
Architect, build, and deploy serverless apps on Cloudflare Workers with D1, R2, KV, and Hono.
Free
Cloudflare Workers: Edge APIs with D1, KV, R2 & AI
Example session with this skill installed
Create a Cloudflare Worker using Hono that stores user profiles in a D1 database and caches frequently accessed metadata in KV. Include the wrangler config and a migration script.
- Read your context and instructions
- Compiled the cloudflare workers
I have scaffolded the Cloudflare Worker using Hono with D1 and KV bindings. This setup includes the API logic, the database schema migration, and the configuration file required for deployment.
Cloudflare Worker Implementation
// src/index.ts
import { Hono } from 'hono';
type Bindings = {
DB: D1Database;
USER_CACHE: KVNamespace;
};
const app = new Hono<{ Bindings: Bindings }>();
app.get('/user/:id', async (c) => {
const id = c.req.param('id');
// Try KV cache first
const cached = await c.env.USER_CACHE.get(`user:${id}`);
if (cached) return c.json(JSON.parse(cached));
// Fallback to D1
const user = await c.env.DB.prepare('SELECT * FROM users WHERE id = ?')
.bind(id)
.first();
if (!user) return c.json({ error: 'Not found' }, 404);
// Update cache for 1 hour
await c.env.USER_CACHE.put(`user:${id}`, JSON.stringify(user), { expirationTtl: 3600 });
return c.json(user);
});
export default app;
// wrangler.jsonc
{
"name": "user-api",
"main": "src/index.ts",
"compatibility_date": "2024-05-01",
"d1_databases": [
{
"binding": "DB",
"database_name": "production-db",
"database_id": "YOUR_D1_UUID"
}
],
"kv_namespaces": [
{
"binding": "USER_CACHE",
"id": "YOUR_KV_UUID"
}
]
}
-- migrations/0001_create_users.sql
CREATE TABLE users (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Next steps
- Run
npx wrangler d1 create production-dband update the ID inwrangler.jsonc. - Apply the migration locally using
npx wrangler d1 migrations apply production-db --local. - Use
npx wrangler devto start the local development server with Miniflare.
Connects securely to your tools. The creator never sees your data.
About this skill
Deploying to the edge often results in configuration debt, cold start issues, or broken database bindings. This skill provides a direct path for building and deploying serverless applications on Cloudflare's global network using the full suite of developer tools.
What it does
- Architectural mapping selects the right primitives like Hono for APIs, D1 for relational data, or R2 for object storage.
- Wrangler configuration generates valid
wrangler.jsoncfiles including environment bindings and compatibility flags. - Database management handles D1 SQLite migrations and parameterized queries to prevent SQL injection.
- State management implements Durable Objects for WebSockets or per-user state and KV for high-read caching.
- Background processing configures Queues with Dead Letter Queues and Cron Triggers for scheduled tasks.
- Inference integration scripts Workers AI and Vectorize for edge-based machine learning workflows.
How it works
- Define the stack by specifying your frontend framework or API needs to select between Workers and Pages.
- Configure bindings for D1, R2, KV, or Durable Objects within the Wrangler configuration.
- Develop and test using local Miniflare environments and Vitest pool workers for edge-consistent testing.
- Deploy and monitor using deployment commands, custom domain routing, and live log tailing.
Frameworks & tools
Uses Hono for routing, Wrangler CLI for orchestration, Vitest for testing, and Zod for input validation. Supports Next.js (OpenNext), Astro, Nuxt, and SvelteKit.
Why this beats prompting it yourself
Generic LLMs often mix up Cloudflare's legacy syntax with modern standards or fail to handle the nuances of env bindings versus process.env. This skill enforces current wrangler.jsonc structures, proper migration flows, and cost-aware execution limits that prevent CPU time-outs.
Use cases
- Build a globally distributed REST API using Hono and D1 SQLite.
- Deploy an S3-compatible image hosting service with R2 and no egress fees.
- Create real-time collaborative tools using Durable Objects and WebSockets.
- Run scheduled data processing jobs using Cron Triggers and Queues.
Known limitations
Requires manual login via Wrangler for initial authentication. Developers must manually manage secret injection for CI/CD environments beyond local development.
Requires manual Wrangler CLI login. Secrets must be manually added to production via CLI. Subject to Cloudflare CPU and subrequest limits.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free forever
No account required to browse
Trust & safety
Security scan
Security scan passed 1 day ago
- Free with an Agensi account
Requires