Skip to main content
The Node Forge Logo

The Node Forge presents:

URL Shortener

License: MIT

Made with TypeScript

NPM Version Build Status Platform

Live Documentation

A simple and flexible URL shortener package for JavaScript/TypeScript applications. This package helps developers create branded or custom short URLs for their projects.

✨ Features

  • Custom Aliases – Shorten with a readable slug.
  • Expiration Support – Set TTL for each link.
  • Self-hostable – Use with your own Express backend.
  • Frontend-Friendly – Call via API from client apps.

📦 Installation

npm install @the-node-forge/url-shortener

or using Yarn:

yarn add @the-node-forge/url-shortener

🛠️ Basic Usage

1️⃣ 🌐 JavaScript/TypeScript Example

import { shortenUrl } from '@the-node-forge/url-shortener';

(async () => {
  const shortUrl = await shortenUrl('https://example.com/some/long/path', {
    alias: 'launch',
    expiresIn: '7d',
  });
  console.log(shortUrl); // Output: https://sho.rt/launch
})();

2️⃣ 🌐 Express Integration Example

import express from 'express';
import { shortenUrl } from '@the-node-forge/url-shortener';

const app = express();
app.use(express.json());

app.post('/shorten', async (req, res) => {
  const { url, options } = req.body;
  try {
    const shortUrl = await shortenUrl(url, options);
    res.json({ shortUrl });
  } catch (err) {
    res.status(400).json({ error: err.message });
  }
});

app.listen(3000, () => console.log('URL shortener running on port 3000'));

API Reference

shortenUrl Function

shortenUrl(longUrl: string, options?: {
  alias?: string;
  expiresIn?: string;
}): Promise<string>
ParameterTypeDescription
longUrlstringThe original URL to be shortened
aliasstringOptional custom slug for the shortened URL
expiresInstringOptional TTL for the short link (e.g. "7d")

Returns

  • Promise<string> – The generated shortened URL.

💡 Contributing

Contributions are welcome! Please submit issues or pull requests.


⭐ Support

If you find this package useful, please give it a ⭐ on GitHub