Official TypeScript SDK with full type definitions. For use in Node.js and server environments.
@lyng/sdk1.0.1MITInstall the package from npm. It ships with TypeScript declarations, so no @types package needed.
npm install @lyng/sdkCreate a client with your API key. Always store the key in an environment variable rather than hardcoding it in your source code. The SDK is for server-side use only since your API key must never be exposed to the browser.
import Lyng from '@lyng/sdk'
// Store your key in an environment variable
const lyng = new Lyng({ apiKey: process.env.LYNG_API_KEY })Pass url and optionally a slug (Premium only). Returns the new shortUrl and slug.
const { shortUrl, slug } = await lyng.links.create({
url: 'https://example.com',
})
console.log(shortUrl) // https://lyng.my.id/abc123
// Custom slug (Premium only)
await lyng.links.create({
url: 'https://example.com',
slug: 'my-link',
})Returns your 100 most recently created links, newest first. Each item has slug, shortUrl, originalUrl, clicks, and createdAt.
const { links } = await lyng.links.list()
for (const link of links) {
console.log(link.shortUrl) // https://lyng.my.id/abc123
console.log(link.clicks) // 42
console.log(link.originalUrl) // https://example.com
}All API errors throw a LyngError with a message and status property 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.message) // "Link limit reached."
console.error(err.status) // 429
}
}