稀饭秘钥API

通过简单的API接口,轻松实现文本与短链接之间的转换

curl "https://key.xif.life/api.php" -X POST
-H "Content-Type: application/json"
-H "Authorization: 你的API密钥"
-d '{"text": "长文本内容"}'

API特性

快速响应

毫秒级响应速度,确保您的应用程序高效运行

安全可靠

API密钥认证机制,保障您的数据安全

多语言支持

提供多种编程语言的示例代码,轻松集成

概述

短链接系统API提供了简单而强大的接口,允许您的应用程序生成和管理短链接。通过API,您可以轻松地将长文本转换为简洁的短链接,或通过短链接获取原始文本内容,帮助您简化分享流程并提升用户体验。

API基础信息

基础URL
https://key.xif.life/api.php
请求格式
JSON
认证方式
API密钥
支持方法
GET, POST, OPTIONS

认证方式

所有API请求都需要通过HTTP头信息进行认证,在请求头中包含您的API密钥:

Authorization: 你的API密钥

请联系系统管理员获取您的API密钥,每个密钥都有独立的使用限额和权限控制。

API接口

POST

生成短链接

通过此接口将长文本转换为短链接,可以指定自定义短码(可选)。系统会自动生成唯一的短码,或使用您提供的自定义短码。

请求URL

https://key.xif.life/api.php

请求头

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"
  }
}
GET

获取文本内容

通过此接口使用短码获取原始文本内容。调用此接口将返回与指定短码关联的完整文本信息。

请求URL

https://key.xif.life/api.php?short_code=短码

请求头

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": "错误信息描述,例如:文本不能为空"
}

准备好开始使用了吗?

集成我们的短链接API到您的应用中,提升用户体验并简化分享流程

复制成功!