Python Examples

import requests

BASE_URL = "https://api-sandbox.slimlinkdev.com/api"

def create_link(api_key, long_url):
    resp = requests.post(
        f"{BASE_URL}/v1/links/",
        headers={"X-API-Key": api_key},
        json={"longUrl": long_url},
    )
    body = resp.json()
    resp.raise_for_status()
    return body["data"]

def get_link(api_key, link_id):
    resp = requests.get(
        f"{BASE_URL}/v1/links/{link_id}",
        headers={"X-API-Key": api_key},
    )
    resp.raise_for_status()
    return resp.json()

def list_links(api_key, page=1, items_per_page=20, search=None):
    payload = {"page": page, "itemsPerPage": items_per_page}
    if search:
        payload["search"] = search
    resp = requests.post(
        f"{BASE_URL}/v1/links/list",
        headers={"X-API-Key": api_key},
        json=payload,
    )
    resp.raise_for_status()
    return resp.json()["data"]

def bulk_create_links(api_key, links):
    resp = requests.post(
        f"{BASE_URL}/v1/links/bulk",
        headers={"X-API-Key": api_key},
        json=links,
    )
    if resp.status_code == 429:
        remaining = resp.headers.get("X-RateLimit-Remaining-Minute")
        raise RuntimeError(f"Rate limited, remaining this minute: {remaining}")
    resp.raise_for_status()
    return resp.json()["data"]

def get_click_analytics(api_key, group_by="link"):
    resp = requests.get(
        f"{BASE_URL}/v1/analytics/clicks",
        headers={"X-API-Key": api_key},
        params={"group_by": group_by},
    )
    resp.raise_for_status()
    return resp.json()["data"]

Handling rate limits and errors

try:
    resp.raise_for_status()
except requests.HTTPError:
    body = resp.json()
    print(body.get("code"), body.get("message"))
    raise