DEV Community

nly.kr
nly.kr

Posted on

How to Shorten URLs from Your App with a Simple API

When building a small app or side project, there are times when you need to generate short links automatically.

You could build your own URL shortening system, but for many projects that means adding a database, redirect logic, unique code generation, and analytics just for one small feature.

A simpler option is to use a URL shortening API.

I’ve been working with the nly.kr URL Shortener, which also provides an API for creating short URLs programmatically.

In this post, I’ll show a few simple examples.


API Endpoint

The endpoint for creating a short URL is:

POST https://nly.kr/api/shorten
Enter fullscreen mode Exit fullscreen mode

The API expects form-encoded data.

Two parameters are required:

  • url — the original HTTP or HTTPS URL
  • category — a category key for the link

You’ll also need an API key for authentication.

The recommended header is:

X-API-Key: your_API_KEY
Enter fullscreen mode Exit fullscreen mode

You can find the full API documentation here:

nly.kr URL Shortener API


Example with cURL

The quickest way to test the API is with cURL.

curl -X POST "https://nly.kr/api/shorten" \
  -H "X-API-Key: your_API_KEY" \
  -H "Accept: application/json" \
  -d "url=https://example.com" \
  -d "category=dev"
Enter fullscreen mode Exit fullscreen mode

A successful response looks similar to this:

{
  "status": "success",
  "short_url": "https://nly.kr/Ab3XkQ",
  "original_url": "https://example.com",
  "code": "Ab3XkQ",
  "member_idx": 123,
  "category": "dev"
}
Enter fullscreen mode Exit fullscreen mode

Once you have the short_url, you can store it, display it to the user, or use it anywhere your application needs a shorter shareable link.


JavaScript Example

You can call the same endpoint with fetch().

const apiKey = "your_API_KEY";
const longUrl = "https://example.com";
const category = "dev";

fetch("https://nly.kr/api/shorten", {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "X-API-Key": apiKey,
    "Accept": "application/json",
  },
  body: new URLSearchParams({
    url: longUrl,
    category: category,
  }),
})
  .then(async (response) => {
    const data = await response.json();

    if (!response.ok) {
      throw new Error(data.message || "Failed to shorten URL");
    }

    return data;
  })
  .then((data) => {
    console.log("Short URL:", data.short_url);
  })
  .catch((error) => {
    console.error("Error:", error);
  });
Enter fullscreen mode Exit fullscreen mode

This can be useful when you want to generate short links from an internal tool, dashboard, CMS, or other web application.


Python Example

The same request is also straightforward with Python and the requests library.

import requests

api_key = "your_API_KEY"
long_url = "https://example.com"
category = "dev"

response = requests.post(
    "https://nly.kr/api/shorten",
    headers={
        "X-API-Key": api_key,
        "Accept": "application/json"
    },
    data={
        "url": long_url,
        "category": category
    },
    timeout=10
)

print("HTTP:", response.status_code)
print(response.json())
Enter fullscreen mode Exit fullscreen mode

If the request succeeds, the JSON response contains the generated short URL.


Categories

The API requires a category when creating a link.

Some available category keys include:

ai
dev
design
productivity
pdf
shopping
news
education
media
community
finance
travel
download
tools
etc
Enter fullscreen mode Exit fullscreen mode

For example, a developer documentation link could use:

category=dev
Enter fullscreen mode Exit fullscreen mode

while an AI-related service could use:

category=ai
Enter fullscreen mode Exit fullscreen mode

Handling Errors

It’s also worth checking the HTTP status instead of assuming every request succeeded.

For example, the API may return an error response such as:

{
  "status": "error",
  "message": "카테고리는 필수입니다."
}
Enter fullscreen mode Exit fullscreen mode

Typical API errors can include invalid parameters, missing authentication, unsupported methods, or server errors.

In production code, always handle both HTTP errors and malformed responses.


When Is a URL Shortening API Useful?

I find this kind of API useful for things like:

  • Automatically generating links from a CMS
  • Creating short links for notification messages
  • Sharing long campaign or tracking URLs
  • Generating links from internal admin tools
  • Creating cleaner URLs for documentation
  • Automating links in side projects

For one-off links, using a web interface is usually faster.

You can create one directly with the nly.kr URL Shortener.

But when link generation becomes part of an application workflow, an API is much easier to automate.


Final Thoughts

URL shortening is a small feature, but implementing and maintaining a complete shortening service yourself can be unnecessary for many projects.

A simple API lets you keep the application logic focused on your actual product while handling short-link generation separately.

If you want to experiment with it:

For small apps, automation scripts, and side projects, that’s often all you need.

Top comments (0)