URL Shortener API: Create Short Links From Your Code

4 mins read
URL Shortener API: Create Short Links From Your Code

The Lnky URL shortener API lets your website, app or script create short links and read their stats without opening the dashboard. Typical uses: shorten order-tracking links before you send them by SMS or WhatsApp, add a short link to every new product in your shop, or pull click counts into your own reports. This guide covers getting a key, your first request, and examples in cURL, PHP, JavaScript and Python.

What you need

  • A plan that includes the API. API access is part of the Growth and Premium plans. See pricing.
  • An API key. In your dashboard, open the account menu at the top right, choose API Keys and press Generate Key. Keep it secret: anyone with the key can create and delete links in your account. If a key leaks, revoke it on the same page and make a new one.
  • The full reference. Every endpoint and parameter is listed in the API documentation.

How the URL shortener API works

  • The base address is https://lnky.pk/api.
  • Send your key in the header of every request: Authorization: Bearer YOUR_API_KEY.
  • Send data as JSON in the request body with Content-Type: application/json.
  • Every response is JSON with an error field: 0 means success, 1 means something went wrong and message says what.
  • Requests are rate limited per minute. Each response includes X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset headers, so your code can slow down before it hits the limit. The limit depends on your plan.

Shorten a link

Send a POST request to /api/url/add. Only url is required. Add custom to choose the name:

curl -X POST "https://lnky.pk/api/url/add" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://yourshop.pk/orders/10452", "custom": "order-10452"}'

The response contains the new link's ID and the short URL:

{"error": 0, "id": 3, "shorturl": "https://lnky.pk/order-10452"}

Other optional fields include password, expiry (for example 2026-12-31 23:59:00), description for your own note, metatitle and metadescription for social previews, and targeting by country, device or language. Which ones work depends on the features in your plan.

Examples in PHP, JavaScript and Python

PHP

$ch = curl_init("https://lnky.pk/api/url/add");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("LNKY_API_KEY"), "Content-Type: application/json"],
    CURLOPT_POSTFIELDS => json_encode(["url" => "https://yourshop.pk/sale"]),
]);
$res = json_decode(curl_exec($ch));
echo $res->error ? $res->message : $res->shorturl;

JavaScript (Node.js 18 or later)

const res = await fetch("https://lnky.pk/api/url/add", {
  method: "POST",
  headers: { "Authorization": `Bearer ${process.env.LNKY_API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ url: "https://yourshop.pk/sale" })
});
const data = await res.json();
console.log(data.error ? data.message : data.shorturl);

Python

import os, requests

r = requests.post(
    "https://lnky.pk/api/url/add",
    headers={"Authorization": f"Bearer {os.environ['LNKY_API_KEY']}"},
    json={"url": "https://yourshop.pk/sale"},
    timeout=10,
)
data = r.json()
print(data["message"] if data["error"] else data["shorturl"])

Keep the key in an environment variable or server config, as above. Never put it in JavaScript that runs in the visitor's browser, where anyone can read it.

Other useful endpoints

  • GET /api/urls?limit=20&page=1&order=click lists your links, sorted by date or clicks. Add q to search.
  • GET /api/url/{id} returns one link with its stats.
  • PUT /api/url/{id}/update changes a link, for example where it points.
  • DELETE /api/url/{id}/delete deletes a link.
  • /api/qr endpoints create and manage QR codes in the same way.

For what the stats mean, see our guide to link tracking. For readable names, see custom URL shortener tips.

Common errors

  • "A valid API key is required to use this service." The Authorization header is missing, or it doesn't start with Bearer followed by a space.
  • The key is rejected after it worked before: the key was revoked, or your plan no longer includes API access.
  • Too many requests: you've reached the per-minute limit. Wait until the time in X-RateLimit-Reset, then try again.
  • The custom name is taken: pick another custom value, or leave it out to get a random one.

Frequently asked questions

Is there a free URL shortener API?

API access is part of the Growth and Premium plans. On the free plan you can still create short links, QR codes and a bio page from the dashboard.

Where do I find my API key?

In your dashboard, open the account menu at the top right and choose API Keys. Press Generate Key, and revoke any key you no longer use.

How many requests can I send?

It depends on your plan. Every response includes X-RateLimit headers that show your limit and how many requests you have left this minute.

Can I create QR codes with the API?

Yes. The QR code endpoints create, update, list and delete QR codes. See the API documentation for the fields.

lnky.pk Free URL Shortener, Bio Pages + Analytics & QR
Try Free →

About the author

Arslan Ahmad

Arslan Ahmad

Founder of Lnky in Lahore. Builds simple web tools that help small businesses and creators in Pakistan sell online.