使用 API 管理您的组织
LangSmith 的 API 支持通过 API 密钥以编程方式访问 UI 中提供的所有操作,只有少数例外情况在下面注明。
推荐阅读
在深入阅读本文档之前,阅读以下内容可能会有所帮助
注意
有一些限制将很快解除
- LangSmith SDK 尚不支持这些组织管理操作。
- 服务密钥尚无法访问新添加的工作区(我们即将添加支持)。我们建议目前使用组织管理员的 PAT,默认情况下,该 PAT 拥有这些操作所需的权限。
下面列出了一些常用的端点和用例。有关可用端点的完整列表,请参阅 API 文档。
所有请求都应包含 X-Organization-Id
标头,而作用域限定为特定工作区的请求应包含 X-Tenant-Id
标头。
工作区
用户管理
RBAC
成员管理
应使用 RBAC 下的 List roles
来检索这些操作的角色 ID。
应在这些操作中使用 List [organization|workspace] members
端点(如下)响应中的 "id"
作为 identity_id
。
组织级别
- 列出组织成员
- 邀请用户加入组织以及一个或多个工作区 。当用户还不是组织成员时,应使用此方法。
- 更新用户的组织角色
- 从组织中移除某人
工作区级别
注意
应省略以下参数:read_only
(已弃用)、password
和 full_name
(仅限 基本身份验证)
API 密钥
注意
使用 X-Tenant-Id
标头指定要定位的工作区。
如果未提供标头,操作将默认为最初创建 API 密钥的工作区。
安全设置
-
更新组织共享设置
- 使用
unshare_all
取消共享组织中的所有共享资源 - 使用disable_public_sharing
阻止未来共享资源
- 使用
仅用户端点
这些端点是用户作用域的,需要已登录用户的 JWT,因此应仅通过 UI 执行。
/api-key/current
端点:这些端点与用户的 PAT 相关/sso/email-verification/send
(仅限云端):此端点与 SAML SSO 相关
示例代码
下面的示例代码介绍了与组织管理相关的一些常见工作流程。
请务必在代码中 <replace_me>
出现的地方进行必要的替换。
import os
import requests
def main():
api_key = os.environ["LANGSMITH_API_KEY"]
# LANGSMITH_ORGANIZATION_ID is not a standard environment variable in the SDK, just used for this example
organization_id = os.environ["LANGSMITH_ORGANIZATION_ID"]
base_url = os.environ.get("LANGSMITH_ENDPOINT") or "https://api.smith.langchain.com"
headers = {
"Content-Type": "application/json",
"X-API-Key": api_key,
"X-Organization-Id": organization_id,
}
session = requests.Session()
session.headers.update(headers)
workspaces_path = f"{base_url}/api/v1/workspaces"
orgs_path = f"{base_url}/api/v1/orgs/current"
api_keys_path = f"{base_url}/api/v1/api-key"
# Create a workspace
workspace_res = session.post(workspaces_path, json={"display_name": "My Workspace"})
workspace_res.raise_for_status()
workspace = workspace_res.json()
workspace_id = workspace["id"]
new_workspace_headers = {
"X-Tenant-Id": workspace_id,
}
# Grab roles - this includes both organization and workspace roles
roles_res = session.get(f"{orgs_path}/roles")
roles_res.raise_for_status()
roles = roles_res.json()
# system org roles are 'Organization Admin', 'Organization User'
# system workspace roles are 'Admin', 'Editor', 'Viewer'
org_roles_by_name = {role["display_name"]: role for role in roles if role["access_scope"] == "organization"}
ws_roles_by_name = {role["display_name"]: role for role in roles if role["access_scope"] == "workspace"}
# Invite a user to the org and the new workspace, as an Editor.
# workspace_role_id is only allowed if RBAC is enabled (an enterprise feature).
new_user_email = "<replace_me>"
new_user_res = session.post(
f"{orgs_path}/members",
json={
"email": new_user_email,
"role_id": org_roles_by_name["Organization User"]["id"],
"workspace_ids": [workspace_id],
"workspace_role_id": ws_roles_by_name["Editor"]["id"],
},
)
new_user_res.raise_for_status()
# Add a user that already exists in the org to the new workspace, as a Viewer.
# workspace_role_id is only allowed if RBAC is enabled (an enterprise feature).
existing_user_email = "<replace_me>"
org_members_res = session.get(f"{orgs_path}/members")
org_members_res.raise_for_status()
org_members = org_members_res.json()
existing_org_member = next(
(member for member in org_members["members"] if member["email"] == existing_user_email), None
)
existing_user_res = session.post(
f"{workspaces_path}/current/members",
json={
"user_id": existing_org_member["user_id"],
"workspace_ids": [workspace_id],
"workspace_role_id": ws_roles_by_name["Viewer"]["id"],
},
headers=new_workspace_headers,
)
existing_user_res.raise_for_status()
# List all members of the workspace
members_res = session.get(f"{workspaces_path}/current/members", headers=new_workspace_headers)
members_res.raise_for_status()
members = members_res.json()
workspace_member = next(
(member for member in members["members"] if member["email"] == existing_user_email), None
)
# Update the user's workspace role to Admin (enterprise-only)
existing_user_id = workspace_member["id"]
update_res = session.patch(
f"{workspaces_path}/current/members/{existing_user_id}",
json={"role_id": ws_roles_by_name["Admin"]["id"]},
headers=new_workspace_headers,
)
update_res.raise_for_status()
# Update the user's organization role to Organization Admin
update_res = session.patch(
f"{orgs_path}/members/{existing_org_member['id']}",
json={"role_id": org_roles_by_name["Organization Admin"]["id"]},
)
update_res.raise_for_status()
# Create a new Service key
api_key_res = session.post(
api_keys_path,
json={"description": "my key"},
headers=new_workspace_headers,
)
api_key_res.raise_for_status()
api_key_json = api_key_res.json()
api_key = api_key_json["key"]
if __name__ == "__main__":
main()