接入教程
1. 在Stytch官网注册并获取API密钥
2. 集成SDK到您的应用程序中
3. 配置身份验证方法(如密码、OAuth)
4. 调用API管理用户会话和数据
5. 测试和部署您的应用
使用Python发送短信验证码
python
import requests
url = 'https://api.stytch.com/v1/otps/sms/send'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
}
data = {
'phone_number': '+8613800138000',
'expiration_minutes': 10
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
使用PHP验证一次性密码
php
<?php
$url = 'https://api.stytch.com/v1/otps/authenticate';
$headers = [
'Content-Type: application/json',
'Authorization: Bearer YOUR_API_KEY'
];
$data = [
'method_id' => 'phone-number',
'code' => '123456'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
使用JavaScript获取用户信息
javascript
fetch('https://api.stytch.com/v1/users/user_id_placeholder', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
常见问题
如何获取API密钥?
登录Stytch控制台,在设置或API密钥部分生成新的密钥。请妥善保管,不要泄露。
API有速率限制吗?
是的,根据订阅计划有不同的速率限制。请参考官方文档了解具体限制。
支持哪些身份验证方式?
支持短信/邮件OTP、OAuth、Magic Links、WebAuthn等多种现代身份验证方法。
Aitishiku.com