Build Your Own URL Shortener in 10 Minutes — Free API Tutorial
Short links are everywhere. Twitter wraps every t.co link, Bitly processes billions of clicks per month, and every marketing team wants clean, trackable URLs. But what if you want to build your own URL shortener instead of paying for a third-party service? The answer is simpler than you think.
In this tutorial, you will build a fully functional link shortener in about 10 minutes using the ProfitEngine API. No database setup, no server configuration, no complex routing. Just a few lines of code and you are running your own URL shortening service.
Why Build Your Own URL Shortener?
Before jumping into the code, here are a few reasons building your own shortener makes sense:
- Full control — You own the data and the redirects. No dependency on a third party that might change pricing or shut down.
- Custom domain — Use your own brand domain instead of bit.ly or t.co. Every shortened link reinforces your brand.
- Privacy — Your links and click data stay on your infrastructure. No third party tracking your users.
- Cost — A simple shortener costs almost nothing to run, especially with a generous free API tier.
How the URL Shortener API Works
The ProfitEngine URL shortener API does the heavy lifting for you. You send a long URL, and the API returns a short, unique code. When someone visits the shortened URL, your application can look up the original URL and redirect them. The API handles the encoding, collision detection, and storage.
The workflow is straightforward:
- Send a POST request with the long URL to the API.
- The API generates a unique short code and stores the mapping.
- Use the returned short code in your application or share it directly.
- When a user visits the short link, your app fetches the original URL from the API and redirects.
Getting Started
First, sign up at ProfitEngine on RapidAPI and grab your API key. The free tier gives you plenty of requests to build and test your shortener.
Shorten a URL with cURL
Here is the simplest way to create a short link:
curl -X POST "https://profitengine-api.p.rapidapi.com/api/shorten" \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d {url: https://example.com/very/long/url/that/needs/shortening}
The response looks like this:
{
"original_url": "https://example.com/very/long/url/that/needs/shortening",
"short_code": "aB3xK9",
"short_url": "https://profitengine-api.p.rapidapi.com/s/aB3xK9",
"created_at": "2026-07-27T12:00:00Z"
}
JavaScript (Node.js) Integration
Here is how to integrate URL shortening into a Node.js application:
const axios = require("axios");
const API_KEY = "YOUR_API_KEY";
async function shortenUrl(longUrl) {
const { data } = await axios.post(
"https://profitengine-api.p.rapidapi.com/api/shorten",
{ url: longUrl },
{ headers: { "x-api-key": API_KEY } }
);
return data.short_url;
}
// Usage
shortenUrl("https://example.com/some/very/long/path")
.then((short) => console.log("Short URL:", short))
.catch((err) => console.error("Error:", err.message));
Python Integration
For Python applications, the integration is just as clean:
import requests
API_KEY = "YOUR_API_KEY"
def shorten_url(long_url):
response = requests.post(
"https://profitengine-api.p.rapidapi.com/api/shorten",
json={"url": long_url},
headers={"x-api-key": API_KEY}
)
response.raise_for_status()
data = response.json()
return data["short_url"]
# Usage
short = shorten_url("https://example.com/a-very-long-url")
print(f"Short URL: {short}")
Building the Redirect Handler
Creating short links is only half the solution. You also need a way to resolve them. Here is a simple Express.js server that handles the redirects:
const express = require("express");
const axios = require("axios");
const app = express();
const API_KEY = "YOUR_API_KEY";
app.get("/s/:code", async (req, res) => {
try {
const { data } = await axios.get(
"https://profitengine-api.p.rapidapi.com/api/resolve",
{ params: { code: req.params.code },
headers: { "x-api-key": API_KEY } }
);
res.redirect(301, data.original_url);
} catch (error) {
res.status(404).send("Link not found");
}
});
app.listen(3000, () => console.log("Shortener running on port 3000"));
Best Practices for Running a Link Shortener
- Use 301 redirects — Permanent redirects (301) tell search engines to transfer SEO value to the target URL. Use 302 for temporary links or analytics tracking.
- Set up analytics — Even basic click counting helps you understand which links perform best. Log each redirect with a timestamp and referrer.
- Implement rate limiting — Public shorteners attract abuse. Limit how many links a single IP can create per hour.
- Validate URLs before shortening — Check that the submitted URL is properly formatted and reachable before creating a short link.
- Consider custom aliases — Let users specify custom short codes for branded links. The API supports this with an optional alias parameter.
Advanced Features
Once the basic shortener is working, you can add more powerful features:
- Custom aliases — Pass an optional "alias" field to create memorable short links like yourbrand.com/s/black-friday.
- Expiration dates — Set links to expire automatically after a certain date. Perfect for time-limited promotions.
- Password protection — Require a password before redirecting. Great for private content.
- Click tracking — Log every redirect with user-agent and IP information for analytics.
These advanced features differentiate your shortener from generic services and let you offer premium functionality to your users.
Try It Now
You now have everything you need to build your own URL shortener. It took about 10 minutes and fewer than 50 lines of code. The URL shortener API handles the complexity while you focus on the user experience. Subscribe to ProfitEngine on RapidAPI and start creating short links today.