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:

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:

  1. Send a POST request with the long URL to the API.
  2. The API generates a unique short code and stores the mapping.
  3. Use the returned short code in your application or share it directly.
  4. 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

Advanced Features

Once the basic shortener is working, you can add more powerful features:

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.