堡垒平台类型SDK
官方TypeScript SDK用于与Forte平台API互动.
安装
npm install @forteplatforms/sdk
认证
SDK支持三种认证模式,视其运行地点而定.
福尔特托管服务(无配置)
FORTE_API_TOKEN 自动设置和范围为服务项目:
const client = new ForteClient();
客户端( 浏览器/ 移动)
请求 client.users.*,构造时没有参数。 浏览器发送 Forte-User-Session-Token 用户登录后自动 cookie 。 SDK套装 credentials: 'include' 所以饼干到达API的交叉原生地.
const client = new ForteClient();
await client.users.renewSessionToken({ projectId });
执行 没有 通过 apiToken 从浏览器代码——这是服务器侧的秘密.
服务器侧 BFF 呼叫 client.users.* (无代号,每召授权)
在代理用户扫描的电话的BFF中,省略了符号并转发每个用户的会话符号作为 authorization 每次调用时的参数 :
const client = new ForteClient();
await client.users.renewSessionToken({
projectId,
authorization: `Bearer ${userSessionToken}`,
});
外部服务器端呼叫 client.projects.* (显式)
const client = new ForteClient({ apiToken: 'your_api_token_here' });
或者设定 FORTE_API_TOKEN (只有节点)和呼叫 new ForteClient()。 。 。 .
您可以从Forte平台仪表板生成 API 令牌.
快速启动
import { ForteClient } from '@forteplatforms/sdk';
const client = new ForteClient();
// List your projects
const projects = await client.projects.listProjects();
// Get a specific project
const project = await client.projects.getProject({ projectId: 'your-project-id' });
处理错误
API 出错作为 HTTP 状态信息的例外 :
try {
const project = await client.projects.getProject({ projectId: 'invalid-id' });
} catch (error) {
console.error('API error:', error);
}
用户自定义属性
在您的用户中存储任意的密钥值元数据 。 用于跟踪订阅级别,特征标志,首选项,或任何应用程序专用数据.
// Set custom attributes on a user
const user = await client.projects.putUserCustomAttributes({
projectId: 'your-project-id',
userId: 'user-id',
requestBody: {
plan: 'pro',
referral_source: 'google',
onboarding_completed: 'true',
},
});
console.log(user.customMetadataAttributes);
// { plan: 'pro', referral_source: 'google', onboarding_completed: 'true' }
主要制约因素:
- 密钥必须是1-64个字符:字母、数字、下划线和连字符
- 值为字符串
- 每次调用都会取代所有现有的属性——包括任何想要保存的属性
与现有属性合并
// Read current attributes, then merge
const user = await client.projects.getProjectUser({
projectId: 'your-project-id',
userId: 'user-id',
});
await client.projects.putUserCustomAttributes({
projectId: 'your-project-id',
userId: 'user-id',
requestBody: {
...user.customMetadataAttributes,
plan: 'enterprise', // update one field
},
});
API 参考
client.projects
管理Forte平台上的项目和服务.
client.users
管理项目中的最终用户.