Official Python SDK for the lyng URL shortener API
Project description
lyng-sdk
Official Python SDK for the lyng URL shortener API.
Installation
pip install lyng-sdk
Requires Python 3.10 or later.
Quick start
from lyng import Lyng
lyng = Lyng(api_key="lk_your_key")
link = lyng.links.create(url="https://example.com")
print(link.short_url) # https://lyng.my.id/abc123
Getting an API key
- Sign up at lyng.my.id
- Go to Dashboard → Developer
- Create a project and click Create key
Store your key in an environment variable rather than hardcoding it:
import os
from lyng import Lyng
lyng = Lyng(api_key=os.environ["LYNG_API_KEY"])
Reference
lyng.links.create(url, slug=None)
Shortens a URL and returns the new short link.
link = lyng.links.create(url="https://example.com")
print(link.short_url) # https://lyng.my.id/abc123
print(link.slug) # abc123
# Custom slug — Premium accounts only
link = lyng.links.create(url="https://example.com", slug="my-link")
Returns CreatedLink
| Field | Type | Description |
|---|---|---|
short_url |
str |
The complete short URL, ready to share. |
slug |
str |
The short code at the end of the URL. |
lyng.links.list()
Returns your 100 most recently created links, newest first.
links = lyng.links.list()
for link in links:
print(link.short_url, link.clicks)
Returns list[Link]
| Field | Type | Description |
|---|---|---|
slug |
str |
The unique short code. |
short_url |
str |
The full short URL. |
original_url |
str |
The original long URL. |
clicks |
int |
Number of times the link was visited. |
created_at |
str |
ISO 8601 creation timestamp. |
Async support
Use AsyncLyng for async/await code (FastAPI, asyncio, etc.):
import asyncio
from lyng import AsyncLyng
async def main():
async with AsyncLyng(api_key="lk_your_key") as lyng:
link = await lyng.links.create(url="https://example.com")
print(link.short_url)
asyncio.run(main())
Context managers
Both clients support context managers to ensure the HTTP connection is closed properly:
# Sync
with Lyng(api_key="lk_your_key") as lyng:
link = lyng.links.create(url="https://example.com")
# Async
async with AsyncLyng(api_key="lk_your_key") as lyng:
link = await lyng.links.create(url="https://example.com")
Error handling
All API errors raise LyngError with a status code and a message.
from lyng import Lyng, LyngError
lyng = Lyng(api_key="lk_your_key")
try:
link = lyng.links.create(url="https://example.com")
except LyngError as e:
print(e.status) # 429
print(str(e)) # "Link limit reached."
Common status codes
| Status | Meaning |
|---|---|
400 |
Bad request — missing or invalid fields. |
401 |
Invalid or missing API key. |
403 |
Feature not available on your plan. |
409 |
Slug already taken. |
429 |
Free account link limit (60) reached. |
500 |
Server error — try again later. |
Links
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lyng_sdk-1.0.0.tar.gz.
File metadata
- Download URL: lyng_sdk-1.0.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Hatch/1.16.5 cpython/3.13.3 HTTPX/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8391f790fa99e378e72cd1638e7efa008dc0a21477b9f7fa18ca3466c5eee52b
|
|
| MD5 |
34e4c0f7eed93e33645c9e04884379d2
|
|
| BLAKE2b-256 |
d4ce47667df4eb0aa7aa7a8c16a6f10129e170d02db3103e99e9402cd72cd79c
|
File details
Details for the file lyng_sdk-1.0.0-py3-none-any.whl.
File metadata
- Download URL: lyng_sdk-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Hatch/1.16.5 cpython/3.13.3 HTTPX/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
941194d023a2c30ad4fb0eb1b6a50b22007e38b8cbd99f1a7bc908a28e55a210
|
|
| MD5 |
50d48ed19a95ef42b1d7f86617e6b304
|
|
| BLAKE2b-256 |
5d0e30f29c0190872ef058a48d09ec0d253653c5c78ef0c85d49166ab9028ed4
|