Skip to content

Resource Management

CruzJS provides CLI commands for managing Cloudflare infrastructure resources directly from your terminal. These commands wrap the Wrangler CLI with a streamlined interface.

Cloudflare Queues provide reliable message delivery between Workers. Use queues to offload background processing from your main application.

Creates a new Cloudflare Queue.

Terminal window
cruz queue create email-queue
cruz queue create order-processing

After creating a queue, you can scaffold a consumer worker for it:

Terminal window
cruz new queue-worker email-sender --queue email-queue

Lists all queues in your Cloudflare account.

Terminal window
cruz queue list

Deletes a queue. This is a destructive operation — any unprocessed messages are lost.

Terminal window
cruz queue delete email-queue

A complete queue setup involves three steps:

Terminal window
# 1. Create the queue on Cloudflare
cruz queue create notification-queue
# 2. Scaffold a consumer worker
cruz new queue-worker notification-sender --queue notification-queue
# 3. Install dependencies and develop locally
cd external-processes/notification-sender
npm install
npx wrangler dev
# 4. Deploy everything (main app + workers)
cruz deploy production

From your main Pages application or any other Worker, send messages to a queue using the queue binding:

// In a tRPC procedure or service
await env.NOTIFICATION_QUEUE.send({
type: 'user.welcome',
data: { userId: 'user_123', email: 'new@example.com' },
});

Secrets are encrypted environment variables stored on Cloudflare. They are available at runtime via the env parameter but are never exposed in logs or the dashboard.

Sets a secret for a specific environment.

Terminal window
# Set a secret for production
cruz secrets set --env production --name STRIPE_SECRET_KEY --value sk_live_xxx
# Set a secret for staging
cruz secrets set --env staging --name API_KEY --value my-api-key

If you omit --value, the CLI prompts you to enter it interactively (useful for sensitive values you do not want in your shell history).

Lists all secret names for an environment. Values are never displayed.

Terminal window
# List production secrets
cruz secrets list --env production

Example output:

Secrets (production)
AUTH_SECRET
STRIPE_SECRET_KEY
STRIPE_WEBHOOK_SECRET
SCM_ENCRYPTION_KEY
SecretDescription
AUTH_SECRETSession encryption key
STRIPE_SECRET_KEYStripe API key (if using billing)
STRIPE_WEBHOOK_SECRETStripe webhook signing secret
SCM_ENCRYPTION_KEYEncryption key for sensitive stored data
GOOGLE_CLIENT_IDOAuth client ID
GOOGLE_CLIENT_SECRETOAuth client secret

In CI/CD pipelines, pass secrets via environment variables and set them before deployment:

Terminal window
# In your CI script
cruz secrets set --env production --name AUTH_SECRET --value "$AUTH_SECRET"
cruz secrets set --env production --name STRIPE_SECRET_KEY --value "$STRIPE_SECRET_KEY"
cruz deploy production --yes

Cloudflare KV is a globally distributed key-value store. CruzJS uses it for caching, session storage, and fast lookups.

Creates a new KV namespace.

Terminal window
cruz kv create my-app-cache
cruz kv create session-store

The namespace ID is stored in .cruz.json and automatically included in the generated wrangler.toml during deployment.

Lists all KV namespaces in your Cloudflare account.

Terminal window
cruz kv list

Example output:

KV Namespaces
my-app-production-cache id: kv_abc123
my-app-staging-cache id: kv_def456
session-store id: kv_ghi789

KV namespaces configured in cruz.config.ts are available through the CloudflareContext:

import { CloudflareContext } from '@cruzjs/core/shared/cloudflare/context';
// In a service
const kv = CloudflareContext.getKV();
await kv.put('user:session:abc', JSON.stringify(sessionData), {
expirationTtl: 3600, // 1 hour
});
const session = await kv.get('user:session:abc', 'json');

Cloudflare R2 is an S3-compatible object storage service. Use it for file uploads, user avatars, document storage, and other binary data.

Creates a new R2 bucket.

Terminal window
cruz r2 create my-app-uploads
cruz r2 create user-avatars

Lists all R2 buckets in your Cloudflare account.

Terminal window
cruz r2 list

Example output:

R2 Buckets
my-app-uploads
user-avatars
backups

R2 buckets configured in cruz.config.ts are available through CloudflareContext:

import { CloudflareContext } from '@cruzjs/core/shared/cloudflare/context';
// Upload a file
const r2 = CloudflareContext.getR2();
await r2.put(`avatars/${userId}.jpg`, imageBuffer, {
httpMetadata: { contentType: 'image/jpeg' },
});
// Download a file
const object = await r2.get(`avatars/${userId}.jpg`);
if (object) {
const arrayBuffer = await object.arrayBuffer();
}
// Delete a file
await r2.delete(`avatars/${userId}.jpg`);
// List files
const listed = await r2.list({ prefix: 'avatars/' });
for (const object of listed.objects) {
console.log(object.key, object.size);
}

When cruz init creates resources, it uses a consistent naming convention:

<app-name>-<environment>-<resource-type>

For example, an app named my-app in the production environment would create:

ResourceName
D1 Databasemy-app-production-db
KV Namespacemy-app-production-cache
R2 Bucketmy-app-production-storage

This naming convention keeps resources organized when you have multiple environments.