# 建立 API 權杖

> 傳回屬於某位使用者的新 API 權杖。
> Canonical HTML: https://tursodb.cn/zh-tw/docs/api-reference/tokens/create/
> Upstream Source (MIT): https://docs.turso.tech/api-reference/tokens/create
> Language: zh-TW

---
權杖可以在三個層級的限制下簽發，限制範圍由寬到窄依序為：

- **組織範圍（Organization-scoped）** — 傳入 `organization`，並可選擇性傳入 `scopes`。此權杖只能作用於該組織內的資源。若提供 `scopes`，它就只能在該組織的每個群組中執行列出的操作。
- **群組範圍（Group-scoped）** — 傳入 `organization`、`group` 與 `scopes`。此權杖會被固定在組織內的單一群組，且只允許 `scopes` 中列出的操作。呼叫者必須是該組織的管理員或擁有者。
- **無限制（Unrestricted）** *（已棄用）* — 不需要請求內容。此權杖可以作用於呼叫者所屬的每一個組織。**無限制權杖已棄用，將於未來版本中移除。** 新權杖請一律傳入 `organization`，並將現有的無限制權杖輪換為具範圍限制的權杖。

群組範圍權杖是為自動化流程設計的，讓它能在一整個群組內佈建與管理資料庫，而不會碰觸組織的其他部分。

<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

回應中的 `token` 之後不會再顯示。請將其存放在安全的地方，切勿分享或提交至版本控制系統。

</div></div>


<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

**無限制（跨組織）權杖已棄用，將於未來版本中移除。** 請在請求內容中至少包含 `organization`。

</div></div>


```bash cURL (org-scoped)
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"}'
```

```bash cURL (org-scoped, with scopes)
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"]
  }'
```

```bash cURL (deprecated, unrestricted)
curl -L -X POST https://api.turso.tech/v1/auth/api-tokens/{tokenName} \
  -H 'Authorization: Bearer TOKEN'
```

```bash cURL (group-scoped, presets)
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"]
  }'
```

```bash cURL (group-scoped, fine-grained)
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"]
  }'
```

```ts Node.js
import { createClient } from "@tursodatabase/api";

const turso = createClient({
  org: "...",
  token: "",
});

const apiToken = await turso.apiTokens.create("my-token");
```


## OpenAPI

````yaml POST /v1/auth/api-tokens/{tokenName}
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: >-
        傳回屬於某位使用者的新 API 權杖。


        權杖可以在三個層級的限制下簽發，限制範圍由寬到窄依序為：


        - **組織範圍（Organization-scoped）** — 傳入 `organization`，並可選擇性
        傳入 `scopes`。此權杖只能作用於該組織內的資源。若提供 `scopes`，它就只能在
        該組織的每個群組中執行列出的操作。

        - **群組範圍（Group-scoped）** — 傳入 `organization`、`group` 與
        `scopes`。此權杖會被固定在組織內的單一群組，且只允許 `scopes` 中列出的
        操作。呼叫者必須是該組織的管理員或擁有者。

        - **無限制（Unrestricted）** *（已棄用）* — 不需要請求內容。此權杖可以作用
        於呼叫者所屬的每一個組織。**無限制權杖已棄用，將於未來版本中移除。** 新權杖
        請一律傳入 `organization`，並將現有的無限制權杖輪換為具範圍限制的權杖。


        群組範圍權杖是為自動化流程設計的，讓它能在一整個群組內佈建與管理資料庫，而不會
        碰觸組織的其他部分。
      operationId: createAPIToken
      parameters:
        - $ref: '#/components/parameters/tokenName'
      requestBody:
        description: >-
          權杖的選擇性限制。若要建立無限制權杖，請省略請求內容；若要建立組織範圍權杖，
          請傳入 `organization`（可選擇性搭配 `scopes`）；若要建立群組範圍權杖，請傳入
          `organization` + `group` + `scopes`。
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                organization:
                  type: string
                  description: >-
                    用以限制此權杖的組織 slug。設定 `group` 時為必填。
                  example: my-org
                group:
                  type: string
                  description: >-
                    用以限制此權杖的群組名稱（位於 `organization` 之內）。需要
                    `organization` 以及非空的 `scopes` 清單。
                  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: >-
                    要授予權杖的權限。每個項目可以是個別範圍，或是預設組合之一：
                    `read-only`（展開為 `read`）與 `full-access`（展開為所有群組
                    範圍，但不含 `group:create`、`group:delete` 與 `org:read`）。設定
                    `group` 時為必填且必須非空。對組織範圍權杖而言則為選填：若不提供
                    `scopes`，權杖即可執行其使用者在該組織中能做的所有事情。
                    `group:create`、`group:delete` 與 `org:read` 僅在組織範圍權杖上
                    有效。`db:mint-token` 讓權杖可簽發新的 SQL 憑證；`db:rotate-creds`
                    會使該資料庫所有既有的 SQL 權杖失效——兩者刻意分開，因為輪換具有
                    破壞性。
                  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: >-
                      權杖實際內容（JWT）。此內容搭配 `Bearer` 標頭使用，詳情請參閱
                      [Authentication](/authentication)。**此權杖之後不會再顯示。**
                    example: ...
components:
  parameters:
    tokenName:
      name: tokenName
      in: path
      required: true
      schema:
        type: string
      description: API 權杖的名稱。

````
