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

# APIを使用してプログラムからオートメーションを構築する

> Teable API と JavaScript を使用してワークフローを作成・設定します

<Tip>オートメーションの例は、AIチャットで直接構築することを推奨します。目的のワークフローを説明するだけで、トリガー、アクション、スクリプト、設定を含むすべての構築をAIが行います。</Tip>

## AIで構築する

テーブル右側のサイドバーでAIチャットを開き、実行したい内容を伝えます。たとえば、次のように入力します。

*「注文ステータスの変更を監視し、注文が発送されたらWebhook通知を送るオートメーションを作成してください」*

AIがワークフロー全体を自動作成します。生成されたスクリプトを確認し、実際のデータでテストして、準備ができたらワークフローを有効にします。

## 前提条件

* 個人アクセストークン（PAT）。[アクセストークン](/ja/api-doc/token)を参照してください。
* ベースID、テーブルID、フィールドID。[IDを取得する](/ja/api-doc/get-id)を参照してください。

## ヘルパー関数

```javascript theme={null}
const baseUrl = "https://app.teable.ai/api";
const token = process.env.TEABLE_TOKEN;
const baseId = "bserxxxxxx";

const headers = {
  Authorization: `Bearer ${token}`,
  "Content-Type": "application/json",
};

async function api(method, path, body) {
  const res = await fetch(`${baseUrl}${path}`, {
    method,
    headers,
    body: body ? JSON.stringify(body) : undefined,
  });
  if (!res.ok) throw new Error(`${method} ${path} → ${res.status}`);
  return res.json();
}
```

## ワークフローを作成する

```javascript theme={null}
// 1. ワークフローを作成
const wf = await api("POST", `/base/${baseId}/workflow`, {
  name: "発送時に通知",
  description: "注文ステータスが発送済みになったらWebhookを送信",
});

// 2. トリガーを追加
const trigger = await api("POST", `/base/${baseId}/workflow/${wf.id}/trigger`, {
  type: "recordUpdated",
  config: { tableId: "tblOrders", watchFieldIds: ["fldStatus"] },
});

// 3. トリガーをテスト
await api("POST", `/base/${baseId}/workflow/${wf.id}/test/${trigger.id}`);

// 4. HTTP リクエストアクションを追加
const action = await api("POST", `/base/${baseId}/workflow/${wf.id}/action`, {
  type: "httpRequest",
  parentNodeId: trigger.id,
});

// 5. 有効化
await api("PUT", `/base/${baseId}/workflow/${wf.id}/active`, {
  method: "activate",
});
```

## 推奨事項

* 必要最小限の権限を持つ PAT を使用し、必要なスペース／ベースだけに制限します。
* トークンは環境変数に保存し、コードへ直接記述しないでください。
* 有効化する前に**ノードをテスト**で確認します。
* 詳細は[オートメーションAPIリファレンス](/ja/api-reference/automation/put-base-workflow-action)を参照してください。
