Skip to main content
Starter Kit - Ships with your template. You own it - modify freely.

Overview

The redirect system provides enterprise-grade URL shortening with three link types:
  • Permanent: Traditional short links that never expire
  • Expiring: Time-limited links with automatic cleanup
  • Single-use: Magic links for secure one-time access
All redirect data is stored in Cloudflare KV with automatic TTL management.

Quick Start

1. Create a Redirect

Response:

3. Manage Redirects

Ensembles

The redirect system consists of two ensembles:

Resolve Ensemble

File: ensembles/system/redirects/resolve.yaml Handles public redirect resolution at /go/:slug.
Key Features:
  • Public access (no authentication)
  • Automatic single-use link marking
  • Comprehensive error handling
  • Low route priority (doesn’t override explicit routes)

API Ensemble

File: ensembles/system/redirects/api.yaml Provides authenticated CRUD operations at /api/v1/redirects.
Key Features:
  • RESTful API design
  • Authentication required
  • Supports all CRUD operations
  • Pagination for list endpoint
  • Filter by type, used status, campaign

Agent Reference

redirect Agent

File: agents/system/redirect/redirect.yaml The core redirect agent that powers both ensembles.

Input Schema

Output Schema

Configuration Schema

Configuration

1. Add KV Binding

Add to your wrangler.toml:
Create the KV namespace:

2. Configure Base Path

The default base path is /go. To customize:

3. Configure Slug Generation

4. Set Default Redirect Type

Examples

Output:
Output:
Output:

Resolve Without Marking Used

Useful for previewing or analytics:

List with Filters

Output:

Update Redirect

Delete Redirect

Customization

Change Slug Algorithm

Edit agents/system/redirect/redirect.ts:

Add Analytics Tracking

Edit agents/system/redirect/redirect.ts in the resolve function:

Add Custom Metadata Fields

Edit agents/system/redirect/redirect.yaml:

Custom Error Pages

Edit ensembles/system/redirects/resolve.yaml:

Add Rate Limiting

Add Webhook Notifications

Common Use Cases

A/B Testing

Best Practices

  • Permanent: Public links, documentation, long-term campaigns
  • Expiring: Time-sensitive promotions, temporary access
  • Single-use: Magic links, password resets, secure tokens

2. Set Appropriate Expiration Times

3. Use Metadata for Organization

4. Choose Status Codes Wisely

  • 301: Permanent redirect (cached by browsers)
  • 302: Temporary redirect (default, allows updates)
  • 307: Temporary redirect (preserves HTTP method)
  • 308: Permanent redirect (preserves HTTP method)

5. Implement Analytics

Track redirect usage:
  • Click counts
  • Geographic distribution
  • Device types
  • Referrer sources
  • Time-based patterns

6. Monitor and Clean Up

  • List unused redirects periodically
  • Remove expired redirects manually if needed
  • Archive old campaign data
  • Monitor KV storage usage

7. Secure Custom Slugs

Error Handling

All errors include descriptive messages:

Next Steps

Slug Generator

Generate URL-safe slugs

Starter Kit Overview

Explore other starter kit components

KV Storage

Learn about Cloudflare KV

Triggers

Trigger configuration