A modern URL shortening web application built with Next.js, Tailwind CSS, and PostgreSQL (Neon).
- Create Short Links: Generate shortened URLs with optional custom codes
- Link Management: View, filter, sort, and delete all your links
- Analytics: Track clicks and view detailed statistics for each link
- URL Validation: Ensures valid URLs and unique codes
- Responsive Design: Beautiful, mobile-friendly UI with Tailwind CSS
- Health Check: Built-in health check endpoint for monitoring
Create a new shortened URL.
Request Body:
{
"url": "https://example.com",
"code": "optional-custom-code"
}Response (201):
{
"code": "abc123",
"originalUrl": "https://example.com",
"clicks": 0,
"createdAt": "2024-01-01T00:00:00.000Z"
}Error Responses:
400: Invalid URL or code format409: Code already exists500: Internal server error
List all links with optional filtering and sorting.
Query Parameters:
sortBy: Sort column (created_at, clicks, code, original_url)order: Sort order (asc, desc)filter: Search filter for code or URL
Response (200):
[
{
"code": "abc123",
"originalUrl": "https://example.com",
"clicks": 5,
"createdAt": "2024-01-01T00:00:00.000Z",
"lastClickedAt": "2024-01-02T00:00:00.000Z"
}
]Get statistics for a specific link.
Response (200):
{
"code": "abc123",
"originalUrl": "https://example.com",
"clicks": 5,
"createdAt": "2024-01-01T00:00:00.000Z",
"lastClickedAt": "2024-01-02T00:00:00.000Z"
}Error Responses:
404: Link not found500: Internal server error
Delete a link.
Response (200):
{
"message": "Link deleted successfully"
}Error Responses:
404: Link not found500: Internal server error
Redirect to the original URL (302 redirect) and track clicks.
Error Responses:
- Redirects to
/?error=notfoundif link doesn't exist
Health check endpoint. Returns system status and uptime information.
Response (200):
{
"status": "healthy",
"timestamp": "2024-01-01T00:00:00.000Z",
"uptime": 3600,
"uptimeFormatted": "1h 0m 0s"
}Error Responses:
503: Database connection failed405: Method not allowed
Custom codes must match the pattern: [A-Za-z0-9]{6,8} (6-8 alphanumeric characters).
- Node.js 18+
- PostgreSQL database (Neon recommended)
- Clone the repository:
git clone <repository-url>
cd url-shortener- Install dependencies:
npm install- Set up environment variables:
cp .env.example .envEdit .env and add your Neon PostgreSQL connection string:
DATABASE_URL=postgresql://user:password@host:port/database?sslmode=require
- Run the development server:
npm run dev- Open http://localhost:3000 in your browser.
-
Push your code to a Git repository (GitHub, GitLab, or Bitbucket).
-
Import your project in Vercel.
-
Add environment variables in Vercel dashboard:
DATABASE_URL: Your Neon PostgreSQL connection string
-
Deploy! Vercel will automatically build and deploy your application.
The application automatically creates the following table on first run:
CREATE TABLE links (
id SERIAL PRIMARY KEY,
code VARCHAR(8) UNIQUE NOT NULL,
original_url TEXT NOT NULL,
clicks INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_clicked_at TIMESTAMP
);The codebase is structured for easy testing. You can add test files using your preferred testing framework (Jest, Vitest, etc.).
MIT