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

# JavaScript SDK

> Profound API 向けの公式 JavaScript および TypeScript SDK をインストールして使用します

JavaScript SDK は npm で [@profoundai/client](https://www.npmjs.com/package/@profoundai/client) として公開されています。TypeScript の型定義が同梱されています。

## インストール

<CodeGroup>
  ```bash npm wrap theme={null}
  npm install @profoundai/client
  ```

  ```bash yarn wrap theme={null}
  yarn add @profoundai/client
  ```

  ```bash pnpm wrap theme={null}
  pnpm add @profoundai/client
  ```

  ```bash bun wrap theme={null}
  bun add @profoundai/client
  ```
</CodeGroup>

## クイックスタート

```typescript Create a client and run a report expandable wrap theme={null}
import Profound from '@profoundai/client';

const client = new Profound({
  apiKey: "your-api-key-here" // or set PROFOUND_API_KEY env var
});

// Get organization categories
const categories = await client.organizations.categories.list();
console.log(categories);

// Generate a report
const report = await client.reports.visibility({
  category_id: categories[0].id,
  start_date: "2024-01-01",
  end_date: "2024-01-31",
  dimensions: ["asset_name"],
  metrics: ["visibility_score"]
});
console.log(report);
```

## 認証

SDK は、次のいずれかから API キーを読み込みます：

1. **環境変数**（推奨）：環境に `PROFOUND_API_KEY` を設定します。
2. **コンストラクターのパラメーター**：クライアントの初期化時に `apiKey` を渡します。

```bash Set the environment variable wrap theme={null}
export PROFOUND_API_KEY="your-api-key-here"
```

API キーの取得方法について詳しくは、[認証](/ja-jp/rest-api/authentication) を参照してください。

## エラー処理

```typescript Catch API errors expandable wrap theme={null}
import Profound from '@profoundai/client';

const client = new Profound();

try {
  const report = await client.reports.visibility({
    category_id: 'invalid-id',
    start_date: '2024-01-01',
    end_date: '2024-01-31',
    dimensions: ['asset_name'],
    metrics: ['visibility_score']
  });
} catch (error) {
  if (error instanceof Profound.APIError) {
    console.error(`API Error: ${error.status} - ${error.message}`);
  }
}
```

## レート制限

API のデフォルトの制限は、API キーごとに 1 時間あたり 600 リクエストです。この制限を超えると、`429 Too Many Requests` エラーが返されます。SDK はエラーをスローするので、それをキャッチして処理できます。

```typescript Handle rate limits expandable wrap theme={null}
import Profound from '@profoundai/client';

try {
  const report = await client.reports.visibility(...);
} catch (error) {
  if (error instanceof Profound.RateLimitError) {
    console.log('Rate limit exceeded. Please wait before retrying.');
  }
}
```

## 非同期サポート

SDK は完全に async/await ベースです。すべてのメソッドは Promise を返します。

```typescript Await any method wrap theme={null}
import Profound from '@profoundai/client';

const client = new Profound();

const categories = await client.organizations.categories.list();
```

## コード例

API ドキュメントのすべてのエンドポイントには、動作する JavaScript のサンプルが含まれています。REST API & SDKs タブの **Endpoints** セクションで、次の内容を扱う例を参照できます：

* 組織の管理
* カテゴリーの検出
* レポートの生成
* 生データへのアクセス

<Tip>
  API の改善に伴い、SDK のインターフェースが変更される場合があります。本番環境では特定のバージョンに固定してください。
</Tip>
