开发者API
SafeW 提供强大的开发者API,让您能够轻松集成安全通信功能到您的应用中。所有API调用都经过端到端加密,确保数据安全。
快速开始
只需几个简单步骤,即可开始使用 SafeW API 构建您的安全通信应用。
1
获取API密钥
注册开发者账户,创建应用并获取您的唯一API密钥和密钥。所有API请求都需要通过API密钥进行认证。
2
阅读文档
详细了解API端点、请求参数、响应格式和最佳实践。我们提供详细的文档和示例代码。
3
集成到应用
将SafeW API集成到您的Web、移动或桌面应用中。我们提供多种语言的SDK和代码示例。
4
测试和部署
使用沙箱环境测试您的集成,确保一切正常后部署到生产环境。我们提供完整的测试工具。
API端点
SafeW 提供完整的RESTful API,支持消息发送、用户管理、文件传输等功能。
POST
/api/v1/messages/send
发送加密消息到指定的用户或群组。支持文本、图片、文件和位置消息。
请求参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
recipient_id |
string | 是 | 接收者ID,可以是用户ID或群组ID |
message_type |
string | 是 | 消息类型:text, image, file, location |
content |
string/object | 是 | 消息内容,根据消息类型不同而变化 |
encryption_key |
string | 否 | 自定义加密密钥,如果不提供则使用默认密钥 |
self_destruct |
integer | 否 | 自毁时间(秒),消息在读取后自动销毁 |
GET
/api/v1/messages/{message_id}
获取指定的消息详情。只有消息的发送者或接收者可以访问消息内容。
路径参数
| 参数 | 类型 | 描述 |
|---|---|---|
message_id |
string | 消息的唯一标识符 |
POST
/api/v1/users/create
创建新的匿名用户账户。支持多种认证方式,包括API密钥认证和OAuth2.0。
请求参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
username |
string | 否 | 用户名,如果不提供则生成随机用户名 |
public_key |
string | 是 | 用户的公钥,用于端到端加密 |
device_info |
object | 否 | 设备信息,包括设备类型、操作系统等 |
GET
/api/v1/files/upload-url
获取文件上传URL。文件上传使用分片上传,支持大文件上传和断点续传。
查询参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
file_name |
string | 是 | 文件名,包括扩展名 |
file_size |
integer | 是 | 文件大小(字节) |
file_type |
string | 否 | 文件MIME类型 |
代码示例
以下示例展示了如何使用不同编程语言调用 SafeW API。
// 发送加密消息 - JavaScript示例
const axios = require('axios');
async function sendEncryptedMessage() {
const apiKey = 'your_api_key_here';
const apiSecret = 'your_api_secret_here';
const messageData = {
recipient_id: 'user_123456',
message_type: 'text',
content: {
text: '这是一条加密消息',
timestamp: Date.now()
},
encryption_key: 'optional_custom_key',
self_destruct: 3600 // 1小时后自毁
};
try {
const response = await axios.post('https://api.safew.com/api/v1/messages/send',
messageData,
{
headers: {
'Authorization': `Bearer ${apiKey}:${apiSecret}`,
'Content-Type': 'application/json',
'X-API-Version': '1.0'
}
}
);
console.log('消息发送成功:', response.data);
return response.data;
} catch (error) {
console.error('发送消息失败:', error.response?.data || error.message);
throw error;
}
}
// 获取消息详情
async function getMessage(messageId) {
const apiKey = 'your_api_key_here';
const apiSecret = 'your_api_secret_here';
try {
const response = await axios.get(`https://api.safew.com/api/v1/messages/${messageId}`,
{
headers: {
'Authorization': `Bearer ${apiKey}:${apiSecret}`,
'X-API-Version': '1.0'
}
}
);
console.log('消息详情:', response.data);
return response.data;
} catch (error) {
console.error('获取消息失败:', error.response?.data || error.message);
throw error;
}
}
# 发送加密消息 - Python示例
import requests
import json
import time
def send_encrypted_message():
api_key = 'your_api_key_here'
api_secret = 'your_api_secret_here'
url = 'https://api.safew.com/api/v1/messages/send'
message_data = {
'recipient_id': 'user_123456',
'message_type': 'text',
'content': {
'text': '这是一条加密消息',
'timestamp': int(time.time())
},
'encryption_key': 'optional_custom_key',
'self_destruct': 3600 # 1小时后自毁
}
headers = {
'Authorization': f'Bearer {api_key}:{api_secret}',
'Content-Type': 'application/json',
'X-API-Version': '1.0'
}
try:
response = requests.post(url, json=message_data, headers=headers)
response.raise_for_status()
print('消息发送成功:', response.json())
return response.json()
except requests.exceptions.RequestException as e:
print(f'发送消息失败: {e}')
if hasattr(e, 'response') and e.response is not None:
print('错误详情:', e.response.text)
raise
# 创建匿名用户
def create_anonymous_user():
api_key = 'your_api_key_here'
api_secret = 'your_api_secret_here'
url = 'https://api.safew.com/api/v1/users/create'
user_data = {
'username': 'anonymous_user',
'public_key': 'user_public_key_here',
'device_info': {
'device_type': 'web',
'os': 'Windows 10',
'browser': 'Chrome 120'
}
}
headers = {
'Authorization': f'Bearer {api_key}:{api_secret}',
'Content-Type': 'application/json',
'X-API-Version': '1.0'
}
try:
response = requests.post(url, json=user_data, headers=headers)
response.raise_for_status()
print('用户创建成功:', response.json())
return response.json()
except requests.exceptions.RequestException as e:
print(f'创建用户失败: {e}')
if hasattr(e, 'response') and e.response is not None:
print('错误详情:', e.response.text)
raise
// 发送加密消息 - Java示例
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import org.json.JSONObject;
public class SafeWAPIExample {
private static final String API_KEY = "your_api_key_here";
private static final String API_SECRET = "your_api_secret_here";
private static final String BASE_URL = "https://api.safew.com";
public static void sendEncryptedMessage() throws Exception {
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
JSONObject messageData = new JSONObject();
messageData.put("recipient_id", "user_123456");
messageData.put("message_type", "text");
JSONObject content = new JSONObject();
content.put("text", "这是一条加密消息");
content.put("timestamp", System.currentTimeMillis());
messageData.put("content", content);
messageData.put("encryption_key", "optional_custom_key");
messageData.put("self_destruct", 3600);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + "/api/v1/messages/send"))
.header("Authorization", "Bearer " + API_KEY + ":" + API_SECRET)
.header("Content-Type", "application/json")
.header("X-API-Version", "1.0")
.POST(HttpRequest.BodyPublishers.ofString(messageData.toString()))
.build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("消息发送成功: " + response.body());
} else {
System.out.println("发送消息失败: " + response.statusCode());
System.out.println("错误信息: " + response.body());
}
}
public static void getMessage(String messageId) throws Exception {
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + "/api/v1/messages/" + messageId))
.header("Authorization", "Bearer " + API_KEY + ":" + API_SECRET)
.header("X-API-Version", "1.0")
.GET()
.build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("消息详情: " + response.body());
} else {
System.out.println("获取消息失败: " + response.statusCode());
System.out.println("错误信息: " + response.body());
}
}
}
# 发送加密消息 - cURL示例
# 设置API凭证
API_KEY="your_api_key_here"
API_SECRET="your_api_secret_here"
# 发送文本消息
curl -X POST "https://api.safew.com/api/v1/messages/send" \
-H "Authorization: Bearer ${API_KEY}:${API_SECRET}" \
-H "Content-Type: application/json" \
-H "X-API-Version: 1.0" \
-d '{
"recipient_id": "user_123456",
"message_type": "text",
"content": {
"text": "这是一条加密消息",
"timestamp": '"$(date +%s)"'
},
"encryption_key": "optional_custom_key",
"self_destruct": 3600
}'
# 获取消息详情
MESSAGE_ID="msg_abc123"
curl -X GET "https://api.safew.com/api/v1/messages/${MESSAGE_ID}" \
-H "Authorization: Bearer ${API_KEY}:${API_SECRET}" \
-H "X-API-Version: 1.0"
# 创建匿名用户
curl -X POST "https://api.safew.com/api/v1/users/create" \
-H "Authorization: Bearer ${API_KEY}:${API_SECRET}" \
-H "Content-Type: application/json" \
-H "X-API-Version: 1.0" \
-d '{
"username": "anonymous_user",
"public_key": "user_public_key_here",
"device_info": {
"device_type": "web",
"os": "Windows 10",
"browser": "Chrome 120"
}
}'
# 获取文件上传URL
curl -X GET "https://api.safew.com/api/v1/files/upload-url?file_name=document.pdf&file_size=1048576&file_type=application/pdf" \
-H "Authorization: Bearer ${API_KEY}:${API_SECRET}" \
-H "X-API-Version: 1.0"
认证与安全
SafeW API 提供多种认证方式,确保API调用的安全性。所有API请求都需要通过HTTPS进行加密传输。
API密钥认证
使用API密钥和密钥进行认证。每个API请求都需要在Authorization头中包含Bearer令牌。密钥对可以在开发者控制台中生成和管理。
OAuth 2.0
支持OAuth 2.0授权框架,允许第三方应用在获得用户授权后访问SafeW API。支持授权码流程和客户端凭证流程。
端到端加密
所有消息内容在客户端进行端到端加密,只有消息的发送者和接收者可以解密。API服务器无法访问消息的明文内容。