# Turso 平台 API

> 使用 Turso 平台 API 以程式化方式管理資料庫、群組、組織與權杖。
> Canonical HTML: https://tursodb.cn/zh-tw/docs/api-reference/introduction/
> Upstream Source (MIT): https://docs.turso.tech/api-reference/introduction
> Language: zh-TW

---
## TypeScript SDK 快速入門

從 JavaScript 或 TypeScript 使用平台 API 最快的方式，就是官方 [`@tursodatabase/api`](https://www.npmjs.com/package/@tursodatabase/api) 用戶端：

```bash
npm install @tursodatabase/api
```

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

const turso = createClient({
  org: process.env.TURSO_ORG!,
  token: process.env.TURSO_PLATFORM_TOKEN!,
});

// Create a database
const db = await turso.databases.create("user-abc123", { group: "default" });

// Generate a scoped auth token for that database
const { jwt } = await turso.databases.createToken("user-abc123");

// Delete it when no longer needed
await turso.databases.delete("user-abc123");
```

如需原始 HTTP 參考文件，請參閱下方的資源端點。

## 常見使用情境

### 每位使用者一個資料庫 / 每個代理一個資料庫（多租戶）

為每位使用者或每個 AI 代理配置專屬資料庫。每個實體都有自己獨立的 SQLite 資料庫，您可以透過 API 建立、限定權杖範圍並予以銷毀。

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

const turso = createClient({
  org: process.env.TURSO_ORG!,
  token: process.env.TURSO_PLATFORM_TOKEN!,
});

// On user signup or agent creation — provision a database
const db = await turso.databases.create(`agent-${agentId}`, { group: "default" });

// Generate a scoped token for that database
const { jwt } = await turso.databases.createToken(`agent-${agentId}`);

// Store db.hostname and jwt for the session
```

資料庫建立後，即可從您的應用程式連線至該資料庫：

* **透過網路連線** — 使用 [`@tursodatabase/serverless`](/zh-tw/docs/sdk/ts/quickstart/) 從遠端查詢資料庫
* **本地優先搭配同步** — 使用 [`@tursodatabase/sync`](/zh-tw/docs/sync/usage/) 將資料庫與本機檔案雙向同步

### 平台 / 經銷商

代表您的終端使用者建立並管理資料庫。使用平台 API 在 Turso 之上打造您自己的資料庫即服務（database-as-a-service）。

```ts
// List all databases in your organization
const databases = await turso.databases.list();

// Get details for a specific database
const db = await turso.databases.get("customer-db");
```

<div class="doc-cards">
<a class="doc-card" href="/zh-tw/docs/api-reference/quickstart/"><div class="doc-card-title"><span>API 快速入門</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">逐步引導您使用 API 建立第一個資料庫。</p></a>
</div>


## API 資源

只要幾個簡單步驟，即可開始將 Turso API 整合至您的平台。

<div class="doc-cards">
<a class="doc-card" href="/api-reference/groups"><div class="doc-card-title"><span>群組 API</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">建立並管理資料庫所在的區位。</p></a>
<a class="doc-card" href="/api-reference/databases"><div class="doc-card-title"><span>資料庫 API</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">建立、管理資料庫並為其產生驗證權杖。</p></a>
<a class="doc-card" href="/api-reference/locations"><div class="doc-card-title"><span>區位 API</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">取得資料庫群組可部署的所有可用區位。</p></a>
<a class="doc-card" href="/api-reference/organizations"><div class="doc-card-title"><span>組織 API</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">管理您的組織、其成員與邀請。</p></a>
<a class="doc-card" href="/api-reference/tokens"><div class="doc-card-title"><span>API 權杖 API</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">建立、驗證與撤銷可用於存取 API 的 API 金鑰。</p></a>
<a class="doc-card" href="/api-reference/audit-logs"><div class="doc-card-title"><span>稽核日誌 API</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">監控組織內所發生事件的日誌。</p></a>
</div>
