Quickstart: Cloudflare Workers
Deploy PACKAGE.broker on Cloudflare Workers for edge-optimized, serverless package proxying.
Choose Your Deployment Method
We offer two ways to deploy:
- CLI Setup (recommended) - Initialize configuration files and follow setup steps
- GitHub Template - Fork-and-deploy with automated GitHub Actions
Method 1: CLI Setup (Recommended)
Prerequisites
- Node.js 18+ installed
- A Cloudflare account
- Authenticated with Cloudflare (
npx wrangler login)
Step 1: Install and Initialize
$ npm init -y
$ npm install @package-broker/cli @package-broker/main
$ npx package-broker init
Note: Packages are available from npmjs.org (default) or GitHub Packages. To install from GitHub Packages, use:
$ npm install @package-broker/cli @package-broker/main --registry https://npm.pkg.github.com
The CLI will:
- Create
wrangler.tomlfrom template - Copy migration files to
migrations/directory - Display next steps for configuration
What Happens Automatically
- ✅ Creates
wrangler.tomlconfiguration file - ✅ Copies database migration files to
migrations/directory - ✅ Displays next steps for manual configuration
Note: The CLI currently provides basic initialization. For automated resource creation and deployment, see the Advanced: Manual Setup section below, or use the GitHub Template method for fully automated deployment.
Step 2: Configure and Deploy
After running npx package-broker init, follow the displayed instructions to:
-
Edit
wrangler.tomlwith your configuration:- Set your worker name
- Configure encryption key (generate with:
openssl rand -base64 32) - Optionally configure SMTP for email sending (see SMTP Configuration below)
-
Login to Cloudflare:
$ npx wrangler login -
Create Cloudflare resources (see Advanced: Manual Setup for detailed steps):
- D1 database
- KV namespace
- R2 bucket
- Queue (if using paid tier)
-
Update
wrangler.tomlwith the generated resource IDs -
Set encryption key as secret:
$ npx wrangler secret put ENCRYPTION_KEY -
Set SMTP secrets (optional, for email sending):
$ npx wrangler secret put SMTP_PASSSee SMTP Configuration below for details.
-
Apply database migrations:
$ npx wrangler d1 migrations apply <worker-name>-db --remote -
Deploy to Cloudflare:
$ npx wrangler deploy
After Deployment
- Open your Worker URL (shown in deployment output)
- Complete initial setup (email + password)
- Create an access token in the dashboard
- Start adding repository sources
Advanced: Manual Setup
If you prefer manual control or need to customize the setup process, follow these detailed steps:
Manual Resource Creation
-
Create D1 database:
$ npx wrangler d1 create <worker-name>-dbCopy the
database_idfrom the output. -
Create KV namespace:
$ npx wrangler kv namespace create <worker-name>-kvCopy the
idfrom the output. -
Create R2 bucket:
$ npx wrangler r2 bucket create <worker-name>-artifacts -
Create Queue (paid tier only):
$ npx wrangler queues create <worker-name>-queue
Manual Configuration
-
Edit
wrangler.tomlwith resource IDs from above:- Update
database_idin[[d1_databases]]section - Update
idin[[kv_namespaces]]section - Update
bucket_namein[[r2_buckets]]section (if different) - Update
queuename in[[queues.producers]]and[[queues.consumers]]sections (if using paid tier)
- Update
-
Generate encryption key:
$ openssl rand -base64 32Copy the generated key.
-
Set encryption key as Cloudflare secret:
$ npx wrangler secret put ENCRYPTION_KEYPaste the encryption key when prompted.
-
Apply database migrations:
$ npx wrangler d1 migrations apply <worker-name>-db --remote -
Deploy Worker:
$ npx wrangler deploy
See CLI Reference for more details on the init command.
Method 2: GitHub Template (CI/CD)
Prerequisites
- A GitHub account
- A Cloudflare account
- Repository must be initialized: The template includes
package.jsonandpackage-lock.jsonwith required dependencies
Step 1: Use Template
- Go to package-broker/cloudflare-template
- Click "Use this template"
- Create your repository
Important: The template repository already includes package.json and package-lock.json with all required dependencies. Do not delete these files.
Step 2: Configure Secrets and Variables
In your repository: Settings → Secrets and variables → Actions
Required Secrets
CLOUDFLARE_API_TOKEN- Your Cloudflare API token- Create at: https://dash.cloudflare.com/profile/api-tokens
- Required permissions: See API Token Permissions below
ENCRYPTION_KEY- Generate with:openssl rand -base64 32
Required Variables
CLOUDFLARE_ACCOUNT_ID- Your Cloudflare account ID (found in dashboard)
Optional Variables
WORKER_NAME- Custom worker name (defaults to repository name)CLOUDFLARE_TIER-freeorpaid(defaults tofree)CUSTOM_DOMAIN- Custom domain to use (e.g.,app.example.com). After deployment, you'll receive instructions to create a CNAME record in Cloudflare DNS.
Step 3: Deploy
Push to main branch or manually trigger the workflow from the Actions tab.
The workflow automatically:
- Validates configuration
- Installs dependencies using
npm ci(requirespackage-lock.json) - Creates Cloudflare resources via
@package-broker/cloudflareCLI - Generates
wrangler.tomlat runtime (not committed to repository) - Sets secrets
- Applies migrations
- Deploys the Worker
API Token Permissions
Your CLOUDFLARE_API_TOKEN must be a scoped API token (not Global API Key) with at least Edit permissions for:
- Workers - Workers Scripts (deploy/update Worker)
- D1 - Create/list databases, run migrations
- KV - Create/list namespaces
- R2 - Create/list buckets
- Queues (paid tier only) - Create/list queues
Optional (recommended for better error messages):
- Account - Account Settings (Read)
Important:
- Do not use the Global API Key
- Keep the token account-scoped and minimal
- Route/zone permissions are only needed for custom domains (not required for
workers.dev)
Security: Encryption Key
The ENCRYPTION_KEY is never stored in wrangler.toml. It's set as a Cloudflare secret:
- CLI: Automatically set via
wrangler secret put ENCRYPTION_KEY - GitHub Actions: Set automatically during workflow
- Manual: Use
wrangler secret put ENCRYPTION_KEYor set via Cloudflare dashboard
To update the key:
- Via CLI:
wrangler secret put ENCRYPTION_KEY - Via Dashboard: Workers & Pages → Settings → Variables and Secrets
SMTP Configuration (Optional)
To enable email sending for user invitations, configure SMTP in wrangler.toml:
[vars]
# ... existing vars ...
SMTP_HOST = "smtp.sendgrid.net"
SMTP_PORT = "587"
SMTP_USER = "apikey"
SMTP_FROM = "noreply@example.com"
Important: For production, store SMTP_PASS as a Cloudflare secret instead of in [vars]:
$ npx wrangler secret put SMTP_PASS
When prompted, enter your SMTP password or API key.
Email Provider Examples
SendGrid (Recommended for Cloudflare)
[vars]
SMTP_HOST = "smtp.sendgrid.net"
SMTP_PORT = "587"
SMTP_USER = "apikey"
SMTP_FROM = "noreply@yourdomain.com"
Then set the API key as a secret:
$ npx wrangler secret put SMTP_PASS
# Enter: SG.your-sendgrid-api-key
Gmail
[vars]
SMTP_HOST = "smtp.gmail.com"
SMTP_PORT = "587"
SMTP_USER = "your-email@gmail.com"
SMTP_FROM = "your-email@gmail.com"
Then set the app-specific password as a secret:
$ npx wrangler secret put SMTP_PASS
Note: Gmail requires an app-specific password for SMTP.
AWS SES
[vars]
SMTP_HOST = "email-smtp.us-east-1.amazonaws.com"
SMTP_PORT = "587"
SMTP_USER = "AKIAIOSFODNN7EXAMPLE"
SMTP_FROM = "noreply@yourdomain.com"
Then set the IAM secret access key as a secret:
$ npx wrangler secret put SMTP_PASS
Note: Email sending is optional. If SMTP is not configured, user creation will still work, but no emails will be sent. See Configuration Reference for complete SMTP documentation.
Initial Setup
After deployment, complete the initial setup:
- Open your Worker URL in a browser
- Enter your email address
- Set a secure password
- Click "Complete Setup"
Create an Access Token
- Navigate to Access Tokens in the dashboard
- Click Create Token
- Enter a description
- Select permissions (readonly or write)
- Click Create
- Copy the token immediately
Verify Installation
Test the health endpoint:
$ curl https://your-worker.workers.dev/health
Expected output:
{
"status": "ok",
"timestamp": 1234567890
}
Free Tier Limitations
Cloudflare Workers free tier includes:
- 100,000 requests/day per account
- 10ms CPU time per request
- 128MB memory per request
- D1: 5GB storage, 5 million reads/month, 100,000 writes/month
- R2: 10GB storage, 1 million Class A operations/month
- KV: 100,000 reads/day, 1,000 writes/day
For production workloads, consider the Workers Paid plan ($5/month) for:
- Unlimited requests
- Higher CPU time limits
- Queue support for async operations
Troubleshooting
CLI: Authentication Failed
If the CLI says you're not authenticated:
$ npx wrangler login
CLI: Resource Creation Fails
- Verify you're authenticated:
npx wrangler whoami - Check your API token has the required permissions (see above)
- Ensure you have available quota for the resource type
GitHub Actions: Workflow Fails
- Missing package-lock.json: The action requires both
package.jsonandpackage-lock.jsonto be committed. If you see an error about missingpackage-lock.json, runnpm installlocally and commit both files. - Missing @package-broker/cloudflare: Ensure
package.jsonincludes@package-broker/cloudflareas a dependency. The template repository already includes this. - Verify all required secrets and variables are set
- Check secret/variable names are exact (case-sensitive)
- Review workflow logs for specific error messages
- See SETUP.md for detailed troubleshooting
Database Migration Errors
Migrations are applied automatically. If they fail:
# Check migration status
$ npx wrangler d1 migrations list <worker-name>-db --remote
# Manually re-run migrations if needed
$ npx wrangler d1 migrations apply <worker-name>-db --remote
Can't Access Dashboard
- Verify the Worker deployed successfully:
npx wrangler deployments list - Check Worker logs:
npx wrangler tail - Ensure you're using HTTPS (required for Workers)
Upgrading PACKAGE.broker
CLI Method
$ npm update @package-broker/cli @package-broker/main
$ npx wrangler deploy
GitHub Template Method
Update package.json in your repository to bump dependency versions:
{
"dependencies": {
"@package-broker/main": "^0.9.0",
"@package-broker/ui": "^0.9.0",
"@package-broker/cloudflare": "^0.9.0"
}
}
Then run npm install locally to update package-lock.json, commit both files, and push to main branch. The workflow will automatically deploy the new version.
Project Structure
After initialization, your project structure will look like:
package-broker-deployment/
├── node_modules/
├── migrations/ # Database migration files (auto-copied)
├── wrangler.toml # Cloudflare Workers configuration (auto-generated)
├── package.json
└── package-lock.json
Note: wrangler.toml does not contain ENCRYPTION_KEY - it's set as a Cloudflare secret.
Next Steps
- Learn about production deployment: Deployment Overview
- Understand authentication: Token Management
- Review operations: Backups and Restore and Scaling
- Check Roadmap for upcoming features