Welcome to the examples directory! This collection contains production-ready reference implementations demonstrating common patterns and integrations for Cloudflare Workers + React applications.
Each example in this directory is a self-contained implementation showcasing a specific feature or pattern. These are not just code snippets - they're complete, working implementations that you can copy, adapt, and integrate into your project.
Think of these examples as a cookbook for your Cloudflare Workers + React application. Each example demonstrates:
- Best practices for implementing a specific feature
- Complete code from frontend to backend
- Configuration needed (bindings, secrets, etc.)
- Database schemas and migrations (where applicable)
- Security considerations and error handling
Examples are designed to be integrated into your main project:
- Review the example - Read the README to understand what it does
- Copy relevant files - Move frontend and worker code to your project
- Update configuration - Add necessary bindings to your
wrangler.jsonc - Generate types - Run
npm run cf-typegenafter adding bindings - Test locally - Verify everything works with
npm run dev - Customize - Adapt the code to your specific needs
Every example includes:
- README.md - Detailed explanation, prerequisites, and setup steps
- Source code - Frontend components and Worker API handlers
- Configuration examples - Sample
wrangler.jsoncsnippets - Database files - Migrations and schemas (for D1 examples)
- PRP.md - Product Requirement Plan for implementation guidance
- Comments - Inline documentation explaining key concepts
Explore various approaches to integrating AI capabilities into your application. See AI Examples Overview for complete details.
Directory: ai/simple-claude-chat/
A straightforward chat interface using Anthropic's Claude API. Perfect for getting started with AI.
What you'll learn:
- Setting up Claude API with Workers
- Handling chat messages and context
- Managing API keys securely
- Error handling and rate limiting
Use this when: You need a basic conversational AI interface
Directory: ai/streaming-chat/
Real-time streaming responses using Server-Sent Events (SSE) for a better user experience.
What you'll learn:
- Implementing streaming responses
- Server-Sent Events (SSE) patterns
- Handling stream interruptions
- Progressive UI updates
Use this when: You want real-time, token-by-token responses
Directory: ai/workers-ai-chat/
Edge AI integration using Cloudflare's Workers AI for low-latency, cost-effective inference.
What you'll learn:
- Using Cloudflare Workers AI binding
- Edge AI deployment
- Model selection and configuration
- Cost optimization strategies
Use this when: You need low-latency AI at the edge
Directory: ai/with-ai-gateway/
Production-ready setup showing how to integrate Cloudflare's AI Gateway product for caching, analytics, and cost control.
What you'll learn:
- Configuring AI Gateway
- Caching strategies for AI responses
- Rate limiting and cost controls
- Analytics and monitoring
Use this when: You're deploying AI features to production
Master data persistence patterns with Cloudflare's serverless storage solutions:
Directory: database/d1-contact-form/
Complete contact form implementation with D1 database storage, including schema design and migrations.
What you'll learn:
- D1 database setup and configuration
- Creating and running migrations
- CRUD operations with prepared statements
- Form handling and validation
- SQL best practices for edge databases
Use this when: You need relational data storage with SQL
Directory: database/kv-sessions/
User session management using Cloudflare KV with automatic expiration.
What you'll learn:
- KV namespace setup
- Session creation and validation
- TTL (Time To Live) configuration
- Secure session handling
- Cookie management
Use this when: You need session storage or simple key-value caching
Before using these examples, ensure you have:
- Node.js 18+ installed
- npm or another package manager
- A Cloudflare account (free tier works)
- This boilerplate template set up and running
- Basic understanding of TypeScript, React, and Cloudflare Workers
AI Examples:
- Anthropic API key (for Claude API examples) - Get one here
- Workers AI enabled on your Cloudflare account (for Workers AI examples)
Database Examples:
- Wrangler CLI installed (
npm install -g wrangler) - Understanding of SQL (for D1 examples)
Follow these steps to integrate any example into your project:
# Navigate to the example you're interested in
cd examples/ai/simple-claude-chat/
# Read the README
cat README.mdUnderstand what the example does, what it requires, and how it works.
Each example is organized by file type:
Frontend Code (React components and hooks)
# Example structure:
examples/ai/simple-claude-chat/src/
components/
ChatInterface.tsx
hooks/
useChat.tsCopy these to your project's src/ directory:
cp -r examples/ai/simple-claude-chat/src/* src/Worker Code (API endpoints)
# Example structure:
examples/ai/simple-claude-chat/worker/
routes/
chat.tsCopy these to your project's worker/ directory:
cp -r examples/ai/simple-claude-chat/worker/* worker/Database Files (for D1 examples)
# Example structure:
examples/database/d1-contact-form/migrations/
0001_create_contacts_table.sqlCopy migrations to your project root:
cp -r examples/database/d1-contact-form/migrations ./Each example's README includes a section on required bindings. Add these to your wrangler.jsonc:
Example: D1 Database Binding
Example: Environment Variables (Secrets)
# Set secrets for production
npx wrangler secret put ANTHROPIC_API_KEY
# For local development, create .dev.vars
echo 'ANTHROPIC_API_KEY=sk-ant-...' > .dev.varsAfter adding bindings, generate types so TypeScript knows about them:
npm run cf-typegenThis updates worker-configuration.d.ts with type definitions for your bindings.
Some examples may require additional packages:
# Check the example's README for any additional dependencies
npm install package-nameStart the development server and test your integration:
npm run devVisit http://localhost:5173 and test the feature.
Now that the example is integrated, customize it for your needs:
- Adjust styling to match your design
- Modify business logic
- Add additional validation
- Integrate with your existing features
- Add error handling specific to your use case
All examples follow these conventions:
examples/
├── ai/
│ ├── README.md # AI examples overview
│ └── simple-claude-chat/
│ ├── README.md # Documentation
│ ├── PRP.md # Product Requirement Plan
│ ├── src/ # Frontend code
│ │ ├── components/
│ │ │ └── ChatInterface.tsx
│ │ └── hooks/
│ │ └── useChat.ts
│ ├── worker/ # Worker code
│ │ └── routes/
│ │ └── chat.ts
│ └── wrangler.example.jsonc # Example configuration
├── database/
│ └── d1-contact-form/
│ ├── README.md
│ ├── PRP.md
│ ├── src/ # Frontend code
│ │ └── components/
│ │ └── ContactForm.tsx
│ ├── worker/ # Worker code
│ │ └── routes/
│ │ └── contacts.ts
│ ├── migrations/ # Database migrations
│ │ └── 0001_create_contacts.sql
│ └── wrangler.example.jsonc
└── README.md # This file
When integrating examples:
- Read the entire README before copying code
- Review the PRP.md to understand the implementation plan
- Test locally first before deploying
- Customize for your needs - examples are starting points
- Follow security best practices mentioned in each example
- Keep dependencies minimal following YAGNI principles
- Use TypeScript for type safety
- Copy blindly without understanding the code
- Skip configuration steps - bindings are essential
- Ignore security warnings in the documentation
- Deploy to production without thorough testing
- Commit secrets - always use environment variables
"Binding not found" error:
- Ensure you've added the binding to
wrangler.jsonc - Run
npm run cf-typegento generate types - Restart your dev server
TypeScript errors after copying code:
- Run
npm run cf-typegento update binding types - Check that you've copied all necessary files
- Verify imports match your project structure
Database migration fails:
- Check database name matches
wrangler.jsonc - Ensure you created the database with
wrangler d1 create - For local testing, use
--localflag
API calls fail in production but work locally:
- Check secrets are set with
wrangler secret put - Verify bindings are correctly configured
- Check CORS settings if making cross-origin requests
If you run into issues:
- Check the example's README - Most common issues are covered
- Review the PRP.md for detailed implementation guidance
- Review the main documentation:
- CLAUDE.md - Project guidelines and patterns
- AI_INTEGRATION.md - AI integration details (coming soon)
- CLOUDFLARE_WORKERS.md - Workers documentation (coming soon)
- Cloudflare Documentation:
- Community Support:
- Cloudflare Discord
- GitHub Issues on this repository
Have a useful pattern to share? Consider contributing an example:
- Follow the file structure conventions above
- Include a comprehensive README
- Add a PRP.md following the template in
.claude/templates/prp_base.md - Add inline comments explaining key concepts
- Test thoroughly before submitting
- Open a pull request
- Browse the available examples above
- Read the README for an example that interests you
- Follow the integration guide to add it to your project
- Customize and build something amazing
Happy coding!
{ "name": "my-project", // ... other config "d1_databases": [ { "binding": "DB", "database_name": "my-database", "database_id": "your-database-id-here" } ] }