Skip to main content

Hands-on Workshop

Build a Bookmark API with Cloudflare Workers

Go from zero to a production API with storage, databases, and AI - in seven steps.

~1.5 hours Beginner – Intermediate Node.js v20+
Workshop Roadmap
  1. 01
    Getting Started 10 min
  2. 02
    Add Routes and CRUD Endpoints 20 min
  3. 03
    Persistent Storage with KV 15 min
  4. 04
    D1 Database with KV Caching 20 min
  5. 05
    AI-Powered Summaries 20 min
  6. 06
    AI Gateway 10 min
  7. 07
    Deploy to Production 5 min
Tools you'll use
WranglerKVD1Workers AIAI Gateway

Hands-on Labs

WORKSHOP STEPS
STEP 01

Getting Started

Scaffold the Bookmark API project, understand the Workers runtime, and test your first Worker locally.

Prerequisites

  • Node.js v20+
  • Cloudflare Account
  • Terminal/Command Line
  • Basic JavaScript knowledge

Learning Objectives

  • Scaffold a Workers project and understand the generated file structure
  • Explain how the fetch handler receives and responds to HTTP requests
  • Use Wrangler to run, test, and iterate on a Worker locally

Watch: Cloudflare Workers 101

Before diving in, watch this overview of Workers architecture and concepts.

Step 1: Authenticate with Cloudflare

What we're building
A verified connection between your local machine and your Cloudflare account.
Why this matters
Later steps create KV namespaces, D1 databases, and deploy your Worker. All of these require authentication. Setting this up first prevents confusing errors later.
# Log in to your Cloudflare account (opens a browser window)
npx wrangler login

# Verify you are authenticated
npx wrangler whoami

You should see your account name and ID. If not, run npx wrangler login again.

Don't Skip This

If you skip authentication, commands like npx wrangler kv namespace create, npx wrangler d1 create, and npx wrangler deploy will fail with auth errors in later steps.

Step 2: Create the Bookmark API Project

What we're building
A new Cloudflare Worker project called bookmark-api running locally.
Why this matters
Every Workers project starts with a scaffold. This gives you the project structure, TypeScript config, and local dev server.
# Create the project
npm create cloudflare@latest -- bookmark-api --type hello-world --ts

# Navigate into the project
cd bookmark-api

# Start the local dev server
npm run dev

Running npm create cloudflare@latest will trigger a series of interactive prompts (e.g. package manager, git initialization, deploy). Accept the defaults for each prompt to match the configuration used throughout this workshop.

What Was Created

Your project contains:

  • src/index.ts - The Worker entry point with a fetch handler
  • wrangler.jsonc - Wrangler CLI configuration (name, entry point, bindings)
  • package.json - Dependencies and scripts
  • tsconfig.json - TypeScript configuration
Troubleshooting
  • npm not found: Install Node.js v20+ from nodejs.org
  • Template not found: Update npm: npm install -g npm@latest
  • Port 8787 busy: Use npx wrangler dev --port 8788
  • Build errors: Delete node_modules and run npm install again

Step 3: Understand the Code

What we're building
An understanding of how the fetch handler processes HTTP requests.
Why this matters
The fetch handler is the entry point for every request your Worker receives. Understanding its parameters is fundamental.

Open src/index.ts:

export default {
  async fetch(request, env, ctx): Promise<Response> {
    return new Response('Hello World!');
  },
} satisfies ExportedHandler<Env>;

Every Cloudflare Worker exports a default object with handler functions. The fetch handler is called for every HTTP request.

ParameterDescription
requestThe incoming HTTP request (URL, headers, method, body)
envEnvironment variables and bindings (KV, D1, AI - you will add these in later steps)
ctxContext with waitUntil() for background work and passThroughOnException() for fallback
Other Handlers

Workers can also export scheduled (cron triggers), queue (message processing), and email (email routing) handlers. This workshop focuses on fetch.

Step 4: Test and Modify

What we're building
Confidence that the local dev server works and hot reloading updates your Worker instantly.
Why this matters
Fast local iteration is essential. You will be modifying this file many times throughout the workshop.

Test in the browser

  1. Open http://localhost:8787. You should see “Hello World!”
  2. Try http://localhost:8787/anything. Same response (no routing yet)

Make a change

Update src/index.ts to return JSON instead of plain text:

export default {
  async fetch(request, env, ctx): Promise<Response> {
    return Response.json({
      name: 'Bookmark API',
      version: '1.0.0',
      status: 'running'
    });
  },
} satisfies ExportedHandler<Env>;

Save the file. Wrangler picks up the change automatically. Refresh your browser to see JSON output.

Response.json()

Response.json() is a convenience method that serializes an object to JSON and sets Content-Type: application/json automatically. You will use it throughout this workshop.

Challenge Try It Yourself
  1. Add a timestamp field to the JSON response using new Date().toISOString()
  2. Add a region field that reads request.cf?.colo (the Cloudflare data center handling the request. This will show a value when deployed, but may be undefined locally)
  3. Return a 404 response for any path other than / (hint: use new URL(request.url).pathname)

These are not graded. Experiment freely, then move on to the next step.

Learning Resources

Documentation for every tool used in this workshop.

Workers Documentation

API references, configuration guides, and best practices for the Workers runtime

READ DOCS

Cloudflare KV

Global key-value storage for high-read, low-write data like caches and config

KV DOCS

D1 Database

Serverless SQL database for relational queries, filtering, and structured data

D1 DOCS

Workers AI

Run inference on machine learning models directly from your Worker

AI DOCS

AI Gateway

Caching, rate limiting, and analytics for AI inference requests

GATEWAY DOCS

Developer Channel

Video walkthroughs and tutorials for the Developer Platform

WATCH VIDEOS