Turso Cloud API 参考 接口资源 API 令牌
创建 API 令牌
返回一个属于用户的新 API 令牌。
POST
https://api.turso.tech/v1/auth/api-tokens/{tokenName} 令牌可以按三种限制级别创建,限制范围依次收窄:
- 组织级(Organization-scoped) — 传入
organization,并可选传入scopes。该令牌只能操作该组织内的资源。配合scopes时,它只能在组织的每一个分组中执行所列出的操作。 - 分组级(Group-scoped) — 传入
organization、group和scopes。该令牌被限定在组织内的单个分组上,且只允许执行scopes中列出的操作。调用方必须是该组织的管理员或所有者。 - 无限制(Unrestricted) (已弃用) — 不传请求体。该令牌可以操作调用方所属的每一个组织。无限制令牌已弃用,将在未来版本中移除。 新令牌务必传入
organization,并将现有的无限制令牌轮换为限定范围的令牌。
分组级令牌专为自动化场景设计:这类自动化应能在单个分组内创建和管理数据库,而无法触及组织的其他部分。
警告
响应中的 token 之后不会再被展示。请将其存放在安全的地方,切勿分享或提交到源代码管理中。
警告
无限制(跨组织)令牌已弃用,将在未来版本中移除。 请求体中务必至少包含 organization。
curl -L -X POST https://api.turso.tech/v1/auth/api-tokens/{tokenName} \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
-d '{"organization": "my-org"}'
curl -L -X POST https://api.turso.tech/v1/auth/api-tokens/{tokenName} \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"organization": "my-org",
"scopes": ["org:read"]
}'
curl -L -X POST https://api.turso.tech/v1/auth/api-tokens/{tokenName} \
-H 'Authorization: Bearer TOKEN'
curl -L -X POST https://api.turso.tech/v1/auth/api-tokens/{tokenName} \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"organization": "my-org",
"group": "default",
"scopes": ["read-only"]
}'
curl -L -X POST https://api.turso.tech/v1/auth/api-tokens/{tokenName} \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"organization": "my-org",
"group": "default",
"scopes": ["db:create", "db:configure", "db:mint-token"]
}'
import { createClient } from "@tursodatabase/api";
const turso = createClient({
org: "...",
token: "",
});
const apiToken = await turso.apiTokens.create("my-token");
OpenAPI
openapi: 3.0.1
info:
title: Turso Platform API
description: API description here
license:
name: MIT
version: 0.1.0
servers:
- url: https://api.turso.tech
description: Turso's Platform API
security: []
paths:
/v1/auth/api-tokens/{tokenName}:
post:
summary: Create API Token
description: >-
Returns a new API token belonging to a user.
The token can be minted at three levels of restriction, in increasing
order of narrowness:
- **Organization-scoped** — pass `organization`, and optionally
`scopes`. The token can only act on resources inside that organization.
With `scopes`, it can do only the listed operations, in every group of
the organization.
- **Group-scoped** — pass `organization`, `group`, and `scopes`. The
token is pinned to a single group inside the organization and only the
operations listed in `scopes` are allowed. The caller must be an admin
or owner of the organization.
- **Unrestricted** *(deprecated)* — no request body. The token can act
on every organization the caller belongs to. **Unrestricted tokens are
deprecated and will be removed in a future release.** Always pass
`organization` for new tokens and rotate existing unrestricted tokens to
scoped tokens.
Group-scoped tokens are designed for automations that should be able to
provision and manage databases inside a single group without being able
to touch the rest of the organization.
operationId: createAPIToken
parameters:
- $ref: '#/components/parameters/tokenName'
requestBody:
description: >-
Optional restriction for the token. Omit the body for an unrestricted
token, pass `organization` (optionally with `scopes`) for an
org-scoped token, or pass `organization` + `group` + `scopes` for a
group-scoped token.
required: false
content:
application/json:
schema:
type: object
properties:
organization:
type: string
description: >-
The organization slug to restrict this token to. Required
when `group` is set.
example: my-org
group:
type: string
description: >-
The group name (inside `organization`) to restrict this
token to. Requires `organization` and a non-empty `scopes`
list.
example: default
scopes:
type: array
items:
type: string
enum:
- read
- db:create
- db:delete
- db:configure
- db:mint-token
- db:rotate-creds
- group:configure
- group:mint-token
- group:rotate-creds
- group:create
- group:delete
- org:read
- read-only
- full-access
description: >-
Permissions to grant the token. Each entry is either an
individual scope or one of the presets `read-only` (expands
to `read`) and `full-access` (expands to every group scope,
without `group:create`, `group:delete`, and `org:read`).
Required and must be non-empty when `group` is set. Optional
for an org-scoped token: without `scopes`, the token can do
everything that its user can do in the organization.
`group:create`, `group:delete`, and `org:read` are valid
only on org-scoped tokens. `db:mint-token` lets the token
issue new SQL credentials; `db:rotate-creds` invalidates
every existing SQL token for the database — they are
deliberately separate because rotation is destructive.
example:
- db:create
- db:configure
- db:mint-token
responses:
'200':
description: Successful response
content:
application/json:
schema:
properties:
name: b0a0a4ff-22dd-479f-9f31-1705bc92d0c3
id: 41a60187-13e0-4ce1-86ab-2dc2ed7fbf91
token:
type: string
description: >-
The actual token contents as a JWT. This is used with the
`Bearer` header, see [Authentication](/authentication) for
more details. **This token is never revealed again.**
example: ...
components:
parameters:
tokenName:
name: tokenName
in: path
required: true
schema:
type: string
description: The name of the api token.
同步自官方 Turso 文档仓库 (
tursodatabase/turso-docs
· MIT 开源协议)
查看官方原文 ↗