This guide provides a structured approach to building a URL shortener API across different languages and frameworks. The project is designed to be built iteratively, allowing for skill development and gradual feature expansion.
- Iterative Development: Build in small, manageable steps
- Idiomatic Code: Use language/framework best practices and conventions
- Educational Focus: Explain decisions, patterns, and trade-offs
- Incremental Complexity: Start simple, add features progressively
- Provide code for ONE feature/step at a time
- Explain the "why" behind architectural decisions
- Include relevant comments in code
- Suggest testing approaches for each step
- Point out language/framework-specific patterns
- Ask clarifying questions before implementing complex features
- URL Shortening: Convert long URLs to short codes
- URL Redirection: Redirect short codes to original URLs
- Basic Validation: Ensure URLs are valid before shortening
- Storage: Persist URL mappings
- Custom short codes
- Analytics (click tracking, timestamps)
- Expiration dates
- Rate limiting
- User authentication
- Bulk operations
- QR code generation
- Admin dashboard
# Adjust these as needed for different implementations
short_code_length: 6
short_code_charset: "abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"
max_url_length: 2048
enable_custom_codes: false
enable_analytics: false
enable_expiration: false- RESTful API design
- Environment-based configuration
- Proper error handling and HTTP status codes
- Input validation and sanitization
- Database abstraction (where applicable)
- Project Setup
- Initialize project structure
- Set up basic dependencies
- Create configuration system
- Set up basic routing
- Core Data Model
- Define URL mapping structure
- Set up database/storage layer
- Create basic CRUD operations
- Basic API Endpoints
- POST /shorten - Create short URL
- GET /{code} - Redirect to original URL
- Basic error handling
- API, Authentication & Error Handling
- Comprehensive error responses
- Authentication (required for creating short URLs)
- Rate limiting
- Configuration & Environment
- Environment variables
- Configurable settings
- Logging setup
- Analytics (if enabled)
- Click tracking
- Timestamp logging
- Basic statistics endpoint
- Custom Features (if enabled)
- Custom short codes
- Expiration handling
- Rate limiting
POST /api/shorten
Body: { "url": "https://example.com/very/long/url" }
Response: { "short_url": "http://short.ly/abc123", "code": "abc123" }
GET /{code}
Response: 302 redirect to original URL
GET /api/stats/{code} (if analytics enabled)
Response: { "clicks": 42, "created_at": "2024-01-01T00:00:00Z" }
{
"error": "Invalid URL",
"code": "INVALID_URL",
"message": "The provided URL is not valid"
}- Go: Use gorilla/mux or gin for routing, proper middleware patterns
- Python: FastAPI for modern async, Flask for simplicity, proper virtual environments
- Node.js: Express.js patterns, async/await, proper error middleware
- Rust: Actix-web or Axum, proper error handling with Result types
- Java: Spring Boot, proper dependency injection, exception handling
- C#: ASP.NET Core, proper dependency injection, async patterns
- Plan: Discuss the current step and approach
- Implement: Write code for the specific feature
- Test: Suggest testing approach and examples
- Review: Explain what was built and why
- Next: Identify the next logical step
- What's the minimal implementation for this step?
- What language/framework patterns should we follow?
- What potential issues should we consider?
- How can we test this feature?
- What's the next logical step?
- Always start by asking which language/framework to use
- Confirm the current phase/step before implementing
- Provide code for ONE specific feature at a time
- Explain architectural decisions and patterns
- Suggest testing approaches
- Ask if constraints need adjustment before complex features
- Choose your target language/framework
- Specify which step/feature to implement next
- Adjust constraints in this document as needed
- Ask for explanations of unfamiliar patterns
- Request testing examples when needed
- Language/Framework: PHP, Laravel
- Phase: 1
- Last completed step: 1
Note: Update this document as you progress through different implementations to track your learning and maintain consistency across different technology stacks.