概述
短链接系统API提供了简单而强大的接口,允许您的应用程序生成和管理短链接。通过API,您可以轻松地将长文本转换为简洁的短链接,或通过短链接获取原始文本内容,帮助您简化分享流程并提升用户体验。
API基础信息
认证方式
所有API请求都需要通过HTTP头信息进行认证,在请求头中包含您的API密钥:
请联系系统管理员获取您的API密钥,每个密钥都有独立的使用限额和权限控制。
API接口
生成短链接
通过此接口将长文本转换为短链接,可以指定自定义短码(可选)。系统会自动生成唯一的短码,或使用您提供的自定义短码。
请求URL
请求头
Content-Type: application/json Authorization: 你的API密钥
请求体参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| text | string | 是 | 需要转换的长文本内容 |
| custom_short_code | string | 否 | 自定义短码,必须由6-20位大小写字母和数字组成。 如果不提供,系统将自动生成。 |
请求体示例
{
"text": "这是一段需要转换为短链接的长文本内容...",
"custom_short_code": "mycustomcode123"
}
成功响应 (200)
{
"status": "success",
"code": 200,
"data": {
"short_code": "abc123",
"short_url": "https://key.xif.life/abc123",
"created_at": "2025-07-15 14:30:45"
}
}
获取文本内容
通过此接口使用短码获取原始文本内容。调用此接口将返回与指定短码关联的完整文本信息。
请求URL
请求头
Authorization: 你的API密钥
查询参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| short_code | string | 是 | 短链接的短码部分 |
成功响应 (200)
{
"status": "success",
"code": 200,
"data": {
"short_code": "abc123",
"text": "这是一段需要转换为短链接的长文本内容...",
"accessed_at": "2025-07-15 15:20:30"
}
}
代码示例
以下是使用不同编程语言调用API的示例代码,您可以根据自己的开发环境选择合适的示例,快速集成到您的项目中。
生成短链接
const apiUrl = 'https://key.xif.life/api.php';
const apiKey = '你的API密钥';
// 生成短链接
async function createShortLink(text, customCode = '') {
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': apiKey
},
body: JSON.stringify({
text: text,
custom_short_code: customCode
})
});
const data = await response.json();
if (data.status === 'success') {
console.log('生成成功:', data.data.short_url);
return data.data;
} else {
console.error('生成失败:', data.message);
throw new Error(data.message);
}
} catch (error) {
console.error('请求错误:', error);
throw error;
}
}
获取文本内容
const apiUrl = 'https://key.xif.life/api.php';
const apiKey = '你的API密钥';
// 获取文本内容
async function getTextByShortCode(shortCode) {
try {
const url = new URL(apiUrl);
url.searchParams.append('short_code', shortCode);
const response = await fetch(url.toString(), {
method: 'GET',
headers: {
'Authorization': apiKey
}
});
const data = await response.json();
if (data.status === 'success') {
console.log('获取成功:', data.data.text);
return data.data.text;
} else {
console.error('获取失败:', data.message);
throw new Error(data.message);
}
} catch (error) {
console.error('请求错误:', error);
throw error;
}
}
错误处理
API会返回适当的错误代码和信息,帮助您诊断问题。以下是常见的错误情况及其解决方法,以便您快速排查问题。
| 状态码 | 描述 | 可能的原因 | 解决方法 |
|---|---|---|---|
| 401 | 未授权 | API密钥缺失或无效 | 检查API密钥是否正确,确保请求头包含密钥 |
| 400 | 请求错误 | 缺少必要参数、参数格式错误或无效的请求数据 | 检查请求参数是否完整且格式正确 |
| 404 | 未找到 | 请求的短码不存在 | 确认短码是否正确,或先创建短链接 |
| 405 | 方法不允许 | 使用了不支持的HTTP方法 | 检查HTTP方法是否为GET或POST |
| 500 | 服务器错误 | 服务器端发生错误 | 联系管理员并提供错误详情 |
错误响应格式
{
"status": "error",
"code": 400,
"message": "错误信息描述,例如:文本不能为空"
}