JavaScript (Node.js) Examples
const BASE_URL = "https://api-sandbox.slimlinkdev.com/api";
async function createLink(apiKey, longUrl) {
const res = await fetch(`${BASE_URL}/v1/links/`, {
method: "POST",
headers: {
"X-API-Key": apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify({ longUrl }),
});
const body = await res.json();
if (!res.ok) {
throw new Error(`${body.code ?? res.status}: ${body.message}`);
}
return body.data;
}
async function getLink(apiKey, linkId) {
const res = await fetch(`${BASE_URL}/v1/links/${linkId}`, {
headers: { "X-API-Key": apiKey },
});
const body = await res.json();
if (!res.ok) {
throw new Error(`${body.code ?? res.status}: ${body.message}`);
}
return body;
}
async function bulkCreateLinks(apiKey, links) {
const res = await fetch(`${BASE_URL}/v1/links/bulk`, {
method: "POST",
headers: {
"X-API-Key": apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify(links),
});
if (res.status === 429) {
const remaining = res.headers.get("X-RateLimit-Remaining-Minute");
throw new Error(`Rate limited, remaining this minute: ${remaining}`);
}
const body = await res.json();
if (!res.ok) {
throw new Error(`${body.code ?? res.status}: ${body.message}`);
}
return body.data;
}
async function getClickAnalytics(apiKey, groupBy = "link") {
const params = new URLSearchParams({ group_by: groupBy });
const res = await fetch(`${BASE_URL}/v1/analytics/clicks?${params}`, {
headers: { "X-API-Key": apiKey },
});
const { data } = await res.json();
return data;
}
Handling rate limits and errors
The bulk-create endpoint returns 429 Too Many Requests with X-RateLimit-* headers when you exceed your account’s per-second or per-minute limit. Other endpoints don’t define a specific rate-limit error; treat any non-2xx response as failed and inspect code/message in the error envelope:
if (!res.ok) {
const body = await res.json();
console.error(body.code, body.message);
}