通过 pip 安装
将 SDK 添加到您的 Python 环境。
pip install lix-sdk
Lix.li URL 缩短和链接分析 API 官方 Python SDK。使用 dataclasses、类型提示和干净的关键字参数构建。以 Pythonic 的简洁方式创建短链接、管理分组并跟踪点击。
from lix_sdk import Client client = Client('lix_live_xxx') result = client.links().create('https://example.com') print(result.link.short_url) # https://lix.li/a3b7k2 print(result.usage.remaining) # 42
使用 dataclasses、类型提示和干净的关键字参数构建。按照 Pythonic 设计。
创建、获取、更新、删除和列出短链接。具有关键字参数和可选默认值的完整 CRUD。
将链接组织成组。创建旋转分组进行网页 A/B 测试。具有定型 dataclass 响应的完整 CRUD。
检索帐户个人资料、计划详情和实时使用限制。
每个 API 响应都是 @dataclass 并具有完整的类型提示。干净的 repr、IDE 自动完成和对属性的自然访问。
7 个专用异常类扩展了基础的 LixException。捕获您所需的内容,访问 .data 中的验证错误数据。
传递您自己的 requests.Session 以进行自定义超时、重试或测试。如果未提供则回退到默认会话。
需要 Python 3.8+ 和 requests。使用 pip 安装并立即开始缩短 URL。
将 SDK 添加到您的 Python 环境。
pip install lix-sdk
传入您的 API 密钥。可选提供自定义的 requests.Session 以进行高级控制。
from lix_sdk import Client client = Client('lix_live_xxx') # With custom session import requests session = requests.Session() session.headers.update({'X-Custom-Header': 'value'}) client = Client('lix_live_xxx', session=session)
缩短 URL 并获取带有短链接和使用信息的 LinkShortenResult。
result = client.links().create('https://example.com') print(result.link.short_url) # https://lix.li/a3b7k2 # API links used: 42 / 100 print(result.usage.used, '/', result.usage.limit)
使用自定义别名、UTM 参数、跟踪像素、密码保护和过期日期创建短链接。每次操作都返回定型的 dataclass。
# Create a basic link result = client.links().create('https://example.com') print(result.link.short_url) # With custom alias and UTM result = client.links().create( 'https://example.com', alias='my-link', title='My Page Title', tags=['sale', 'promo'], utm={'utm_source': 'google', 'utm_medium': 'email', 'utm_campaign': 'summer'}, tracking_pixel_ids=[1001, 1002], is_public=True, ) # Password protection + expiration result = client.links().create( 'https://example.com', password='secret123', active_before_datetime='2026-12-31T23:59:59+00:00', ) # Update a link updated = client.links().update(79697, title='New Title') print(updated.link.title)
分页示例:
client.links().list(limit=20, from_id=79500)
返回带有 ResponseMeta 的下 20 个链接,其中包含总数和下一个 URL。跳过两者以使用 API 默认值。
分组有助于按营销活动、团队或项目对链接进行分类。旋转组通过在多个链接之间分配流量来实现 A/B 测试。
# Create a regular group group = client.groups().create('Marketing') print(group.name, group.url) # Create a rotating group for A/B testing group = client.groups().create( 'Landing Pages', description='Rotating landing pages', is_rotate=True, ) # Update group group = client.groups().update( 10, description='Updated description', ) # List with pagination page = client.groups().list(limit=10, from_id=1000) for g in page.groups: print(g.name, g.url) print(page.meta.total)
检索您的个人资料、计划和实时使用限制 — 一切都返回为嵌套的 dataclass 实例并具有完整的属性访问。
profile = client.profile().me() # Account info print(profile.client.name, profile.client.email) print(profile.user.email) # Plan details print(profile.plan.name, profile.plan.end_datetime) # Usage limits print(profile.usages.links.remaining) print(profile.usages.api_links.used) print(profile.usages.mass_links.limit)
id、名称、电子邮件、created_datetime
名称、电子邮件、created_datetime
id、名称、start_datetime、end_datetime
链接、api_links、mass_links — 每个具有 limit、used、remaining
捕获您正好需要的内容。所有异常都扩展自 LixException。ValidationException 将完整的字段级验证错误数据包含在 .data 中。
from lix_sdk import ( ValidationException, NotFoundException, UnauthorizedException, RateLimitException, ServerException, ) try: result = client.links().create('invalid-url') except ValidationException as e: # Field-level validation errors print('Validation:', e.data) except NotFoundException: print('Not found') except UnauthorizedException: # Invalid API key except RateLimitException: # Too many requests — retry after except ServerException: # 5xx server error except LixException as e: # Generic SDK error print('Error:', e)
400 — 无效输入。访问 e.data 以获取字段错误。
401 — 无效的 API 密钥或缺少授权。
404 — 未找到链接或组。
429 — 超出速率限制。
500 — 意外的服务器错误。
网络 / 传输故障。
所有异常都扩展自 LixException,其扩展了 Exception。捕获基类以处理任何 SDK 错误,或定位特定类进行精细处理。
从 PHP、JavaScript、Python 或 Go 使用相同的 Lix.li API,配备为每个生态系统量身定制的 SDK。