> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# workspace

> workspace を作成し、メンバーを招待し、ロールを管理し、チーム間でアプリケーションを共有します。

<Info>
  workspace は `api.workspaces` モジュールで公開されます — v4 で追加された機能です。**Standard、Pro、Enterprise** プランで利用できます。
</Info>

## workspace の作成

```typescript theme={null}
const { id: workspaceId } = await api.workspaces.create({ name: "Acme" });
```

`name` は 1〜32 文字である必要があります。

## workspace の一覧表示

`api.workspaces.list()` は、認証されたユーザーがオーナーまたはメンバーであるすべての workspace を返します。

```typescript theme={null}
const workspaces = await api.workspaces.list();

for (const ws of workspaces) {
    console.log(
        `${ws.name} (${ws.memberList.length} members, ${ws.applicationList.length} apps)`,
    );
}
```

<Warning>
  v3 では `ws.members` と `ws.applications` は生のデータ配列を保持していました。v4 ではこれらは **モジュール** (`ws.members.add(...)`、`ws.applications.add(...)`) になりました。生の配列は `ws.memberList` と `ws.applicationList` に移動しました。
</Warning>

## 単一の workspace の取得

```typescript theme={null}
const workspace = await api.workspaces.fetch(workspaceId);

console.log(workspace.id);
console.log(workspace.name);
console.log(workspace.owner);
console.log(workspace.createdAt);
console.log(workspace.memberList);
console.log(workspace.applicationList);
```

## メンバーの招待

メンバーの招待は 2 段階のハンドシェイクです — 招待される側が **自分の** クライアントから短命（5 分間）のコードを生成して同意を証明し、オーナーがそれを消費します。

```typescript theme={null}
// 1. From the invitee's own SDK client
const otherApi = new SquareCloudAPI(process.env.OTHER_USER_API_KEY!);
const code = await otherApi.workspaces.generateInviteCode();

// 2. From the workspace owner's client
await workspace.members.add(code, "maintain");
```

`group` 引数に有効なロール:

| ロール        | 説明                           |
| ---------- | ---------------------------- |
| `view`     | 読み取り専用アクセス                   |
| `manager`  | アプリケーションの管理                  |
| `maintain` | アプリケーションの管理 + 自分より下位のメンバーの管理 |
| `admin`    | 完全な管理者権限                     |

<Note>
  `owner` ロールは API 経由で割り当てることはできません — 所有権の譲渡は公開されていません。
</Note>

## メンバーのロールの変更

```typescript theme={null}
await workspace.members.update("1234567890abcdef1234567890abcdef", "admin");
```

## メンバーの削除

```typescript theme={null}
await workspace.members.remove("1234567890abcdef1234567890abcdef");
```

<Note>
  メンバーを削除できるのは workspace のオーナーのみです。
</Note>

## アプリケーションの共有

```typescript theme={null}
await workspace.applications.add("abc123def456abc123def456");
await workspace.applications.remove("abc123def456abc123def456");
```

## workspace からの退出または削除

```typescript theme={null}
// As a member
await workspace.leave();

// As the owner (irreversible)
await workspace.delete();
```

モジュールから ID を直接指定して削除または退出することもできます。

```typescript theme={null}
await api.workspaces.delete(workspaceId);
await api.workspaces.leave(workspaceId);
```
