API Design
Create Short URL
POST /api/vi/urls
Request body:
- longUrl (required): destination URL to shorten
- customAlias (optional): user generated short code instead of a system generated one
- expiresAt (optional): ISO 8601 timestamp to define when the short URL should stop working
Response body:
- shortCode: system generated or user generated
- shortUrl: full short URL
- longUrl: echo of the destination URL for confirmation
- createdAt: timestamp of creation
- expiresAt: timestamp of expiry if set
Authentication: API key in the authorization header. Every write request must be associated with a registered account for rate limiting and abuse tracking.
Rate limit: 100 URLs per hour per API key for free tier. Higher limits for paid accounts. Returns 429 (Too Many Requests) when exceeded with a Retry-After header indicating when the client can try again.
Redirect
GET /shortCode
- Returns a 302 (Found) response with the Location header set to the original long URL. The browser follows the redirect automatically.
- No authentication required. Anyone with the shortUrl can follow it. This is intentional. Short URLs are shared publicly and must work for everyone who clicks them.
- Error handling:
- If the short code doesn't exist or has expired, should return 404 (Not Found) with a JSON body explaining the error.
- For expired links, include a message indicating the link has expired rather than saying "not found". This helps users understand what happened and reduces confusion.
- For SEO and social media capability, following standard headers should be added:
- Cache-Control to control CDN behavior.
- X-Robots-Tag (optional) to tell search engines whether to index the short URL or the long URL.
301 vs 302
301 (Moved Permanently):
- Browser caches the long URL. Next time, the same short URL is clicked, the browser will directly redirect to the destination without engaging the shortener service.
- Reduces server load.
- Trade off:
- Analytics can not be tracked.
- Cannot the update the destination URL.
- Expiries can not be enforced, since the browser never checks back.
302 (Found):
- Redirect is temporary. Every click goes through the shortener service.
- Enables click analytics, destination updates and expiration enforcement.
- Trade off:
- higer server load because every click hits your infrastructure.
For most URL shortening services, 302 is the right default.
- Analytics and policy controls outweigh the server load increase.
- Server load can be handled by CDN caching.
- CDN caches the 302 response at edge locations, giving the load reduction of 301 with the flexibility of 302.
Rate Limiting
- Without rate limiting, a single malicious or misconfigured client could create millions of URLs, exhausting the short code space and consuming storage.
- Rate limiting protects shared resources and ensures fair usage.
- Implementation: per-API-key counters with sliding window limits, enforced at the API Gateway layer before requests reach the shortening service.