@lyng/sdk
v1.0.1
Published
Official JavaScript SDK for the Lyng URL shortener API
Readme
@lyng/sdk
Official JavaScript / TypeScript SDK for the lyng URL shortener API.
Installation
npm install @lyng/sdkRequires Node.js v18 or later. Ships with TypeScript declarations — no @types package needed.
Quick start
import Lyng from '@lyng/sdk'
const lyng = new Lyng({ apiKey: process.env.LYNG_API_KEY! })
const { shortUrl } = await lyng.links.create({ url: 'https://example.com' })
console.log(shortUrl) // https://lyng.my.id/abc123Getting an API key
- Sign up at lyng.my.id
- Go to Dashboard → Developer
- Create a project and click Create key
Your key starts with lk_ and is shown only once — save it somewhere safe.
Store it as an environment variable rather than hardcoding it:
# .env
LYNG_API_KEY=lk_your_key_hereReference
lyng.links.create(options)
Shortens a URL and returns the new short link.
const { shortUrl, slug } = await lyng.links.create({
url: 'https://example.com',
})Options
| Field | Type | Required | Description |
|--------|----------|----------|-------------|
| url | string | Yes | The URL to shorten. Must start with http:// or https://. |
| slug | string | No | Custom slug for the short link (e.g. my-link → lyng.my.id/my-link). Premium accounts only. |
Returns
| Field | Type | Description |
|------------|----------|-------------|
| shortUrl | string | The complete short URL, ready to share. |
| slug | string | The short code at the end of the URL. |
// Custom slug — Premium accounts only
const { shortUrl } = await lyng.links.create({
url: 'https://example.com',
slug: 'my-link',
})
// https://lyng.my.id/my-linklyng.links.list()
Returns your 100 most recently created links, newest first.
const { links } = await lyng.links.list()
for (const link of links) {
console.log(link.shortUrl, link.clicks)
}Returns { links: LyngLink[] }
Each item in the array has the following shape:
| Field | Type | Description |
|---------------|----------|-------------|
| slug | string | The unique short code. |
| shortUrl | string | The full short URL. |
| originalUrl | string | The original long URL. |
| clicks | number | Number of times the link has been visited. |
| createdAt | string | ISO 8601 creation timestamp. |
Error handling
All API errors throw a LyngError with a message string and a status number matching the HTTP status code.
import Lyng, { LyngError } from '@lyng/sdk'
try {
await lyng.links.create({ url: 'https://example.com' })
} catch (err) {
if (err instanceof LyngError) {
console.error(err.status) // 429
console.error(err.message) // "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 (e.g. custom slugs require Premium). |
| 409 | Slug already taken. |
| 429 | Free account link limit (60) reached. |
| 500 | Server error — try again later. |
See the full error code reference for details and fix suggestions.
TypeScript types
All types are exported from the package:
import type { LyngLink, CreateLinkOptions, CreateLinkResult } from '@lyng/sdk'| Type | Description |
|---------------------|-------------|
| LyngLink | A single link object returned by links.list(). |
| CreateLinkOptions | Options accepted by links.create(). |
| CreateLinkResult | Return value of links.create(). |
Links
License
MIT
