Introduction
The shrtnr API is a RESTful API built with Next.js App Router that allows you to create, manage, and track shortened URLs. The API uses JSON for request and response payloads and leverages NextAuth for session-based authentication.Base URL
All API endpoints are relative to your shrtnr instance:API Endpoints
Create Short Link
Generate a new shortened URL with optional custom slug
List Links
Retrieve all links created by the authenticated user
Update Link
Modify an existing link’s short code or expiration
Delete Link
Permanently remove a shortened link
Endpoint Reference
POST /api/shorten
Create a new shortened link. Anonymous users can create links with auto-generated codes, while authenticated users can use custom slugs. Request Body:url(required): The original URL to shorten. Must be a valid HTTP/HTTPS URL.customSlug(optional): Custom short code (1-20 chars: letters, numbers,_,-). Requires authentication.expiresInDays(optional): Link expiration in days (1-30). Default: 7 days.
Custom slugs require authentication. Anonymous users receive auto-generated 8-character codes.
GET /api/links
Retrieve all links created by the authenticated user, ordered by creation date (newest first). Authentication: Required Response:id: Unique numeric identifiershort_code: The short code used in the URLoriginal_url: The destination URLcreated_at: ISO 8601 timestamp of creationexpires_at: ISO 8601 expiration timestamp (ornullfor no expiration)custom_slug: Boolean indicating if this was a custom slugclicks: Total number of times this link was accesseduser_id: ID of the user who created the link
PATCH /api/links/[id]
Update an existing link’s short code or expiration date. Only the link owner can update their links. Authentication: Required Request Body:shortCode(optional): New short code (must follow slug rules: 1-20 chars, alphanumeric,_,-)expiresAt(optional): New expiration date as ISO 8601 string, ornullto remove expiration
Changing the short code will invalidate the old URL and clear the Redis cache.
DELETE /api/links/[id]
Permanently delete a shortened link. Only the link owner can delete their links. Authentication: Required Response:Error Responses
All endpoints return consistent error responses:400 Bad Request: Invalid input (malformed URL, invalid slug, etc.)401 Unauthorized: Authentication required or session expired404 Not Found: Link not found or you don’t have permission500 Internal Server Error: Server-side error occurred503 Service Unavailable: Database not initialized
Rate Limiting
Currently, shrtnr does not implement rate limiting at the API level. Consider implementing this based on your infrastructure needs (e.g., using Next.js middleware or a reverse proxy).
Data Persistence
Shrtnr uses a dual-storage approach:- PostgreSQL: Persistent storage for all link data, user accounts, and analytics
- Redis: High-speed cache for URL resolution and improved performance
Next Steps
Authentication
Learn how to authenticate API requests
Shorten Endpoint
Create short links via API