通过 npm 安装
将 SDK 添加到您的 Node.js 或浏览器项目。
npm install lix-url
Lix.li URL 缩短 API 官方 JavaScript SDK。在 Node.js 和浏览器环境中运行。使用 async/await 和基于类的干净 DTO 创建短链接、管理分组并跟踪点击。
// npm install lix-url const { Client } = require('lix-url'); const client = new Client('lix_live_xxx'); async function main() { const result = await client.links().create('https://example.com'); console.log(result.link.shortUrl); // https://lix.li/a3b7k2 } main();
基于 async/await、基于类的干净 DTO 和可交换的 fetch 函数构建,适用于 Node.js 或浏览器。
创建、获取、更新、删除和列出短链接。具有定型类响应的完整 CRUD。
将链接组织成组。创建旋转分组进行网页 A/B 测试。
检索帐户个人资料、计划详情和实时使用限制。
每个 API 响应都是一个普通 JavaScript 类实例。没有原始对象 — 具有可预测结构和点记号访问。
6 个专用异常类型用于验证、授权、速率限制、服务器错误和传输故障。
注入任何与 fetch 兼容的函数。可与 Node.js fetch、undici 或浏览器 fetch 配合使用。
在任何支持 fetch 的 Node.js 或浏览器环境中运行。没有重型依赖 — 只有一个干净的 HTTP 客户端层。
将 SDK 添加到您的 Node.js 或浏览器项目。
npm install lix-url
传入您的 API 密钥。可选注入自定义的 fetch 函数以进行测试或边缘运行时。
const { Client } = require('lix-url'); const client = new Client('lix_live_xxx'); // With custom fetch (e.g., for testing) const client = new Client('lix_live_xxx', myFetchFn);
缩短 URL 并获取带有短链接和使用信息的定型 LinkShortenResult。
const result = await client.links().create('https://example.com'); console.log(result.link.shortUrl); // https://lix.li/a3b7k2 // Links used: 42 / 100 console.log(result.usage.used, '/', result.usage.limit);
使用自定义别名、UTM 参数、跟踪像素、密码保护和过期日期创建短链接。每次操作都返回一个定型的类实例。
// Create a basic link const result = await client.links().create('https://example.com'); // With custom alias and UTM const result = await client.links().create( 'https://example.com', 'summer-sale', // alias 'My Summer Sale', // title null, // groupId ['marketing', '2026'], // tags [], // meta { // utm source: 'newsletter', medium: 'email', campaign: 'summer-sale', }, [1, 2], // trackingPixelIds null, // activeBeforeDatetime null, // password true // isPublic ); // Password protection + expiration const result = await client.links().create( 'https://example.com', null, null, null, [], [], [], [], '2026-12-31T23:59:59Z', // activeBeforeDatetime 'secret123' // password );
分页示例: $client->links()->list(limit: 100, fromId: 500) 返回从 ID 500 开始的下 100 个链接,其中 ResponseMeta 包含总数和下一个 URL。
分组有助于按营销活动、团队或项目对链接进行分类。旋转组通过在多个链接之间分配流量来实现 A/B 测试。
// Create a regular group const group = await client.groups().create('Marketing'); console.log(group.name); // Create a rotating group for A/B testing const group = await client.groups().create( 'Landing Pages', null, true // isRotate ); // Update group const group = await client.groups().update( 10, null, 'Updated description' ); // List with pagination const response = await client.groups().list(10, 1000); for (const group of response.groups) { console.log(group.name); }
检索您的个人资料、计划和实时使用限制 — 一切都返回为嵌套的类实例,具有完整的属性访问。
const profile = await client.profile().me(); // Account info console.log(profile.client.name, profile.client.email); console.log(profile.user.email); // Plan details console.log(profile.plan.name, profile.plan.endDatetime); // Usage limits console.log(profile.usages.links.remaining); console.log(profile.usages.apiLinks.used); console.log(profile.usages.massLinks.limit);
组织和帐户元数据。
经过身份验证的用户信息和帐户详情。
当前订阅和计费周期。
API 限制、链接配额和剩余使用量。
捕获您正好需要的内容。所有异常都扩展自 LixException。ValidationException 将完整的字段级验证错误数据包含在 err.data 中。
const { ValidationException, NotFoundException, UnauthorizedException, RateLimitException, ServerException } = require('lix-url'); try { await client.links().create('invalid-url'); } catch (err) { if (err instanceof ValidationException) { // Field-level validation errors console.error('Validation:', err.data); } else if (err instanceof NotFoundException) { console.error('Not found:', err.message); } else if (err instanceof UnauthorizedException) { // Invalid API key } else if (err instanceof RateLimitException) { // Too many requests — retry after } else if (err instanceof ServerException) { // 5xx server error } else { // Generic Lix or network error console.error('Error:', err.message); } }
400 — 无效输入。访问 err.data 以获取字段错误。
401 — 缺少、无效或已过期的 API 凭据。
404 — 未找到链接、组或资源。
429 — 超出速率限制。
500 — 意外的服务器错误。
网络 / 传输故障。
所有异常都扩展自 LixException,其扩展了原生的 Error。捕获基类以处理任何 SDK 错误,或定位特定类以进行精细处理。
从 PHP、JavaScript、Python 或 Go 使用相同的 Lix.li API,配备为每个生态系统量身定制的 SDK。